echartLine.vue
5.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<div :ref="id" class="canvas"></div>
</template>
<script>
// 按需引入 echarts
import * as echarts from "echarts"
export default {
data() {
return {
client: "",
chart: ""
}
},
props: {
colors: {
type: Array,
default: () => ["#FFC154", "#FF9671"]
},
xData: {
type: Array,
default: []
},
yData: {
type: Array,
default: []
},
id: {
type: String,
default: ""
},
title: {
type: String,
default: ""
}
},
watch: {
yData: {
handler() {
this.chart.setOption(this.getChartSetOption())
},
deep: true
}
},
mounted() {
this.client = this.$refs[this.id]
this.chart = echarts.init(this.client)
this.chart.setOption(this.getChartSetOption())
window.addEventListener("resize", this.resizeHandler)
},
methods: {
resizeHandler() {
if (this.chart) {
this.chart.clear()
setTimeout(() => {
this.chart.setOption(this.getChartSetOption())
this.chart.resize()
}, 1000)
}
},
getMax() {
let tickIntervalNum = 0
if (this.yData.length) {
let s = this.yData[0]
for (var i = 1; i < this.yData.length; i++) {
if (s < this.yData[i]) {
s = this.yData[i]
}
}
tickIntervalNum = s
}
return tickIntervalNum || 1
},
getChartSetOption() {
let maxVal = this.getMax()
// 计算合适的最大值,向上取整到最接近的整数
let yAxisMax = Math.ceil(maxVal)
// 确保最小值为0
let yAxisMin = 0
// 计算间隔,确保正好显示5个点 (0, interval, 2*interval, 3*interval, 4*interval)
let interval = Math.ceil(yAxisMax / 5)
var option = {
grid: {
top: "37",
bottom: "0",
left: "13",
right: "16",
containLabel: true
},
tooltip: {
trigger: "axis",
axisPointer: {
lineStyle: {
type: "solid"
}
},
padding: 12,
borderColor: "transparent",
borderWidth: 1,
backgroundColor: "rgba(255, 255, 255, 0.95)",
borderRadius: 4,
textStyle: {
color: "rgba(0, 0, 0, 0.65)",
fontSize: 14
},
formatter: (params) => {
return `
<div style="font-weight:normal;color:rgba(0, 0, 0, 0.65); line-height: 20px;margin-bottom:4px;">${params[0].name}</div>
<div style="rgba(0, 0, 0, 0.65);display:flex;align-items:center; line-height: 20px;"> <div style="width: 6px;
height: 6px;background: ${params[0].color};border-radius:50%;margin-right:7.8px;"></div>
${this.title}:${params[0].value}辆
</div>
`
}
},
xAxis: {
type: "category",
// boundaryGap: false,
// splitNumber: 4,
axisLine: {
show: false
},
axisTick: {
show: false,
lineStyle: {
color: "#fff"
}
},
splitLine: {
show: false, // 隐藏网格线
color: "#E9E9E9"
},
axisLine: {
show: false
},
axisLabel: {
// rotate: 30, // 横轴名称旋转
// interval: 0,// 强制显示所有标签
show: true,
color: "rgba(0, 0, 0, 0.65)",
fontSize: "12px"
},
data: this.xData
},
yAxis: {
type: "value",
name: "辆",
nameTextStyle: {
color: "rgba(0, 0, 0, 0.65)",
align: "right"
},
offset: 10,
labelLine: {
show: true
},
axisLine: {
show: false,
lineStyle: {
color: "#E7EBEF"
}
},
axisLabel: {
show: true,
color: "rgba(0, 0, 0, 0.65)",
fontSize: "12px"
},
splitLine: {
show: true, // 根据隐藏网格线
color: "#E9E9E9"
},
axisTick: {
show: false
},
// 设置y轴固定显示5个点
min: yAxisMin,
max: interval * 5, // 确保最大值是间隔的4倍,这样就有0,1,2,3,4共5个点
interval: interval
},
series: [
{
type: "line",
avoidLabelOverlap: false,
label: {
formatter: "{c}%",
position: "top",
fontSize: 11,
show: false,
color: "#999999"
},
showSymbol: true,
itemStyle: {
color: "#2A58AD"
},
data: this.yData
}
]
}
return option
},
destroyChart() {
// 移除窗口 resize 事件监听器
window.removeEventListener("resize", this.resizeHandler)
// 销毁 ECharts 实例
if (this.chart) {
this.chart.dispose()
this.chart = null
}
}
},
beforeDestroy() {
this.destroyChart()
},
// 如果使用的是 Vue 3,则使用 beforeUnmount
beforeUnmount() {
this.destroyChart()
}
}
</script>
<style scoped lang="scss">
.canvas {
width: 100%;
height: 100%;
}
</style>