<template>
    <div class="data_body">
        <div style="width: 100%; height: 100%">
            <div id="charts_canvas" class="echart" style="width: 100%; height: 100%" v-show="this.ooption.data.length"></div>
            <div v-show="!this.ooption.data.length" class="noneBox">暂无数据</div>
        </div>
    </div>
</template>
  
<script>
export default {
    props: {
        dataJ: {
            type: Object
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                type: [],
            },
            option: {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: {d}%'
                },
                legend: {
                    bottom: '5%',
                    left: 'center'
                },
                series: [
                    {
                        name: '',
                        bottom: '15%',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            borderRadius: 10,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                        label: {
                            show: true,
                            formatter: '{b}: {d}%'
                        },
                        labelLine: {
                            show: true
                        },
                        data: [
                            // { value: 1048, name: 'Search Engine' },
                            // { value: 735, name: 'Direct' },
                            // { value: 580, name: 'Email' },
                            // { value: 484, name: 'Union Ads' },
                            // { value: 300, name: 'Video Ads' }
                        ]
                    }
                ]
            },
        };
    },
    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;
                let _this = this
                _this.option.series[0].data = array.data
                // 初始化雷达图
                _this.chartObj = chartObj;
                _this.chartObj.setOption(_this.option);
            });
        },
        setJson(array) {
            this.ooption = {
                data: []
            }
            for(var i = 0; i < array.length; i++){
                if(array[i].count > 0){
                    this.ooption.data.push({value: array[i].count, 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);
            }
        }
    },
    watch: {
        dataJ: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setJson(newValue)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.setJson(this.dataJ)
        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; */
}

.noneBox { 
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    width: 100%;
}
</style>