index.js
1.77 KB
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
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'
import { previewDataInterface } from '@/api/systemData/dataInterface'
export default {
mixins: [resize],
props: {
title: { type: String, default: '' },
dataType: { type: String, default: 'static' },
propsApi: { type: String, default: '' },
option: { type: Object, default: () => {} }
},
data() {
return {
chart: null,
currOption: {},
isEmpty: false
}
},
mounted() {
if (this.dataType === 'dynamic') {
if (!this.propsApi) return
previewDataInterface(this.propsApi).then(res => {
this.currOption = res.data
this.resetChart()
})
} else {
this.currOption = this.option
this.initChart()
}
},
watch: {
option: {
handler(val) {
this.currOption = val
this.resetChart()
},
deep: true
},
dataType(val) {
if (val !== 'dynamic') {
this.currOption = this.option
this.resetChart()
}
},
propsApi(val) {
if (this.dataType === 'static') return
if (!val) return
previewDataInterface(val).then(res => {
this.currOption = res.data
this.resetChart()
})
}
},
methods: {
initChart() {
this.chart = echarts.init(this.$refs.chart)
this.chart.setOption(this.currOption)
setTimeout(() => {
this.$nextTick(() => {
this.chart.resize();
})
}, 50);
},
resetChart() {
this.isEmpty = JSON.stringify(this.currOption) === "{}"
this.chart && this.chart.dispose()
this.chart = null
if (!this.isEmpty) this.initChart()
}
},
beforeDestroy() {
if (!this.chart) return
this.chart.dispose()
this.chart = null
}
}