pieChart.vue
2.25 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
<template>
<div class="chart" :id="id"></div>
</template>
<script>
export default {
name: "pieChart",
props: {
id: String,
params: Array,
tooltipFormatter:false
},
watch: {
params: {
handler: function (val) {
if (val.length) {
this.initData();
}
},
deep: true,
},
},
data() {
return {
chart: null,
};
},
created() {},
mounted() {
this.initData();
},
methods: {
setOption() {
const that = this;
const options = {
color: this.colors || ["#ff80db", "#c8cc00", "#67c6b5"],
backgroundColor: "#f8f8f8",
tooltip: {
trigger: "item",
confine: true,
formatter(v) {
return `${v.marker} ${v.name}:${v.value}${that.tooltipFormatter?'%':''}`
},
},
legend: {
show: false,
bottom: 40,
left: 60,
tight: 60,
selectedMode: false,
formatter: function (name) {},
textStyle: {
rich: {
b: {
color: "#999",
},
},
},
},
series: {
type: "pie",
center: ["50%", "50%"], // 位置
radius: [0, "80%"],
data: that.params,
label: {
normal: {
formatter(v) {
let rate = v.data.rate*100
return v.name + rate.toFixed(2) + "%";
},
},
},
selectedMode: true,
hoverAnimation: true,
avoidLabelOverlap: true,
animationType: "scale",
animationEasing: "elasticOut",
animationDelay() {
return Math.random() * 100;
},
},
};
return options;
},
initData() {
if (!this.chart) {
const div = document.getElementById(this.id);
this.chart = this.$echarts.init(div);
}
const options = this.setOption();
this.chart?.clear();
this.chart.setOption(options, true);
this.chart.off("click");
this.chart.on("click", "series", (params) => {
this.$emit("clickPieChart", params);
});
},
},
};
</script>
<style scoped>
.chart {
height: 100%;
}
</style>