PieChart.vue
8.11 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
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
<template>
<div class="pie-box">
<div :id="id" :style="{ height: height }" class="pie-chart"></div>
<!-- <div class="center-content" :style="{ fontSize: size }">
<div :class="[id, 'center-content-num', 'center-content-num-color']"></div>
</div> -->
<!-- <div class="content-desc">
<div class="content-desc-icon" :style="{ background: descColor }"></div>
{{ numDesc }}
</div> -->
</div>
</template>
<script>
import * as echarts from 'echarts'
import utils from '@/utils/util.js'
export default {
name: 'PieChart',
props: {
id: {
type: String,
default: ''
},
keys: {
type: Array,
default: () => ['name', 'value']
},
height: {
type: String,
default: '8.54vh'
},
list: {
type: Array,
default: () => []
},
radius: {
type: Array,
default: () => ['50%', '80%']
},
colors: {
type: Array,
default: () => ['#16E193', '#747DFF', '#E588FF', '#34AFFF', '#FFC200', '#EB6001']
},
size: {
type: String,
default: '12px'
},
formatter: {
type: String,
default: ''
},
formatterName: {
type: String,
default: ''
},
formtUnit: String
},
data() {
return {
option: '',
echartsPie: '',
mTime: '',
viewData: [],
text: '',
subtext: '',
currentIndex: -1,
mainValue: 0,
numDesc: '',
descColor: ''
}
},
watch: {
list: {
handler(newVal) {
if (newVal.length > 0) {
this.initData()
this.$nextTick(() => {
this.draw()
})
}
},
immediate: true
}
},
mounted() {},
methods: {
// 初始化数据
initData() {
const { list, keys } = this
this.viewData = []
list.forEach(item => {
this.viewData.push({
name: item[keys[0]],
value: item[keys[1]],
avarage: item[keys[2]]
})
})
},
draw() {
if (this.mTime) clearInterval(this.mTime)
this.echartsPie = echarts.init(document.getElementById(this.id))
window.addEventListener('resize', () => {
this.echartsPie.resize()
})
const _this = this
// 指定图表的配置项和数据
this.option = {
// tooltip: {
// trigger: 'item',
// label: {
// show: true
// }
// },
color: this.colors,
series: [
{
name: '客户年龄分布',
type: 'pie',
radius: this.radius,
center: ['50%', '50%'],
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: 'center'
},
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: this.size,
fontWeight: 'bold',
color: '#fff',
formatter: params => {
const _dataIndex = params.dataIndex
// 价格
const _purchaseamount = utils.numAddSymbol(_this.list[_dataIndex].purchaseamount)
// 均价
const _avarage = utils.numAddSymbol(_this.list[_dataIndex].averprice)
// 总价
let _totalValue = 0
_this.list.forEach(item => {
_totalValue += item[this.keys[1]]
})
_totalValue = utils.numAddSymbol(_totalValue)
let _html
/**
* price -- 价格+均价组合
* total -- 名称+总数组合
* none -- 不显示内容
* 默认显示名称
*/
switch (_this.formatter) {
case 'price':
_html = `{a|¥${_purchaseamount}}\n{b|均价¥ ${_avarage}}`
break
case 'total':
_html = `${_this.formatterName}\n{c|${_totalValue}${_this.formtUnit || ''}}`
break
case 'none':
_html = ''
break
case 'rate':
_html = utils.toPercent(params.percent)
break
default:
_html = utils.legend(params.name)
}
return _html
},
rich: {
a: {
color: '#c8c8c8'
},
b: {
color: '#ffffff66',
fontSize: '12px',
lineHeight: '28'
},
c: {
lineHeight: '20'
}
}
}
},
labelLine: {
show: false
},
data: this.viewData,
minAngle: 30
}
]
}
this.echartsPie.clear()
this.mainValue = 0
this.numDesc = ''
this.echartsPie.setOption(this.option)
this.pieActive()
this.mTime = setInterval(this.pieActive, 5000)
// 鼠标划出
this.echartsPie.on('mouseout', () => {
if (this.mTime) clearInterval(this.mTime)
this.mTime = setInterval(this.pieActive, 5000)
})
this.echartsPie.on('mouseover', data => {
this.mainValue = data.data.value
this.numDesc = data.data.name
this.descColor = data.color
// 停止定时器,清除之前的高亮
clearInterval(this.mTime)
this.currentIndex = data.dataIndex
this.highlightPie()
this.emitEvent(data.data)
})
},
// 事件触发
emitEvent(data) {
this.echartsPie.setOption(this.option)
this.$emit('pieAction', data)
},
highlightPie() {
// 取消所有高亮并高亮当前图形
// 遍历饼图数据,取消所有图形的高亮效果
for (var idx in this.option.series[0].data) {
this.echartsPie.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: idx
})
}
// 高亮当前图形
this.echartsPie.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: this.currentIndex
})
this.descColor = this.option.color[this.currentIndex]
},
// 高亮
pieActive() {
const dataLen = this.option.series[0].data.length
this.currentIndex = (this.currentIndex + 1) % dataLen
this.highlightPie()
this.emitEvent(this.viewData[this.currentIndex])
}
}
}
</script>
<style lang="scss" scoped>
.pie-box {
// background: #262525;
position: relative;
flex: 1;
.center-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
max-width: 40%;
&-num-color {
color: #fff;
}
&-num {
// overflow: hidden;
// text-overflow: ellipsis;
// white-space: nowrap;
// font-size: 0.6vw;
--step: 0;
counter-reset: count var(--step);
z-index: var(--step);
transition: --step 0.5s;
&::before {
content: counter(count);
}
}
&-desc {
font-size: 0.5vw;
color: #606266;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
animation: rotateIn 3s infinite;
}
}
.content-desc {
display: flex;
align-items: center;
font-size: 8px;
color: #c8c8c8;
text-align: center;
font-weight: 400;
position: absolute;
top: 91%;
left: 50%;
transform: translateX(-50%);
text-align: center;
height: 14px !important;
white-space: nowrap;
&-icon {
width: 8px;
height: 4px;
border-radius: 2px;
margin-right: 4px;
}
}
}
@property --step {
syntax: '<integer>';
inherits: false;
initial-value: 15;
}
@keyframes rotateIn {
from {
transform: rotateY(0turn);
}
to {
transform: rotateY(-1turn);
}
}
</style>