jiangping
2023-08-11 5c98be0b50b8ca78ed05358cbd434193e75dcd8a
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
<template>
    <div ref="main1" style="width: 100%; height: 300px;"></div>
</template>
 
<script setup lang="ts">
    import {ref, inject, onMounted, defineProps, onUnmounted} from 'vue'
 
    // 获取ref对象
    const main1 = ref()
 
    // 接收父级组件参数
    const props = defineProps({
        legendData: {
            type: Array,
            required: true
        },
        seriesData: {
            type: Array,
            required: true
        }
    })
 
    // 获取echarts实例对象
    const echarts: any = inject('ec')
 
    // 赋值操作
    const init = (): void => {
        let myChart = echarts.init(main1.value)
        let option = {
            tooltip: {
                trigger: "item",
                backgroundColor: "#ffffff",
                axisPointer: {
                    type: 'shadow'
                },
                confine: true,
                textStyle: {
                    color: "#000000",
                    fontSize: 15
                }
            },
            grid: {
                top: '0%',
                left: '2%',
                right: '2%',
                containLabel: true
            },
            legend: {
                bottom: '0',
                itemHeight: 10,
                itemWidth: 10,
                data: props.legendData
            },
            series: [
                {
                    type: "pie",
                    top: '-20%',
                    radius: ["30%", "40%"],
                    labelLine: {
                        normal: {
                            length: 20,
                            length2: 60,
                        }
                    },
                    label: {
                        formatter: "{per|{d}%}{b|{b}}\n\n",
                        borderWidth: 20,
                        borderRadius: 4,
                        padding: [0, -70],
                        rich: {
                            b: {
                                color: "#000",
                                fontSize: 12,
                                lineHeight: 33
                            },
                            per: {
                                fontSize: 12,
                                padding: [2, 4],
                                borderRadius: 2
                            }
                        }
                    },
                    data: props.seriesData
                }
            ]
        }
 
        myChart.setOption(option)
    }
 
    const resizeEvent = () => {
        let myChart = echarts.init(main1.value)
        myChart.resize()
    }
 
    // 初始化图表
    onMounted(() => {
        init()
        window.addEventListener('resize', resizeEvent, false)
    })
 
    // 组件销毁
    onUnmounted(() => {
        window.removeEventListener('resize', resizeEvent)
    })
</script>
 
<style lang="scss" scoped>
 
</style>