Mr.Shi
2023-08-11 6e19ef63c0a87588abd76c04ab228a73e3060ea4
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 ref="main" style="width: 100%; height: 300px;"></div>
</template>
 
<script setup lang="ts">
    import { ref, inject, onMounted, defineProps, onUnmounted } from 'vue'
 
    // 获取ref对象
    const main = ref()
 
    // 接收父级组件参数
    const echarts: any = inject('ec')
 
    // 接收父级组件参数
    const props = defineProps({
        legendData: {
            type: Array,
            required: true
        },
        seriesData: {
            type: Array,
            required: true
        },
        xAxisData: {
            type: Array,
            required: true
        }
    })
 
    // 赋值操作
    const init = (): void => {
        let myChart1 = echarts.init(main.value)
        let option = {
            tooltip: {
                trigger: 'axis',
                backgroundColor: "#ffffff",
                axisPointer: {
                    type: 'shadow'
                },
                confine: true,
                textStyle: {
                    fontSize: 12,
                    color: "#000000"
                }
            },
            legend: {
                bottom: '0',
                itemHeight: 10,
                itemWidth: 10,
                data: props.legendData
            },
            grid: {
                top: '5%',
                left: '2%',
                right: '2%',
                bottom: '15%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                axisLabel: {
                    textStyle: {
                        fontSize: 11
                    }
                },
                axisTick: {
                    show: false
                },
                data: props.xAxisData
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    textStyle: {
                        fontSize: 11
                    }
                },
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                }
            },
            series: props.seriesData
        };
 
        myChart1.setOption(option);
 
        myChart1.resize()
    }
 
    const resizeEvent = () => {
        let myChart1 = echarts.init(main.value)
        myChart1.resize()
    }
 
    // 初始化图表
    onMounted(() => {
        init()
        window.addEventListener('resize', resizeEvent, false)
    })
 
    // 组件销毁
    onUnmounted(() => {
        window.removeEventListener('resize', resizeEvent)
    })
</script>
 
<style lang="scss" scoped>
 
</style>