PieChart.vue
4.09 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
<template>
<div class="pie-box">
<div id="pie-chart" class="pie-chart"></div>
</div>
</template>
<script>
import * as echarts from 'echarts'
export default {
name: 'PieChart',
props: {
keys: {
type: Array,
default: () => ['name', 'number']
},
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' }
]
}
},
data() {
return {
echartsPie: '',
mTime: '',
viewData: []
}
},
created() {
this.initData()
},
mounted() {
this.echartsPie = echarts.init(document.getElementById('pie-chart'))
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() {
// 指定图表的配置项和数据
const option = {
tooltip: {
trigger: 'item'
},
// color: ['#289df5', '#ff5050'],
// graphic: {
// type: 'text',
// left: 'center',
// top: '40%',
// style: {
// text: '运动达标率',
// textAlign: 'center',
// fill: '#333',
// fontSize: 20,
// fontWeight: 700
// }
// },
title: {
text: '主标题',
// 副标题
subtext: '50%',
// 主副标题间距
itemGap: 40,
x: 'center',
y: 'center',
top: '280',
// 主标题样式
textStyle: {
fontSize: '12',
color: '#ffffff',
textAlign: 'center',
fontWeight: 500
},
// 副标题样式
subtextStyle: {
fontSize: '10',
fontWeight: '400',
color: '#606266'
}
},
series: [
{
name: '客户年龄分布',
type: 'pie',
radius: ['55%', '70%'],
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
},
data: this.viewData
}
]
}
this.echartsPie.setOption(option)
this.pieActive()
// 鼠标划出
this.echartsPie.on('mouseout', () => {
this.pieActive()
})
},
//高亮
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
}
}, 2000)
// 鼠标划入
this.echartsPie.on('mouseover', () => {
// 停止定时器,清除之前的高亮
clearInterval(this.mTime)
this.echartsPie.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: index
})
})
}
}
}
</script>
<style lang="scss" scoped>
.pie-box {
height: 14vh;
.pie-chart {
flex: 1;
height: 100%;
}
}
</style>