LTVChart.vue 6.1 KB
<template>
  <div class="ltv-chart-container">
    <!-- Controls Header -->
    <div class="chart-controls">
      <div class="control-item">
        <span class="label">窗口期</span>
        <el-select v-model="windowPeriod" placeholder="请选择" size="small" style="width: 120px;">
          <el-option v-for="item in windowOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </div>

      <div class="control-item">
        <span class="label">展示指标</span>
        <el-select v-model="metric" placeholder="请选择" size="small" style="width: 120px;">
          <el-option v-for="item in metricOptions" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </div>

      <div class="control-item">
        <span class="label">展示分组值</span>
        <el-select v-model="selectedGroups" multiple collapse-tags placeholder="请选择" size="small" style="width: 200px;">
          <el-option v-for="item in groupOptions" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </div>

      <div class="control-item">
        <el-button type="primary" size="small" @click="resetControls">重置</el-button>
      </div>
    </div>

    <!-- Chart Container -->
    <div ref="chart" class="chart-body" style="height: 400px; width: 100%;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'LTVChart',
  data() {
    return {
      windowPeriod: 'LTV0',
      metric: '总营收',
      selectedGroups: ['20-30岁', '30-40岁', '40-50岁'],

      windowOptions: [
        { label: 'LTV 当天', value: 'LTV0' },
        { label: 'LTV1', value: 'LTV1' },
        { label: 'LTV2', value: 'LTV2' },
        { label: 'LTV3', value: 'LTV3' },
        { label: 'LTV4', value: 'LTV4' },
        { label: 'LTV5', value: 'LTV5' },
        { label: 'LTV6', value: 'LTV6' },
        { label: 'LTV7', value: 'LTV7' }
      ],
      metricOptions: ['总营收', 'ARPU', 'ARPPU'],
      groupOptions: ['<20岁', '20-30岁', '30-40岁', '40-50岁', '50-60岁', '≥60岁'],

      chart: null
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
    window.removeEventListener('resize', this.handleResize);
  },
  watch: {
    windowPeriod() { this.updateChart(); },
    metric() { this.updateChart(); },
    selectedGroups() { this.updateChart(); }
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    resetControls() {
      this.windowPeriod = 'LTV0';
      this.metric = '总营收';
      this.selectedGroups = ['20-30岁', '30-40岁', '40-50岁'];
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart);
      this.updateChart();
    },
    getLast7Days() {
      const dates = [];
      const today = new Date();

      for (let i = 6; i >= 0; i--) {
        const d = new Date(today);
        d.setDate(today.getDate() - i);
        const year = d.getFullYear();
        const month = String(d.getMonth() + 1).padStart(2, '0');
        const date = String(d.getDate()).padStart(2, '0');
        dates.push(`${year}-${month}-${date}`);
      }
      return dates;
    },
    getPseudoRandom(seed) {
      let value = 0;
      for (let i = 0; i < seed.length; i++) {
        value += seed.charCodeAt(i);
      }
      const sinValue = Math.sin(value) * 10000;
      return Math.floor((sinValue - Math.floor(sinValue)) * (280 - 150) + 150);
    },
    updateChart() {
      if (!this.chart) return;

      const dates = this.getLast7Days();

      // Mock data generation based on selected groups
      const series = this.selectedGroups.map(group => {
        return {
          name: group,
          type: 'line',
          smooth: false,
          data: dates.map(date => {
            // Create a deterministic seed based on filter criteria
            const seed = `${this.windowPeriod}-${this.metric}-${group}-${date}`;
            return this.getPseudoRandom(seed);
          })
        };
      });

      const option = {
        tooltip: {
          trigger: 'axis',
          backgroundColor: '#fff',
          textStyle: {
            color: '#333'
          },
          formatter: (params) => {
            let result = `${params[0].axisValue}: ${this.metric}<br/>`;
            params.forEach(param => {
              result += `${param.marker} ${param.seriesName} &nbsp;&nbsp;&nbsp; ${param.value}<br/>`;
            });
            return result;
          }
        },
        legend: {
          bottom: 0,
          data: this.selectedGroups,
          icon: 'rect',
          itemWidth: 12,
          itemHeight: 12
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: dates,
          axisLine: {
            lineStyle: {
              color: '#eee'
            }
          },
          axisLabel: {
            color: '#666'
          }
        },
        yAxis: {
          type: 'value',
          min: 150, // Adjust based on mock data range
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              type: 'dashed',
              color: '#eee'
            }
          },
          axisLabel: {
            color: '#666'
          }
        },
        series: series,
        color: ['#409EFF', '#67C23A', '#9b59b6', '#E6A23C', '#73c0de', '#3ba272', '#fc8452', '#9a60b4']
      };

      this.chart.setOption(option, true);
    }
  }
};
</script>

<style scoped>
.ltv-chart-container {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  /* margin-top: 20px; */
}

.chart-controls {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.control-item {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.control-item .label {
  margin-right: 10px;
  font-size: 14px;
  color: #606266;
  font-weight: 500;
}
</style>