<template>
  <div :class="className"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    refName: {
      type: String,
      default: 'chart'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '365px'
    },
    smooth: {
      type: Boolean,
      default: true
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  // watch: {
  //   chartData: {
  //     deep: true,
  //     handler(val) {
  //       // this.setOptions(val)
  //     }
  //   }
  // },
  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')
      this.renewOption(this.chartData)
    },
    renewOption(chart_data) {
      var series = []
      for (let i = 0; i < chart_data.legend.length; i++) {
        series[i] = {
          name: chart_data.legend[i],
          smooth: this.smooth,
          type: 'line',
          data: chart_data.dataY[i],
          symbolSize: 15,
          symbol: 'circle',
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }
      }
      this.setOptions(series)
    },
    setOptions(series) {
      this.chart.setOption({
        color: ['#ffd030'],
        xAxis: {
          type: 'category',
          data: this.chartData.dataX,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#fff'
            }
          },
          axisLabel: {
            // x轴文字的配置
            show: true,
            // rotate: 20,
            interval: 0, // 使x轴文字显示全
            textStyle: {
              color: '#fff',
              fontSize: 14,
              padding: [10, 0, 0, 0]
            }
          },
          offset: 30
        },
        grid: {
          left: '3%',
          right: '10%',
          bottom: '40',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'category',
          splitLine: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#fff',
              fontSize: 16
            }
          },
          offset: 10
        },
        legend: {
          top: 0,
          data: this.chartData.legend,
          textStyle: {
            color: '#fff'
          }
        },
        series: series
      })
    }
  }
}
</script>
