Pie.vue
2.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
<template>
<div class='pie'>
<div
class="pie-canvas"
ref='data'
></div>
<div class='pie-title'>
<slot></slot>
</div>
</div>
</template>
<script type='text/ecmascript-6'>
export default {
name: 'Pie',
props: {
list: {
type: Array,
default: []
}
},
data () {
return {
roseColors: ['#773ae6', '#372ef0', '#5242f3', '#0060ff', '#0096ff', '#00e5dd', '#36a8ff', '#0078f4'],
canvas: null
}
},
watch: {
'list' (n, o) {
if (n.length > 0) {
this.createdCanvas()
}
}
},
methods: {
createdCanvas () {
if (this.canvas) this.canvas.clear()
let roseColors = []
let data = JSON.parse(JSON.stringify(this.list))
let sum = data.reduce((pre, next) => pre + Number(next.value), 0)
data.forEach((e, i) => {
roseColors.push(this.roseColors[8 - data.length + i])
})
this.canvas = echarts.init(this.$refs.data)
let options = {
series: [{
type: 'pie',
radius: '65%',
center: ['50%', 150],
color: roseColors,
data: data,
label: {
normal: {
show: false,
formatter: ['{b|{b}}', '{d|{d}%}'].join('\n'),
rich: {
d: {
color: '#fff',
fontSize: 16,
},
b: {
color: '#fff',
fontSize: 16,
lineHeight: 20
},
},
}
},
labelLine: {
normal: {
lineStyle: {
color: '#fff',
},
smooth: 0.1,
length: 1,
length2: 14,
}
},
itemStyle: {
normal: {
shadowColor: 'rgba(0,0,0,0.5)',
shadowBlur: 50,
}
}
}]
}
let option = options.series[0]
this.canvas.on('mouseover', params => {
option.data[params.dataIndex].labelLine = option.data[params.dataIndex].label = {
normal: {
show: true
}
}
this.canvas.setOption(options)
})
this.canvas.on('mouseout', params => {
option.data[params.dataIndex].label = option.data[params.dataIndex].labelLine = {
normal: {
show: false
}
}
})
this.canvas.setOption(options)
}
}
}
</script>
<style scoped lang='scss'>
.pie {
width: 100%;
height: 100%;
position: relative;
&-canvas {
width: 100%;
height: 100%;
}
&-title {
position: absolute;
left: 0;
bottom: 18px;
width: 100%;
text-align: center;
font-size: 24px;
color: #8e99a6;
}
}
</style>