<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: {
        evCourseArray: {
            type: Array,
            default: []
        },
        eva: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                sdata: [],
            },
            option: {
                xAxis: {
                    type: 'value',
                    show:false,
                },
                grid:{
                    left: '15%',
                    right: '5%',
                    bottom: '5%',
                    top: '5%',
                },
                yAxis: {
                    inverse: true,
                    type: 'category',
                    data: [
                        '休闲状态(<=50%)',
                        '热身状态(50-60%)',
                        '脂肪燃烧(60-70%)',
                        '心肺提升(70-80%)',
                        '乳酸闽值(80-90%)',
                        '极限无氧(>=90%)'
                    ]
                },
                series: [
                    {
                        showBackground: true,
                        data: [
                            {
                                value: 10,
                                itemStyle: {
                                    color: 'rgb(157, 157, 157)'
                                }
                            },
                            {
                                value: 20,
                                itemStyle: {
                                    color: 'rgb(15, 216, 242)'
                                }
                            },
                            {
                                value: 40,
                                itemStyle: {
                                    color: 'rgb(0, 208, 146)'
                                }
                            },
                            {
                                value: 80,
                                itemStyle: {
                                    color: 'rgb(255, 192, 29)'
                                }
                            },
                            {
                                value: 70,
                                itemStyle: {
                                    color: 'rgb(247, 123, 1)'
                                }
                            },
                            {
                                value: 15,
                                itemStyle: {
                                    color: 'rgb(255, 58, 32)'
                                }
                            },
                        ],
                        type: 'bar'
                    }
                ]
            },
        };
    },
    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.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>
  