<template>
    <div class="RoundChart-box">
        <div ref="echart" class="RoundChart-echarts" :style="`width : ${chart?.clientWidth}px`"/>
    </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { nextTick, onMounted, onUnmounted, ref } from 'vue'

//初始变量
let chart = null;
const echart = ref(null)

//初始化图表
const initChart = () => {
    chart = echarts.init(echart.value)
    setOptions()
}

//监听浏览器窗口变化
const windowResize = () => {
    window.addEventListener("resize", resizeEvent);
}

const resizeEvent = () => {
    window.requestAnimationFrame(() => {
        chart.resize();
    })
}

//设置图表
const setOptions = () => {
    chart.setOption({
        tooltip: {
            trigger: "item",
            formatter: "{d}%"
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['80%', '90%'],
                center: ["50%", "50%"],
                label: {
                    show: false,
                    position: "center",
                },
                labelLine: {
                    show: false,
                },
                data: [
                    {
                        value: 30,
                        name: "完成率",
                        itemStyle: {
                            color: "#0081ff",
                        }
                    },
                    {
                        value: 70,
                        name: "",
                        itemStyle: {
                            color: "#F5F5F5",
                        }
                    },
                ]
            }
        ]
    })
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        initChart()
        windowResize()
    })
})

//被卸载
onUnmounted(() => {
    window.removeEventListener("resize",resizeEvent);
    chart.dispose()
    chart = null
})
</script>

<style lang="scss" scoped>
.RoundChart-box {
    height: 100%;
    overflow: hidden;
    position: relative;
    .RoundChart-echarts {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2;
        width: 100%;
        height: 100%;
    }
}
</style>
