PieChart.vue
4.76 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
<template>
<div class="pie-box" >
<div :id="id" :style="{ width: width, height: height }" class="pie-chart"></div>
</div>
</template>
<script>
import * as echarts from 'echarts'
export default {
name: 'PieChart',
props: {
id: {
type: String,
default: ''
},
keys: {
type: Array,
default: () => ['name', 'number']
},
height: {
type: String,
default: '12.7vh'
},
width: {
type: String,
default: '7.2vw'
},
list: {
type: Array,
default: () => [
{ number: 1048, name: 'Search Engine' },
{ number: 735, name: 'Direct' },
{ number: 580, name: 'Email' },
{ number: 484, name: 'Union Ads' },
{ number: 300, name: 'Video Ads' }
]
},
radius: {
type: Array,
default: () => ['55%', '72%']
},
colors: {
type: Array,
default: () => ['#4FA0FF', '#7CFFB2', '#FDF006', '#EB6001', '#F5AA01', '#BF98FF', '#58D9F9']
}
},
data() {
return {
option: '',
echartsPie: '',
mTime: '',
viewData: [],
text: '',
subtext: ''
}
},
created() {
this.initData()
},
mounted() {
let id = this.id
this.echartsPie = echarts.init(document.getElementById(id))
window.addEventListener('resize', () => {
this.echartsPie.resize()
})
this.draw()
},
methods: {
//初始化数据
initData() {
let { list, viewData, keys } = this
list.forEach(item => {
viewData.push({
name: item[keys[0]],
value: item[keys[1]]
})
})
},
draw() {
// 指定图表的配置项和数据
this.option = {
// tooltip: {
// trigger: 'item'
// },
color: this.colors,
title: {
text: this.text,
// 副标题
subtext: this.subtext,
// 主副标题间距
itemGap: 5,
x: 'center',
y: 'center',
top: '50',
// 主标题样式
textStyle: {
fontSize: '12',
color: '#ffffff',
textAlign: 'center',
fontWeight: 500
},
// 副标题样式
subtextStyle: {
fontSize: '10',
fontWeight: '400',
color: '#606266'
}
},
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: false,
fontSize: '14',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
animationType: 'scale',
animationEasing: 'elasticOut',
animationDurationUpdate: 3000,
animationTypeUpdate: 'transition',
animation: true,
data: this.viewData
}
]
}
this.echartsPie.setOption(this.option)
this.pieActive()
// 鼠标划出
this.echartsPie.on('mouseout', () => {
this.pieActive()
})
},
// 事件触发
emitEvent(data) {
this.text = data.name
this.subtext = data.value
this.echartsPie.setOption(this.option)
this.$emit('pieAction', data)
},
//高亮
pieActive() {
let index = -1 //高亮所在下标
let dataLength = this.viewData.length // 当前饼图有多少个扇形
// 用定时器控制饼图高亮
this.mTime = setInterval(() => {
// 清除之前的高亮
this.echartsPie.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: index
})
index = (index + 1) % dataLength
// 当前下标高亮
this.echartsPie.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: index
})
if (index > dataLength) {
index = 0
}
this.emitEvent(this.viewData[index])
}, 2000)
// 鼠标划入
this.echartsPie.on('mouseover', data => {
// 停止定时器,清除之前的高亮
clearInterval(this.mTime)
this.echartsPie.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: index
})
console.log('mouseover')
console.log(data)
this.emitEvent(data.data)
})
}
}
}
</script>
<style lang="scss" scoped>
.pie-box {
background: #262525;
.pie-chart {
}
}
</style>