HolPieChart.vue
4.35 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
<template>
<div :id="chartId" class="chart-container"></div>
</template>
<script>
import * as echarts from 'echarts'
export default {
props: {
title: {
type: String,
default: ''
},
unit: {
type: String,
default: '万(㎡)'
},
tiplabel: {
type: String,
default: ''
},
chartId: {
type: String,
required: true
},
data: {
type: Array,
default: () => {
return []
}
},
showPercent: {
type: Boolean,
default: true
},
showCenter: {
type: Boolean,
default: false
},
centerValue: {
type: String,Number,
default: ''
},
},
data() {
return {
myChart: undefined
}
},
watch: {
data: {
handler: function(newVal, oldVal) {
this.init()
this.setOptions()
},
deep: true
}
},
mounted() {
this.init()
},
methods: {
init() {
this.myChart = echarts.init(document.getElementById(this.chartId))
this.setOptions()
window.addEventListener('resize', () => {
setTimeout(() => {
this.myChart.resize()
}, 300)
})
},
setOptions() {
if(!this.myChart) {
return
}
let option
if (this.data && this.data.length == 0 ) {
option = {
title: {
text: '暂无数据',
x: 'center',
y: 'center',
textStyle: {
fontSize: 18,
fontWeight: 'normal',
color: '#1578D9'
}
}
}
} else {
option = {
title: {
text: this.title ? this.title : '',
x: 'center',
top: '20',
textStyle: {
fontSize: 18,
fontWeight: 'normal',
color: '#000'
}
},
legend: {
show: false,
right: '10',
orient: 'vertical',
},
graphic: this.showCenter ? [
{
//环形图中间添加文字
type: "text", //通过不同top值可以设置上下显示
left: "center",
top: "center",
style: {
text: `${this.centerValue}\n平方米`,
textAlign: "center",
fill: "#1E7CE8", //文字的颜色
fontSize: 15,
lineHeight: 18,
},
},
] : [],
tooltip: {
trigger: 'item',
backgroundColor: '#ffffff',
borderColor:'#1578D9',
formatter: (params) => {
let unit = this.unit
let tiplabel = this.tiplabel
let res=""
res+='<div style="font-size: 14px;text-align: left;color: #000000;">'+ '<div><span>'+params.name+'</span></div>'+ '<div ><span>'+tiplabel+':'+params.value+unit+'</span></div>'+'<div><span>比例:'+params.percent+'%'+'</span></div>' +'</div>'
return res
},
},
series: [
{
type: 'pie',
data: this.data,
radius: ['30%', '50%'],
label: {
formatter: (series) => {
console.log("series",series)
const data = series.data
return `{a|${data.name}}\n{b|${data.value}}{c|${this.unit}},{d|${series.percent}%}`
},
show: true,
verticalAlign: 'middle',
borderWidth: 0,
width: 100,
// backgroundColor: '#1578D9',
borderStatus: 30,
rich: {
a: {
padding: [12, 0, 2, 0],
// color: '#eeeded',
fontSize: 16,
align: 'center',
},
b: {
padding: [2, 0, 8, 0],
// color: '#f2f2f2',
fontSize: 16,
align: 'center',
}
}
},
labelLine: {
show: true
},
}
]
}
}
this.myChart.clear()
this.myChart.setOption(option)
}
}
}
</script>
<style scoped lang="scss">
.chart-container {
height: 100%;
}
</style>