<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: {
        loginArray: {
            type: Array
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                type: [],
            },
            option: {
                tooltip: {
                    trigger: 'item'
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['80%', '100%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            borderRadius: 10,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                        label: {
                            show: true,
                            position: 'center',
                            // formatter: '{b}\n{c}'
                            fontSize: 20,
                            rich: {
                                a: {
                                    fontSize: 30,
                                },
                                b: {
                                    lineHeight: 20,
                                    fontSize: 14,
                                    color: '#00000099'
                                }
                            }
                            // formatter: function (params) {
                            //     console.log(params);
                            //     var sum = 0;
                            //     for (var i = 0; i < params.data.length; i++) {
                            //         sum += params.data[i].value;
                            //     }
                            //     return sum + '\n表单总数';
                            // }
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: 20,
                                rich: {
                                    a: {
                                        fontSize: 30,
                                    },
                                    b: {
                                        lineHeight: 20,
                                        fontSize: 14,
                                        color: '#00000099'
                                    }
                                }
                                // formatter: '{c}'
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        hoverOffset: 0, // 设置鼠标悬停时不放大
                        data: [
                            { value: 10, name: '进行中', itemStyle: { normal: { color: '#3673e8' } } },
                            { value: 8, name: '未发布', itemStyle: { normal: { color: '#61b8ff' } } },
                            { value: 6, name: '已完成', itemStyle: { normal: { color: '#96d8a8' } } },
                            { value: 7, name: '逾期', itemStyle: { normal: { color: '#f5b763' } } },
                        ]
                    }
                ]
            },
        };
    },
    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].label.emphasis = _this.option.series[0].label.formatter = function (params) {
                    console.log(params);
                    var sum = 0;
                    for (var i = 0; i < _this.option.series[0].data.length; i++) {
                        sum += _this.option.series[0].data[i].value;
                    }
                    return '{a|' + sum + '}\n{b|表单总数}';
                }
                // 初始化雷达图
                _this.chartObj = chartObj;
                _this.chartObj.setOption(_this.option);
            });
        },
        setJson(array) {
            this.ooption = {
                xdata: [],
                sdata: [],
                max: 0
            }

            if (!this.chartObj) {
                this.setChart(array);
            } else {
                this.option.series[0].data = array;
                this.chartObj.setOption(this.option);
            }
        }
    },
    watch: {
        loginArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.setJson(newValue)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.setJson(this.loginArray)
        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>