riku
2025-06-11 30cf71affd5149a1310d84f658b028d267beee8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<template>
  <div class="chart-wrap">
    <div
      ref="lineChart"
      class="line-chart"
      :style="'height:' + chartHeight + ';'"
    ></div>
  </div>
</template>
<script>
import * as echarts from 'echarts';
import { smallLineOption } from '@/utils/chart/chart-option';
 
export default {
  props: {
    modelValue: {
      type: Object
      // default: () => {
      //   return {
      //     xAxis: [],
      //     series: []
      //   };
      // }
    },
    chartHeight: {
      type: String,
      default: '140px'
    }
  },
  data() {
    return {
      option: null
    };
  },
  watch: {
    modelValue: {
      handler() {
        this.refreshChart();
      },
      deep: false,
      immediate: true
    }
  },
  methods: {
    refreshChart() {
      const { xAxis, series } = this.modelValue;
      if (!this.option) {
        this.option = smallLineOption(xAxis, series);
      } else {
        this.option.xAxis[0].data = xAxis;
        this.option.series = series;
      }
      if (this.lineChart) {
        this.lineChart.setOption(this.option, { notMerge: true });
        // console.log('折线图生成:立即');
      } else {
        this.onChartCreated = () => {
          this.lineChart.setOption(this.option, { notMerge: true });
        };
      }
    }
  },
  mounted() {
    this.lineChart = echarts.init(this.$refs.lineChart);
    this.onChartCreated();
    // setTimeout(() => {
    //   if (typeof this.onChartCreated === 'function') {
    //     this.onChartCreated();
    //     // console.log('折线图生成:滞后');
    //   }
    // }, 500);
  }
};
</script>
<style scoped>
.chart-wrap {
  /* width: 300px; */
  display: flex;
  justify-content: center;
}
.line-chart {
  width: 318px;
  /* height: 140px; */
  /* border-bottom: 1px solid rgba(255, 255, 255, 0.329); */
}
</style>