<template>
    <echartsBase :option="setOption"/>
</template>

<script setup>
import echartsBase from './index.vue'
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'

const props = defineProps({
    color: {
        type: Array,
        default: () => (['#5087EC', '#68BBC4'])
    },
    datas: {
        type: Array,
        default: () => ([])
    }
})

//初始变量
const setOption = ref({})
const colors = ref(props.color)
const datas = ref(props.datas)

watch(() => [
    props.color
], ([color]) => {
    colors.value = color
    setOptions()
})

//深度监听数据变化
watch(() => [
    props.datas,
], ([data]) => {
    datas.value = data
    setOptions()
}, { deep: true })

//设置图表
const setOptions = () => {
    setOption.value = {
        color: colors.value,
        tooltip: {
            trigger: "item"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            left: '-7',
        },
        series: [
            {
                type: 'pie',
                radius: '80%',
                center: ["76%", "50%"],
                label: {
                    show: false,
                    position: "center",
                },
                labelLine: {
                    show: false,
                },
                data: datas.value,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    }
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        setOptions()
    })
})
</script>
