<template>
    <div
        class="linechart"
        :id="id"
        style="width: calc(37.5vw - 180px); height: calc(42vh - 165px)"
    ></div>
</template>

<script>
//import  from '';

export default {
    name: "pieChartMainPage", // 首页-饼图
    components: {},
    data() {
        return {
            charts: "",
            colors: [
                "#5B8FF9",
                "#61DDAA",
                "#65789B",
                "#F6BD16",

                "#FF5C5C",
                "#4671C6",
                "#7CCE7A",
                "#FFC655",
                "#4BC2DF",
                "#00A476",
                "#FF7D46",
                "#A45DB2",
            ],
        };
    },

    props: {
        id: {
            default: "pieChartMainPage",
            type: String,
        },
        seriesName: {
            default: "统计",
            type: String,
        },
        stateName: {
            default: "name",
            type: String,
        },
        dataList: {
            default: function () {
                return [];
            },
            type: Array,
        },
    },

    watch: {
        dataList(val) {
            if (val.length > 0) {
                this.initStateList();
            }
        },
    },

    methods: {
        initStateList() {
            let dataList = this.dataList;

            let data = [];

            for (let index = 0; index < dataList.length; index++) {
                const element = dataList[index];
                data[index] = {
                    value: element.num,
                    name: element[this.stateName] + "\n" + element.num,
                    itemStyle: {
                        normal: {
                            color: this.colors[index],
                        },
                    },
                };
            }

            let option = {
                title: {
                    text: "暂无数据",
                    show: dataList.length === 0,
                    left: "center",
                    top: "center",
                },
                tooltip: {
                    //提示框，可以在全局也可以在
                    trigger: "item", //提示框的样式
                    // formatter: "{a} <br/>{b}: {c} ({d}%)",
                    formatter: "{a} <br/>{b} ({d}%)",
                    color: "#000", //提示框的背景色
                    textStyle: {
                        //提示的字体样式
                        color: "#ffffff",
                    },
                    confine: true,
                },
                series: [
                    {
                        name: this.seriesName,
                        type: "pie",
                        data,
                        top: 15,
                        avoidLabelOverlap: true,
                        label: {
                            edgeDistance: 15,
                            distance: 25,
                        },
                    },
                ],
            };

            this.charts.setOption(option);
        },
    },

    mounted() {
        this.charts = this.$echarts.init(document.getElementById(this.id));

        this.initStateList();

        this.$nextTick(() => {
            window.addEventListener("resize", () => {
                this.charts.resize();
            });
        });
    },
    destroyed() {
        // console.log("pie destroyed");
        this.charts.dispose();
    },
};
</script>

<style lang='less' scoped>
.linechart {
    margin: 0 auto;
    overflow: hidden;
}
</style>