<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/dashboard/mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
        pie_1Data: {
            type: Array,
            default: () => {
                return [
                    { value: 0, name: '计划需求量', itemStyle: { color: '#65dfe0' } },
                    { value: 0, name: '已供应量', itemStyle: { color: '#ff8e9d' } },
                    { value: 0, name: '待供应量', itemStyle: { color: '#6ccaf6' } }
                ]
            }
        }
    },
    data() {
        return {
            chart: null
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart(this.pie_1Data)
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    watch: {
        pie_1Data: {
            deep: true,
            handler(val) {
                this.initChart(val)
            }
        }
    },
    methods: {
        initChart(pie_1Data) {
            this.chart = echarts.init(this.$el, 'macarons')
            const total = pie_1Data.reduce((sum, item) => sum + item.value, 0)
            this.chart.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} : {c} ({d}%)'
                    // formatter: '{b} {c}'
                },
                legend: {
                    left: 'center',
                    bottom: '0',
                    icon: 'circle',
                    data: ['计划需求量', '已供应量', '待供应量']
                },
                series: [
                    {
                        type: 'pie',
                        // roseType: 'radius',  // radius 为玫瑰饼图
                        radius: [0, 95],
                        center: ['50%', '50%'],
                        // data: [
                        //     { value: 320, name: '待供应量', itemStyle: { color: '#6ccaf6' } },
                        //     { value: 240, name: '计划需求量', itemStyle: { color: '#65dfe0' } },
                        //     { value: 149, name: '已供应量', itemStyle: { color: '#ff8e9d' } }
                        // ],
                        data: pie_1Data,
                        animationEasing: 'cubicInOut',
                        animationDuration: 2600,
                        itemStyle: {
                            borderWidth: 1, // 分割线宽度
                            borderColor: '#fff' // 分割线颜色（白色）
                        },
                        label: {
                            show: true, // 显示标签
                            // formatter: '{b}: {d}%', // 显示 name 和比率
                            formatter: function (params) {
                                const percent =
                                    params.data.value == 0 ? 0 : ((params.data.value / total) * 100).toFixed(2)
                                return `${params.name}: ${percent}%`
                            },
                            fontSize: 12, // 字体大小
                            color: '#333' // 字体颜色
                        }
                    }
                ]
            })
        }
    }
}
</script>
