懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 <template>
2   <div :class="className" :style="{height:height,width:width}" />
3 </template>
4
5 <script>
6 import * as echarts from 'echarts'
7 require('echarts/theme/macarons') // echarts theme
8 import resize from './mixins/resize'
9
10 export default {
11   mixins: [resize],
12   props: {
13     className: {
14       type: String,
15       default: 'chart'
16     },
17     width: {
18       type: String,
19       default: '100%'
20     },
21     height: {
22       type: String,
23       default: '350px'
24     },
25     autoResize: {
26       type: Boolean,
27       default: true
28     },
29     chartData: {
30       type: Object,
31       required: true
32     }
33   },
34   data() {
35     return {
36       chart: null
37     }
38   },
39   watch: {
40     chartData: {
41       deep: true,
42       handler(val) {
43         this.setOptions(val)
44       }
45     }
46   },
47   mounted() {
48     this.$nextTick(() => {
49       this.initChart()
50     })
51   },
52   beforeDestroy() {
53     if (!this.chart) {
54       return
55     }
56     this.chart.dispose()
57     this.chart = null
58   },
59   methods: {
60     initChart() {
61       this.chart = echarts.init(this.$el, 'macarons')
62       this.setOptions(this.chartData)
63     },
64     setOptions({ expectedData, actualData } = {}) {
65       this.chart.setOption({
66         xAxis: {
67           data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
68           boundaryGap: false,
69           axisTick: {
70             show: false
71           }
72         },
73         grid: {
74           left: 10,
75           right: 10,
76           bottom: 20,
77           top: 30,
78           containLabel: true
79         },
80         tooltip: {
81           trigger: 'axis',
82           axisPointer: {
83             type: 'cross'
84           },
85           padding: [5, 10]
86         },
87         yAxis: {
88           axisTick: {
89             show: false
90           }
91         },
92         legend: {
93           data: ['expected', 'actual']
94         },
95         series: [{
96           name: 'expected', itemStyle: {
97             normal: {
98               color: '#FF005A',
99               lineStyle: {
100                 color: '#FF005A',
101                 width: 2
102               }
103             }
104           },
105           smooth: true,
106           type: 'line',
107           data: expectedData,
108           animationDuration: 2800,
109           animationEasing: 'cubicInOut'
110         },
111         {
112           name: 'actual',
113           smooth: true,
114           type: 'line',
115           itemStyle: {
116             normal: {
117               color: '#3888fa',
118               lineStyle: {
119                 color: '#3888fa',
120                 width: 2
121               },
122               areaStyle: {
123                 color: '#f3f8ff'
124               }
125             }
126           },
127           data: actualData,
128           animationDuration: 2800,
129           animationEasing: 'quadraticOut'
130         }]
131       })
132     }
133   }
134 }
135 </script>