Chart.vue
5.3 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<template>
<div :id="'chart' + index" style="height: 360px;width: 100%"></div>
</template>
<script>
export default {
props: {
option: {
type: Object,
default: () => {}
},
index: {
type: Number,
default: -1
},
//是否转换options
isConvertOption: {
type: Boolean,
default: false
}
},
watch: {
option: {
handler: function(newVal, oldVal) {
this.initChart()
},
deep: true
}
},
mounted() {
this.initChart()
},
methods: {
initChart() {
let chart = this.$echarts.init(
document.getElementById('chart' + this.index)
)
chart.clear() //清空图表实例
let myOption = {}
if (this.isConvertOption) {
if (
this.option &&
this.option.series &&
this.option.series[0].type === 'pie'
) {
myOption = {
title: {
text: this.option.name,
subtext: this.option.subtext
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)'
},
legend: {
data: this.option.legend
},
series: this.option.series
}
} else {
myOption = {
title: {
text: this.option.name,
subtext: this.option.subtext,
top: 20
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
calculable: true,
legend: {
x: 'right',
data: this.option.legend
},
toolbox: {
show: true,
orient: 'vertical',
y: 'center',
right: 20,
itemGap: 13,
feature: {
mark: {show: true},
magicType: {
show: true,
type: ['line', 'bar', 'stack', 'tiled']
},
//重写Restore
myTool1: {
show: true,
title: 'Restore',
icon:
'path://M512 981.333333c-209.866667 0-396.693333-126.026667-466.293333-314.08a35.52 35.52 0 0 1 23.626666-44.426666 38.613333 38.613333 0 0 1 48 20.693333c58.666667 158.933333 217.013333 265.493333 394.666667 265.6s336-106.666667 394.666667-266.133333a37.6 37.6 0 0 1 28.853333-23.626667 38.986667 38.986667 0 0 1 35.786667 11.946667 34.773333 34.773333 0 0 1 7.146666 35.36c-69.386667 188.373333-256.48 314.666667-466.453333 314.666666z m431.36-574.08a37.92 37.92 0 0 1-35.946667-24.266666C849.386667 222.56 690.613333 114.88 512 114.72S174.72 222.346667 116.746667 382.773333A38.72 38.72 0 0 1 69.333333 403.733333a35.786667 35.786667 0 0 1-24.106666-44.373333C113.333333 169.866667 301.013333 42.666667 512 42.666667s398.666667 127.306667 467.146667 316.96a34.56 34.56 0 0 1-4.906667 32.64 38.933333 38.933333 0 0 1-30.88 14.986666z',
onclick: () => {
// 还原
chart.clear()
chart.setOption(myOption)
}
},
// restore: { show: true },
saveAsImage: {show: true},
dataZoom: {
yAxisIndex: 'none'
},
dataView: {
show: true,
readOnly: true,
optionToContent: function(opt) {
let axisData = opt.xAxis[0].data
let series = opt.series
let tdHeaders = '<th></th>' //表头
series.forEach(function(item) {
tdHeaders += '<th>' + item.name + '</th>' //组装表头
})
let table =
'<div class="table-responsive"><table class="table table-bordered table-striped table-hover" style="text-align:center"><tbody><tr>' +
tdHeaders +
'</tr>'
let tdBodys = '' //数据
for (let i = 0, l = axisData.length; i < l; i++) {
for (let j = 0; j < series.length; j++) {
tdBodys += '<td>' + series[j].data[i] + '</td>' //组装表数据
}
table +=
'<tr><td style="padding: 0 10px">' +
axisData[i] +
'</td>' +
tdBodys +
'</tr>'
tdBodys = ''
}
table += '</tbody></table></div>'
return table
}
}
}
},
grid: {
y: 80,
y2: 40,
x2: 40
},
xAxis: [
{
type: 'category',
data: this.option.xAxis
}
],
yAxis: [
{
type: 'value'
}
],
series: this.option.series
}
}
} else {
myOption = this.option
}
chart.setOption(myOption)
}
}
}
</script>
<style scoped></style>