<template>
<!-- <div id='chart' style="width:100%,height:100%"></div> -->
<div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
    name: 'LineChart2',
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        maxValue: {
            type: Number,

        },
        minValue: {
            type: Number,
        },
        threshold: {
            type: Number,
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            // type: Array,
            type: Object,
            required: true
        },
        title: {
            type: String,
            default: 'chart'
        },
        // dateData: {
        //   type: Array,
        //   required: true
        // },

    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(this.title, val)
            }
        },
        title: {
            deep: true,
            handler(val) {
                this.setOptions(val, this.chartData)
            }
        },
        maxValue: {
            deep: true,
            handler(val) {
                this.setOptions(val, this.chartData)
            }
        },
        minValue: {
            deep: true,
            handler(val) {
                this.setOptions(val, this.chartData)
            }
        },
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // var chartDom = document.getElementById('chart');
            // this.chart = echarts.init(chartDom)
            this.setOptions(this.title, this.chartData)
        },
        setOptions(txt, actualData) {
            console.log('txt', txt)
            console.log('chartData', actualData)
            console.log('chartData series', actualData.series)
            this.chart.setOption({
                animation: false,
                title: {
                    text: txt
                },
                xAxis: actualData.xAxis,
                // xAxis: {

                //     // type:'time',
                //     type: 'category',
                //     name: '时间',
                //     boundaryGap: false,
                //     // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

                // },

                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        animation: false
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    // max: function (value) {
                    //     return value.max * 2
                    // },
                    max: this.maxValue,
                    min: this.minValue,
                    axisTick: {
                        show: true
                    },
                    splitLine: {
                        show: false
                    },
                    boundaryGap: [0, '100%'],
                },
                legend: actualData.legend,
                // legend: {
                //     data: ['valueList']
                // },
                series: actualData.series
            })
        }
    }
}
</script>
