<template>
    <div class="container-echart">
        <div :id="'echart' + timeId" class="interface-echart"></div>
    </div>
</template>
<script lang="ts" setup>
import * as echarts from 'echarts'
import { EChartsType } from 'echarts'
import { onMounted, ref, watch, onBeforeUnmount, nextTick } from 'vue'

const props = defineProps({
    message: {
        type: Array,
        default: true
    }
})
let xAxisData: string[] = [];
let data1: number[] = [];
let data2: number[] = [];
let data3: number[] = [];
let data4: number[] = [];

for (let i = 0; i < 10; i++) {
    xAxisData.push('201' + i);
    data1.push(+(Math.random() * 2).toFixed(2));
    data2.push(+(Math.random() * 5).toFixed(2));
    data3.push(+(Math.random() + 0.3).toFixed(2));
    data4.push(+Math.random().toFixed(2));
}

var emphasisStyle = {
    itemStyle: {
        shadowBlur: 10,
        shadowColor: 'rgba(0,0,0,0.3)'
    }
};
let myEchart: EChartsType
const timeId = ref(Math.floor(new Date().getTime() * Math.random())); // 使该图表保持唯id
const myEchartData = () => {
    const option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        legend: {
            data: ['发电量', '光伏发电', '风力发电','水力发电','火力发电'],
            textStyle: {
                color: 'white' // 设置字体颜色为红色
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '发电量',
                min: 0,
                interval: 50,
                axisLabel: {
                    formatter: '{value}'
                }
            },
        ],
        series: [
            {
                name: '光伏发电',
                type: 'bar',
                stack: 'Ad',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' Kwh';
                    }
                },
                data: [20, 10, 40, 20, 10, 50, 20, 30, 40, 10, 20, 40]
            },
            {
                name: '风力发电',
                type: 'bar',
                stack: 'Ad',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' Kwh';
                    }
                },
                data: [20, 30, 30, 30, 90, 30, 20, 30, 40, 50, 30, 20]
            },
            {
                name: '水力发电',
                type: 'bar',
                stack: 'Ad',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' Kwh';
                    }
                },
                data: [20, 30, 30, 30, 90, 30, 20, 30, 40, 50, 30, 20]
            },
            {
                name: '火力发电',
                type: 'bar',
                stack: 'Ad',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' Kwh';
                    }
                },
                data: [20, 30, 30, 30, 90, 30, 20, 30, 40, 70, 10, 20]
            },
            {
                name: '发电量',
                type: 'line',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' KWH';
                    }
                },
                data: [80, 100, 120, 110, 280, 140, 80, 120, 160, 180, 90, 100]
            }
        ]
    };
    // { notMerge: true } 解决删除数据时,数据不刷新的问题
    myEchart.setOption(option, { notMerge: true })
}
watch(
    //监控数据变化
    () => props.message,
    () => {
        setTimeout(() => {
            myEchartData()
        }, 500)
    },
    { deep: true }
)
onMounted(() => {
    setTimeout(() => {
        const dom = document.getElementById(`echart${timeId.value}`) as any
        myEchart = echarts.init(dom)
        myEchartData()
    }, 500)

    // 当窗口发生变化时
    window.addEventListener('resize', () => {
        myEchart.resize()
    })
})
onBeforeUnmount(() => {
    window.removeEventListener('resize', () => {
        myEchart.resize()
    })
})
</script>
<style scoped lang="scss">
.container-echart {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.interface-echart {
    width: 100%;
    height: 100%;
}
</style>