<template>
    <div class="data_body">
        <!--  v-if="this.ooption.xdata.length" -->
        <div style="width: 100%; height: 100%">
            <div id="charts_canvas" class="echart" style="width: 100%; height: 100%; "></div>
        </div>
        <!-- <div style="width: 100%; height: 100%;display: flex;align-items: center;justify-content: center;"
            v-show="!this.ooption.xdata.length">暂无数据</div> -->
    </div>
</template>
  
<script>
export default {
    props: {
        evCourseArray: {
            type: Array,
            default: []
        },
        eva: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                sdata: [],
            },
            option: {
                tooltip: {},
                radar: {
                    axisName: {
                        color: "#000"
                    },
                    // shape: 'circle',
                    indicator: [
                        // { name: '目标一', max: 5 },
                        // { name: '目标二', max: 5 },
                        // { name: '目标三', max: 5 },
                        // { name: '目标四', max: 5 },
                        // { name: '目标五', max: 5 }
                    ]
                },
                series: [
                    {
                        name: '学生综合评价',
                        type: 'radar',
                        data: [
                            {
                                areaStyle: {
                                    opacity: 0.2,
                                    color: '#0061FF'
                                },
                                itemStyle: {
                                    color: '#0061FF',
                                    lineStyle: {
                                        color: '#0061FF'
                                    }
                                },
                                // 5, 4, 3, 5, 5, 2
                                value: [],
                                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.radar.indicator = option.xdata;
                // this.option.series[0].data[0].value = option.sdata;
                this.option.radar.indicator = [
                    { name: '成茁小先生', max: 5 },
                    { name: '成行小先生', max: 5 },
                    { name: '成智小先生', max: 5 },
                    { name: '成德小先生', max: 5 },
                    { name: '成彩小先生', max: 5 }
                ]
                this.option.series[0].data[0].value = [1,1,3,5,5];
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
        setArray(array) {
            this.setChart(array);
            return
            this.ooption = {
                xdata: [],
                sdata: [],
            }
            for (var i = 0; i < array.length; i++) {
                if (array[i].evid == this.eva) {
                    this.ooption.xdata = array[i].indicator
                    this.ooption.sdata = array[i].value
                    break;
                }
            }
            setTimeout(() => {
                // if (!this.chartObj) {
                this.setChart(this.ooption);
                // } else {
                //     this.option.radar.indicator = this.ooption.xdata;
                //     this.option.series[0].data[0].value = this.ooption.sdata;
                //     this.chartObj.setOption(this.option);
                // }
            }, 100);
            this.$forceUpdate();
        },
    },
    watch: {
        evCourseArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setArray(newValue)
                this.$forceUpdate();
            },
        },
        eva: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setArray(this.evCourseArray)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.setArray(this.evCourseArray)
        // 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>
  