<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_2Data: {
            type: Array,
            default: () => {
                return [
                    { value: 0, name: '在库量', itemStyle: { color: '#26a8ff' } },
                    { value: 0, name: '在用量', itemStyle: { color: '#45d8d9' } },
                    { value: 0, name: '在修量', itemStyle: { color: '#ff8e9d' } },
                    { value: 0, name: '新购待入库量', itemStyle: { color: '#6ccaf6' } },
                    { value: 0, name: '修试待入库量', itemStyle: { color: '#ffb667' } }
                ]
            }
        }
    },
    data() {
        return {
            chart: null
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart(this.pie_2Data)
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    watch: {
        pie_2Data: {
            deep: true,
            handler(val) {
                this.initChart(val)
            }
        }
    },
    methods: {
        initChart(pie_2Data) {
            this.chart = echarts.init(this.$el, 'macarons')
            const total = pie_2Data.reduce((sum, item) => sum + item.value, 0)
            this.chart.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} : {c} ({d}%)'
                },
                legend: {
                    left: 'center',
                    bottom: '0',
                    icon: 'circle',
                    data: ['在库量', '在用量', '在修量', '新购待入库量', '修试待入库量']
                },
                series: [
                    {
                        type: 'pie',
                        // roseType: 'radius',  // radius 为玫瑰饼图
                        radius: [60, 95],
                        center: ['50%', '50%'],
                        // data: [
                        //     { value: 320, name: '在库量', itemStyle: { color: '#26a8ff' } },
                        //     { value: 240, name: '在用量', itemStyle: { color: '#45d8d9' } },
                        //     { value: 120, name: '在修量', itemStyle: { color: '#ff8e9d' } },
                        //     { value: 80, name: '新购待入库量', itemStyle: { color: '#6ccaf6' } },
                        //     { value: 90, name: '修试待入库量', itemStyle: { color: '#ffb667' } }
                        // ],
                        data: pie_2Data,
                        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>
