PieChartDeviceStatus.vue
3.43 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
<template>
<a-row type="flex" justify="space-between" align="middle">
<a-col
class="flex items-center"
v-for="(item, index) in seriesStatusData"
:key="index"
:span="8"
>
<a-col
:id="`chartPie${item.key}`"
style="width: 10rem; height: 12rem; position: relative; top: -0.7rem"
/>
</a-col>
</a-row>
</template>
<script lang="ts">
import {
defineComponent,
PropType,
onMounted,
toRefs,
shallowReactive,
onUnmounted,
nextTick,
} from 'vue';
import * as echarts from 'echarts';
import { seriesDataT } from './props';
export default defineComponent({
props: {
seriesStatusData: {
type: Array as PropType<seriesDataT[]>,
default: () => [],
},
},
setup(props) {
const chartsInstance = shallowReactive<{ [key: string]: echarts.ECharts }>({});
const { seriesStatusData } = toRefs(props);
const total = seriesStatusData.value
.map((m) => m.value)
.reduce((prev, cur) => prev! + cur!, 0);
nextTick(() => {
for (const item of seriesStatusData.value) {
const { key, value } = item;
//牵引线条颜色
const labelLine = {
normal: {
show: true,
length: 20,
length2: 20,
lineStyle: {
color: '#808080',
},
},
} as any;
const legendKey = key == 'onLine' ? '在线' : key == 'offLine' ? '离线' : '待激活';
chartsInstance[key!] = echarts.init(
document.getElementById(`chartPie${key}`) as HTMLElement
);
const option = {
backgroundColor: '#ffffff',
tooltip: {
trigger: 'item',
formatter: `${legendKey}设备${((value! / total!) * 100).toFixed(2)}%`,
},
graphic: [
{
type: 'text',
left: '42%',
top: '48%',
z: 10,
style: {
fill: '#1a1a1a',
text: value + '个',
font: '1.25vh Microsoft YaHei',
},
},
],
series: [
{
data: [item],
avoidLabelOverlap: false,
type: 'pie',
radius: ['40%', '60%'],
label: {
normal: {
show: true,
position: 'outer',
formatter: `${legendKey}${((value! / total!) * 100).toFixed(2)}%`,
borderWidth: 5,
borderRadius: 0,
padding: [90, -60],
},
},
labelLine,
},
],
};
chartsInstance[key!].setOption(option);
}
});
const resize = (flag) => {
Object.keys(chartsInstance).forEach((key) => {
const doFuncKey = chartsInstance[key];
flag === 'r' ? doFuncKey.resize() : doFuncKey.dispose();
});
};
onMounted(() => {
window.addEventListener('resize', () => resize('r'));
});
onUnmounted(() => {
window.removeEventListener('resize', () => resize('d'));
});
},
});
</script>