<template>
    <div class="data_body">
        <div style="width: 100%; height: 100%">
            <div id="charts_canvas" class="echart" style="width: 100%; height: 100%; "></div>
        </div>
    </div>
</template>
  
<script>
export default {
    props: {
        courseNumberArray: {
            type: Array,
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                data: [],
            },
            option: {
                tooltip: {
                    trigger: 'item'
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['35%', '70%'],
                        avoidLabelOverlap: false,
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: 16,
                                fontWeight: 'bold'
                            }
                        },

                        data: [
                            // { value: 1048, name: '一年级' },
                            // { value: 735, name: '二年级' },
                            // { value: 580, name: '三年级' },
                            // { value: 484, name: '四年级' },
                            // { value: 484, name: '五年级' },
                            // { value: 300, name: '六年级' }
                        ]
                    }
                ]
            },
        };
    },
    methods: {
        setChart(option) {
            // 雷达图显示的标签
            let newPromise = new Promise((resolve) => {
                resolve();
            });
            //然后异步执行echarts的初始化函数
            newPromise.then(() => {
                const chartObj = this.$echarts.init(
                    //劳动课程
                    this.$el.querySelector("#charts_canvas")
                );
                this.option.series[0].data = this.ooption.data;
                chartObj.off('click')
                let _this = this
                chartObj.on('click', function (param) {  
                    //param参数包含的内容有： 
                    //param.name：X轴的值 
                    //param.data：Y轴的值 
                    //param.value：Y轴的值 
                    //param.type：点击事件均为click 
                    //param.seriesName：legend的名称 
                    //param.seriesIndex：系列序号（series中当前图形是第几个图形第几个） 
                    //param.dataIndex：数值序列（X轴上当前点是第几个点）
                    //alert(param.seriesName);  //legend的名称
                    console.log(param);  //X轴的值
                    _this.$emit('openCourse',param.dataIndex)
                });
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
        setArray(array) {
            this.ooption = {
                data: [],
            }
            for (var i = 0; i < array.length; i++) {
                this.ooption.data.push({ value: array[i].course, name: array[i].name})
            }
            if (!this.chartObj) {
                this.setChart(this.ooption);
            } else {
                this.option.series[0].data = this.ooption.data;
                this.chartObj.setOption(this.option);
            }
            this.$forceUpdate();
        },
    },
    watch: {
        courseNumberArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setArray(newValue)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.setArray(this.courseNumberArray)
        var _this = this;
        window.addEventListener("resize", () => {
            if (_this.chartObj) {
                _this.chartObj.resize();
            }
        });
    },
};
</script>
  
<style scoped>
.data_body {
    height: 100%;
    /* display: flex; */
    position: relative;
    border-radius: 5px;
    /* border: 1px solid #eee; */
    margin: 0 auto;
    box-sizing: border-box;
    padding: 0;
    width: 95%;
    background: #fff;
}
</style>
  