gaoluyang
2025-05-19 16e22cfa6144d883ffcac0daf844322df179ea8b
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
  <div>
    <div ref="chartRef" :style="chartStyle"></div>
  </div>
</template>
 
<script setup>
import { ref, onMounted, onBeforeUnmount, watchEffect } from 'vue'
import * as echarts from 'echarts'
 
// Props
const props = defineProps({
  options: {
    type: Object,
    default: () => ({})
  },
  chartStyle: {
    type: Object,
    default: () => ({
      height: '80%',
      width: '100%'
    })
  },
  dataset: {
    type: Object,
    default: () => {}
  },
  xAxis: {
    type: Array,
    default: () => []
  },
  yAxis: {
    type: Array,
    default: () => []
  },
  series: {
    type: Array,
    default: () => []
  },
  grid: {
    type: Object,
    default: () => ({})
  },
  legend: {
    type: Object,
    default: () => ({})
  },
  tooltip: {
    type: Object,
    default: () => ({})
  },
  lineColors: {
    type: Array,
    default: () => ['#A4EEDA', '#86C1F4', '#91A0FC', '#F6C18B', '#F09595']
  },
  barColors: {
    type: Array,
    default: () => ['#A4EEDA', '#86C1F4', '#91A0FC', '#F6C18B', '#F09595']
  },
  pieColors: {
    type: Array,
    default: () => ['#A4EEDA', '#86C1F4', '#91A0FC', '#F6C18B', '#F09595']
  },
  loadingOption: {
    type: Object,
    default: () => ({
      text: '数据加载中...',
      color: '#00BAFF',
      textColor: '#000',
      maskColor: 'rgba(255, 255, 255, 0.8)',
      zlevel: 0
    })
  }
})
 
// Refs
const chartRef = ref(null)
let chartInstance = null
 
// Methods
function generateChart(option) {
  if (option.series && option.series.length > 0) {
    option.series.forEach((s, index) => {
      if (s.type === 'line') {
        s.itemStyle = {
          color: props.lineColors[index] || props.lineColors[0]
        }
        s.lineStyle = {
          color: props.lineColors[index] || props.lineColors[0]
        }
      } else if (s.type === 'bar') {
        s.itemStyle = {
          color: props.barColors[index] || props.barColors[0]
        }
      }
    })
  }
  
  chartInstance.setOption(option)
}
 
function renderChart() {
  const option = {
    backgroundColor: props.options.backgroundColor || '#fff',
    xAxis: props.xAxis,
    yAxis: props.yAxis,
    dataset: props.dataset,
    series: props.series,
    grid: props.grid,
    legend: props.legend,
    tooltip: props.tooltip
  }
  
  chartInstance.clear()
  generateChart(option)
}
 
function windowResizeListener() {
  if (!chartInstance) return
  chartInstance.resize()
}
 
// Lifecycle hooks
onMounted(() => {
  chartInstance = echarts.init(chartRef.value)
  renderChart()
  window.addEventListener('resize', windowResizeListener)
})
 
onBeforeUnmount(() => {
  if (chartInstance) {
    window.removeEventListener('resize', windowResizeListener)
    chartInstance.dispose()
    chartInstance = null
  }
})
 
// Watch all reactive props that affect the chart
watchEffect(() => {
  // 避免在 mounted 前触发
  if (chartInstance) {
    renderChart()
  }
}, {
  flush: 'post'
})
</script>