<template>
    <div id="electr1" :style="`width:100%;height:100%;`" ref="zjlx"></div>
</template>

<script>
export default {
    props: {
        descentData: {
            type: Array,
            default: undefined
        }
    },
    data() {
        return {
            device: null,
            zoom: undefined, //图表焦点校验
            h: null,
            isLoading1: false,
            time: this.time,
            barData: [], // 数值
            myChart: ''
        }
    },
    created() {},
    mounted() {
        this.setHeight()
        window.addEventListener('resize', this.setHeight)
        this.resizeEchartZ()

        //   }

        // this.$nextTick(() => {
        //   this.descent();
        // });

        //   console.log(this.time[0],'1234567');
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
            let heightd = window.sessionStorage.getItem('appheight') - 250
            this.h = heightd / 2 + 'px'
            if (this.myChart !== '') {
                this.myChart.resize()
            }
        },
        async descent() {
            this.isLoading1 = true
            this.barData = []
            let echartsColor = JSON.parse(window.localStorage.echartsColor)
            this.descentData.forEach((item, index) => {
                let obj = {
                    value: item.value,
                    name: item.name,
                    itemStyle: {
                        normal: { show: true, color: echartsColor.FigureColor[index] }
                    }
                }
                this.barData.push(obj)
                this.barData.forEach((item, i) => {
                    if (item.name == '总计') {
                        this.barData.splice(i, 1)
                    }
                })
            })
            let echarts = require('echarts')
            let domID1 = document.getElementById('electr1')
            let myChart = echarts.init(domID1)
            this.myChart = myChart
            setTimeout(() => {
                ;(domID1.style.width = document.getElementById('zjlx').offsetWidth - 0 + 'px'),
                    (domID1.style.height = (window.sessionStorage.getItem('appheight') - 200) / 2 + 'px')
            }, 0)
            this.isLoading1 = false
            let option = {
                title: {
                    text: '暂降原因',
                    left: 'center',
                    top: 0
                },
                legend: {
                    type: 'scroll',
                    orient: 'vertical',

                    left: 10,
                    top: '5%',
                    tooltip: {
                        show: true
                    }
                },
                tooltip: {
                    trigger: 'item',
                    axisPointer: {
                        type: 'shadow',
                        label: {
                            color: '#fff',
                            fontSize: 16
                        }
                    },
                    textStyle: {
                        color: '#fff',
                        fontStyle: 'normal',
                        opacity: 0.35,
                        fontSize: 14
                    },
                    backgroundColor: 'rgba(0,0,0,0.35)',
                    formatter: '{a} <br/>{b} : {c} (次)',
                    confine: true
                },
                series: [
                    {
                        name: '暂降原因',
                        type: 'pie',
                        center: ['50%', '50%'],
                        selectedOffset: 30,
                        clockwise: true,
                        label: {
                            show: false,
                            position: 'outside',
                            textStyle: {
                                //数值样式
                                color: echartsColor.WordColor
                            }
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: '12',
                                formatter: '{b}: {c} (次)',
                                color: echartsColor.WordColor
                            }
                        },
                        data: this.barData,
                        itemStyle: {
                            opacity: 1,
                            color: {
                                repeat: 'repeat'
                            }
                        }
                    }
                ]
            }
            myChart.setOption(option)
            window.echartsArr.push(myChart)
            let _this = this
            _this.$erd.listenTo(_this.$refs.zjlx, element => {
                _this.$nextTick(() => {
                    domID1.style.width = document.getElementById('zjlx').offsetWidth - 0 + 'px'
                    myChart.resize()
                })
            })
        },
        resizeEchartZ() {
            var elementResizeDetectorMaker = require('element-resize-detector')
            var erd = elementResizeDetectorMaker()
            erd.listenTo(document.getElementById('zjlx'), element => {
                let domeId2 = document.getElementById('electr1')
                domeId2.style.width = document.getElementById('zjlx').offsetWidth - 0 + 'px'
                if (this.myChart !== '') {
                    this.myChart.resize()
                }
            })
        }
    }
}
</script>

<style lang="less" scoped></style>
