PieChartDeviceSub.vue
3.1 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
<template>
<div v-show="dataSeries.length" ref="chartRef" :style="{ height, width }"></div>
<div class="empty-box" v-show="!dataSeries.length"
><Empty :image="Empty.PRESENTED_IMAGE_SIMPLE"
/></div>
</template>
<script lang="ts">
import { defineComponent, PropType, ref, Ref, onMounted, toRefs, computed, watch } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import { Empty } from 'ant-design-vue';
import { seriesDataT } from './props';
import { useAppStore } from '/@/store/modules/app';
export default defineComponent({
components: { Empty },
props: {
width: {
type: String as PropType<string>,
default: '100%',
},
height: {
type: String as PropType<string>,
default: '200px',
},
legendData: {
type: Array,
default: () => [],
},
seriesData: {
type: Array,
default: () => [],
},
isCircle: {
type: Boolean,
default: true,
},
},
setup(props) {
const appStore = useAppStore();
const { legendData, seriesData } = toRefs(props);
const dataSeries: Ref<seriesDataT[]> = ref([]);
const legendDatas: Ref<seriesDataT[]> = ref([]);
dataSeries.value = seriesData.value as unknown as seriesDataT[];
legendDatas.value = legendData.value as unknown as seriesDataT[];
const skinName = computed(() => {
return appStore.getDarkMode === 'light' ? '#ffffff' : '#151515';
});
const chartRef = ref<HTMLDivElement | null>(null);
const { setOptions, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
const getOptions: any = () => {
return {
backgroundColor: skinName.value,
tooltip: {
trigger: 'item',
formatter: '{b} {d}%',
},
series: [
{
avoidLabelOverlap: false,
type: 'pie',
radius: props.isCircle ? '60%' : ['40%', '60%'],
data: dataSeries.value,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)',
},
},
labelLine,
},
],
};
};
watch(
() => appStore.getDarkMode,
(target) => {
const backgroundColor = target === 'light' ? '#ffffff' : '#151515';
setOptions &&
setOptions({
...getOptions(),
backgroundColor,
});
}
);
const labelLine = {
normal: {
show: true,
length2: 1,
},
} as any;
onMounted(() => {
setOptions(getOptions());
//自适应
window.addEventListener('resize', () => resize());
});
return { chartRef, Empty, dataSeries };
},
});
</script>
<style>
.empty-box {
display: flex;
align-items: center;
margin: 0 120px;
}
</style>