<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: {
        monthArray: {
            type: Array,
            default: []
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                teacher: [],
                student: [],
            },
            option: {
                tooltip: {
                    trigger: 'axis'
                },
                // legend: {
                //     data: ['老师', '学生'],
                //     right: '10'
                // },
                grid: {
                    top: '30',
                    left: '5%',
                    right: '5%',
                    bottom: '5%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: true,
                    // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
                    data: []
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '用户',
                        type: 'line',
                        // data: [120, 50, 101, 130, 110, 120]
                        areaStyle: {
                            color: 'rgb(220, 231, 251)'
                        },
                        data: [],
                        itemStyle: {
                            normal: {
                                color: function (params) {
                                    return "#106bff";
                                },
                            },
                        },
                    },
                ]
            },
        };
    },
    methods: {
        setChart(option) {
            // 雷达图显示的标签
            let newPromise = new Promise((resolve) => {
                resolve();
            });
            //然后异步执行echarts的初始化函数
            newPromise.then(() => {
                const chartObj = this.$echarts.init(
                    //劳动课程
                    this.$el.querySelector("#charts_canvas")
                );
                // this.option.xAxis.data = option.xdata;
                // this.option.series[0].data = option.teacher;
                this.option.xAxis.data = ['3月', '4月', '5月', '6月', '7月', '8月', '9月'];
                this.option.series[0].data = [300, 500, 490, 510, 102, 201, 550];
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
    },
    watch: {
        monthArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.ooption = {
                    xdata: [],
                    teacher: [],
                    student: [],
                }
                let _array = newValue
                for (var i = 0; i < _array.length; i++) {
                    this.ooption.xdata.push(_array[i].Month + '月')
                    this.ooption.teacher.push(_array[i].teacher)
                    this.ooption.student.push(_array[i].student)
                }

                if (!this.chartObj) {
                    this.setChart(this.ooption);
                } else {
                    this.option.xAxis.data = this.ooption.xdata;
                    this.option.series[0].data = this.ooption.teacher;
                    this.option.series[1].data = this.ooption.student;
                    this.chartObj.setOption(this.option);
                }
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.ooption = {
            xdata: [],
            teacher: [],
            student: [],
        }
        let _array = this.monthArray
        for (var i = 0; i < _array.length; i++) {
            this.ooption.xdata.push(_array[i].Month + '月')
            this.ooption.teacher.push(_array[i].teacher)
            this.ooption.student.push(_array[i].student)
        }
        this.setChart(this.ooption);
        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>
  