<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: {
                xAxis: {
                    type: 'category',
                    data: [],
                    axisLabel: {
                        interval: 0,
                        formatter: function (value) {
                            var result = '';
                            for (var i = 0; i < value.length; i += 2) {
                            result += value.substr(i, 2) + '\n';
                            }
                            return result;
                        }
                    }
                },
                yAxis: {
                    type: 'value',
                    minInterval: 1,
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    left: '30',
                    right: '20',
                    bottom: '30',
                    top: '20',
                    // containLabel: true
                },
                series: [
                    {
                        data: [],
                        type: 'bar',
                        label: {
                            show: true,
                            position: 'top'
                        },
                        itemStyle: {
                            color: function (params) {
                                var colorList = [
                                    '#3673e8',
                                    '#61b8ff',
                                    '#96d8a8',
                                    '#f5b763',
                                    '#fd9588',
                                    '#c23531',
                                    '#2f4554',
                                    '#61a0a8',
                                    '#d48265',
                                    '#91c7ae',
                                    '#749f83',
                                    '#ca8622'
                                ];
                                return colorList[params.dataIndex];
                            }
                        }
                    }
                ]
            },
        };
    },
    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.series[0].data = this.ooption.sdata;
                this.option.xAxis.data = this.ooption.xdata;
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
        setJson(array) {
            this.ooption = {
                xdata: [],
                sdata: [],
            }
            for (let i = 0; i < array.length; i++) {
                this.ooption.xdata.push(array[i].name);
                this.ooption.sdata.push(array[i].kcount);
            }
            if (!this.chartObj) {
                this.setChart(this.ooption);
            } else {
                this.option.series[0].data = this.ooption.sdata;
                this.option.xAxis.data = this.ooption.xdata;
                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>