封装一个仪表盘组件

最近因为工作需要,封装了一个仪表盘组件。

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<template>
<div ref="guageChart" id="charts"></div>
</template>

<script>
import * as echarts from 'echarts'
import { deepMerge } from '@/utils/index'
export default {
name: 'CgGuageChart',
data() {
return {
level: '一级',
color: '#eee305',
defaultOptions: {},
chartInstance: null,
value: 0,
}
},
props: {
chartOptions: {
type: Object,
default: () => ({}),
},
},
methods: {
// 更新颜色
updateColor(value) {
if (value >= 0 && value <= 33) {
this.color = '#eee305'
this.level = '一级'
} else if (value > 33 && value <= 66) {
this.color = '#f68d06'
this.level = '二级'
} else if (value > 66 && value <= 100) {
this.color = '#ff2023'
this.level = '三级'
}
},
// 浏览器窗口发生变化的回调
handleResize() {
if (this.chartInstance) {
this.chartInstance.resize()
}
},
// 初始化echarts
initChart() {
const chartContainer = this.$refs.guageChart
this.chartInstance = echarts.init(chartContainer)
this.updateChart()
},
// 生成配置项
generateChartOption() {
// 定义渐变色
const colorList = [
new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'red' },
{ offset: 1, color: 'blue' },
]),
]
this.defaultOptions = {
title: {
text: '风险等级', // 标题文本
left: 'center', // 标题水平位置,可选值有 'left'、'center' 或 'right',或具体的像素值
bottom: '10%', // 标题垂直位置,可选值有 'top'、'middle' 或 'bottom',也可以是具体的像素值
textStyle: {
// 标题文本样式
color: '#fff', // 文本颜色
fontSize: 18, // 字体大小
},
},
grid: {
top: 20,
left: 20,
right: 20,
bottom: 20,
},
series: [
// 最外侧的仪表盘
{
type: 'gauge',
center: ['50%', '50%'],
radius: '94%',
startAngle: 230,
endAngle: -50,
axisLabel: {
show: false,
},
pointer: {
show: false,
},
axisLine: {
lineStyle: {
color: [
[0.08, '#2a7099'],
[0.16, '#002342'],
[0.2, '#2a7099'],
[0.33, '#002342'],
[0.46, '#2a7099'],
[0.56, '#002342'],
[0.66, '#2a7099'],
[0.8, '#002342'],
[0.88, '#2a7099'],
[0.94, '#002342'],
[1, '#2a7099'],
],
width: 2,
},
},
splitLine: {
show: false,
length: 30,
distance: -30,
lineStyle: {
width: 5,
color: '#01033a',
},
},
detail: false,
axisTick: {
show: false,
},
data: [
{
value: this.chartOptions.value || this.value,
},
],
},
{
// 第二个仪表盘
type: 'gauge',
center: ['50%', '50%'],
radius: '86%',
startAngle: 230,
endAngle: -50,
axisLabel: {
show: false,
},
pointer: {
show: false,
},
axisLine: {
lineStyle: {
color: [
[0.08, '#2a7099'],
[0.16, '#002342'],
[0.2, '#2a7099'],
[0.33, '#002342'],
[0.46, '#2a7099'],
[0.56, '#002342'],
[0.66, '#2a7099'],
[0.8, '#002342'],
[0.88, '#2a7099'],
[0.94, '#002342'],
[1, '#2a7099'],
],
width: 2,
},
},
splitLine: {
show: false,
length: 30,
distance: -30,
lineStyle: {
width: 5,
color: '#01033a',
},
},
detail: false,
axisTick: {
show: false,
},
data: [
{
value: this.chartOptions.value || this.value,
},
],
},
// 第三个仪表盘
{
type: 'gauge',
center: ['50%', '50%'],
radius: '80%',
startAngle: 230,
endAngle: -50,
min: 0,
max: 100,
pointer: {
show: true,
icon: 'pin',
length: 32,
width: 20,
itemStyle: {
color: this.color,
},
},
markPoint: {
symbol: 'circle',
symbolSize: 70,
data: [
//跟你的仪表盘的中心位置对应上,颜色可以和画板底色一样
{
value: this.level,
x: 'center',
y: 'center',
itemStyle: {
color: this.color,
shadowColor: this.color,
shadowBlur: 150,
shadowOffsetX: 0,
shadowOffsetY: 0,
},
},
],
label: {
show: true,
color: '#FFF', //气泡中字体颜色
fontSize: 20,
},
},
axisLabel: {
show: false,
},
axisLine: {
lineStyle: {
color: [
[0.12, '#eee305'],
[0.33, '#eee305'],
[0.54, '#f68d06'],
[0.66, '#f68d06'],
[0.9, '#ff2023'],
[1, '#ff2023'],
],
width: 30,
},
},
detail: false,
axisTick: {
show: false,
},
splitLine: {
show: true,
length: 30,
distance: -30,
lineStyle: {
width: 5,
color: '#01033a',
},
},
splitNumber: 6,
data: [
{
value: this.chartOptions.value || this.value,
},
],
},
// 内侧圆形
{
name: '数量',
type: 'pie',
hoverAnimation: false,
clockwise: false,
radius: ['35%', '35%'],
center: ['50%', '50%'],
data: [10],
itemStyle: {
normal: {
borderWidth: 1,
borderType: 'dotted', //dotted 虚线
borderColor: this.color, // 虚线颜色
opacity: 0.4,
},
},
label: {
normal: {
show: false,
},
emphasis: {
show: false,
textStyle: {
fontSize: '14',
},
},
},
labelLine: {
normal: {
show: false,
},
},
},
// 内侧圆形
{
name: '数量',
type: 'pie',
hoverAnimation: false,
clockwise: false,
radius: ['36%', '36%'],
center: ['50%', '50%'],
data: [10],
itemStyle: {
normal: {
borderWidth: 1,
borderType: 'dotted', //dotted 虚线
borderColor: this.color, // 虚线颜色
opacity: 0.4,
},
},
label: {
normal: {
show: false,
},
emphasis: {
show: false,
textStyle: {
fontSize: '14',
},
},
},
labelLine: {
normal: {
show: false,
},
},
},
// 内侧圆形
{
name: '数量',
type: 'pie',
hoverAnimation: false,
clockwise: false,
radius: ['30%', '30%'],
center: ['50%', '50%'],
data: [10],
itemStyle: {
normal: {
borderWidth: 1,
borderType: 'dotted', //dotted 虚线
borderColor: this.color, // 虚线颜色
opacity: 0.4,
},
},
label: {
normal: {
show: false,
},
emphasis: {
show: false,
textStyle: {
fontSize: '14',
},
},
},
labelLine: {
normal: {
show: false,
},
},
},
// 内侧圆形
{
name: '数量',
type: 'pie',
hoverAnimation: false,
clockwise: false,
radius: ['32%', '32%'],
center: ['50%', '50%'],
data: [10],
itemStyle: {
normal: {
borderWidth: 1,
borderType: 'dotted', //dotted 虚线
borderColor: this.color, // 虚线颜色
opacity: 0.4,
},
},
label: {
normal: {
show: false,
},
emphasis: {
show: false,
textStyle: {
fontSize: '14',
},
},
},
labelLine: {
normal: {
show: false,
},
},
},
],
}
const mergedOptions = deepMerge({}, this.defaultOptions)
const res = deepMerge(
mergedOptions,
this.chartOptions.echartOptions || {}
)

return res
},
// 更新图表
updateChart() {
const option = this.generateChartOption()
if (this.chartInstance) {
this.chartInstance.setOption(option, true)
} else {
console.warn('ECharts instance is not initialized yet.')
}
},
},
mounted() {
window.addEventListener('resize', this.handleResize)
this.initChart()
},
beforeDestroy() {
window.removeEventListener('resize', this.handleResize)
if (this.chartInstance) {
this.chartInstance.dispose()
}
},
watch: {
'chartOptions.value': {
handler(newValue) {
this.updateColor(newValue)
this.updateChart()
},
immediate: true,
},
chartOptions: {
deep: true,
handler() {
this.updateChart()
},
},
},
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
#charts {
width: 100%;
height: 100%;
}
</style>