<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: {
        typeJArray: {
            type: Array
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                type: [],
            },
            option: {
                tooltip: {},
                radar: {
                    indicator: [
                        // { name: '煜煜星光', max: 100 },
                        // { name: '个人写真', max: 100 },
                        // { name: '明灯导航', max: 100 },
                        // { name: '教师耕耘', max: 100 },
                        // { name: '其他', max: 100 }
                    ],
                    axisName: {
                        color: '#000',
                        fontSize: 12
                    },
                    radius: ['0%', '70%'],
                    center: ['50%', '55%'],
                    nameGap: 5,
                },
                series: [
                    {
                        name: '综合平均分',
                        type: 'radar',
                        data: [
                            {
                                value: [],
                                itemStyle: {
                                    color: '#3673E8' // 设置数据点的颜色
                                },
                                lineStyle: {
                                    normal: {
                                        color: '#3673E8' // 
                                    }
                                },
                                areaStyle: {
                                    normal: {
                                        color: '#3673E833' // 
                                    }
                                }
                            }
                        ]
                    }
                ]
            },
        };
    },
    methods: {
        setChart(array) {
            // 雷达图显示的标签
            let newPromise = new Promise((resolve) => {
                resolve();
            });
            //然后异步执行echarts的初始化函数
            newPromise.then(() => {
                const chartObj = this.$echarts.init(
                    this.$el.querySelector("#charts_canvas")
                );
                //   this.option.series[0].data = array;
                this.option.radar.indicator = array.indicator;
                this.option.series[0].data[0].value = array.data;
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
        setJson(array) {
            this.ooption = {
                indicator: [],
                max: 100,
                data:[]
            }
            // this.ooption.data = [];
            // Math.ceil(100 / 100) * 100
            for(var i=0;i<array.length;i++){
                this.ooption.data.push(array[i].zcount)
                this.ooption.max = this.ooption.max > array[i].zcount ? this.ooption.max : array[i].zcount
                this.ooption.indicator.push(
                    { name: array[i].name },
                )
            }
            this.ooption.max = Math.ceil(this.ooption.max / 100) * 100
            for(var i = 0;i<this.ooption.indicator.length;i++){
                this.ooption.indicator[i].max = this.ooption.max
            }
            
            if (!this.chartObj) {
                this.setChart(this.ooption);
            } else {
                this.option.radar.indicator = this.ooption.indicator;
                this.option.series[0].data[0].value = this.ooption.data;
                this.chartObj.setOption(this.option);
            }
        }
    },
    watch: {
        typeJArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setJson(newValue)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.setJson(this.typeJArray)
        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: 100%;
    /* background: #fff; */
}
</style>