<template>
    <div class="line-chart-wrapper">
        <div ref="LineChart" :class="className" :style="{height:height,width:width}" />
    </div>
</template>

<script>
    import echarts from 'echarts'
    require('echarts/theme/macarons') // echarts theme
    import { debounce } from '@/utils'
    export default {
        props: {
            className: {
                type: String,
                default: 'chart'
            },
            width: {
                type: String,
                default: '100%'
            },
            height: {
                type: String,
                default: '400px'
            },
            autoResize: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {
                chart: null,
            }
        },
        mounted() {
            if (this.autoResize) {
                this.__resizeHandler = debounce(() => {
                    if (this.chart) {
                        this.chart.resize()
                    }
                }, 100)
                window.addEventListener('resize', this.__resizeHandler)
            }

            // 监听侧边栏的变化
            const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
            sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
        },
        beforeDestroy() {
            if (!this.chart) {
                return
            }
            if (this.autoResize) {
                window.removeEventListener('resize', this.__resizeHandler)
            }

            const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
            sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

            this.chart.dispose()
            this.chart = null
        },
        methods: {
            sidebarResizeHandler(e) {
                if (e.propertyName === 'width') {
                    this.__resizeHandler()
                }
            },
            setOptions({ dateData, profitData, revenueData } = {}) {
                this.chart.setOption({
                    legend: {
                        data: ['总收益','总利润']
                    },
                    xAxis: {
                        data: dateData,
                        boundaryGap: false,
                        axisTick: {
                            show: false
                        }
                    },
                    grid: {
                        left: 40,
                        right: 40,
                        bottom: 20,
                        top: 30,
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross'
                        },
                        padding: [5, 10]
                    },
                    yAxis: {
                        name: '数量',
                        type: 'value',
                        axisTick: {
                            show: false
                        }
                    },
                    series: [
                        {
                            name: '总利润',
                            smooth: false,
                            type: 'line',
                            itemStyle: {
                                normal: {
                                    areaStyle: {
                                        color: '#f3f8ff'
                                    }
                                }
                            },
                            data: profitData,
                            animationDuration: 2800,
                            animationEasing: 'quadraticOut'
                        },
                        {
                            name: '总收益',
                            smooth: false,
                            type: 'line',
                            data: revenueData,
                            animationDuration: 2800,
                            animationEasing: 'quadraticOut'
                        }
                    ]
                })
            },
            initChart(data) {
                this.chart = echarts.init(this.$refs.LineChart, 'macarons')
                this.setOptions(data)
            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .line-chart-wrapper {
        position: relative;
    }
</style>
