<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import {debounce} from '../utils/debounce'

export default {
  name: 'lineChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
    }
  },
  mounted() {
    // console.log(this.chartData)
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions({xData, yData, legendData} = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        color:['#FFB130','#01AEFE'],
        legend: {
          data:legendData,
          bottom:'2%',
          textStyle: {
            color: '#59AAF7'
          }
        },
        grid: {
          top:'8%',
          left: '3%',
          right: '4%',
          bottom: '14%',
          containLabel: true
        },

        xAxis: {
          axisLabel : {
            textStyle: {
              color: '#fff'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          data: xData
        },
        yAxis: {
          type: 'value',
          axisLabel : {
            formatter: '{value}',
            textStyle: {
              color: '#fff'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          splitNumber:3
        },
        series: [
          {
            name:legendData[0],
            type:'line',
            data:yData.indata,
            lineStyle: {
              color: '#FFB130'
            }
          },
          {
            name:legendData[1],
            type:'line',
            data:yData.outdata,
            lineStyle: {
              color: '#08B9FC'
            }
          }
        ]

      })
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>
