<template>
  <div :id="id" :style="{height:height,width:width}"/>
</template>
<script>
import * as echarts from 'echarts'
export default {
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    lineTitle: {
      default: []
    },
    xLineDate: {
      default: []
    },
    yLineDate: {
      default: []
    },
    legendShow: {
      default: true
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOption(xLineDate, yLineDate) {
      var series = []
      var yAxis = []
      yLineDate.forEach((item, index) => {
        series.push({
          name: this.lineTitle[index],
          type: 'line',
          smooth: true,
          symbol: 'emptyCircle',
          symbolSize: 8,
          showSymbol: true,
          data: item,
          yAxisIndex: index
        })
        yAxis.push({
          type: 'value',
          // name: '(%)',
          axisTick: {
            show: false
          },
          axisLine: {
            show: index == 0,
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            margin: 10,
            textStyle: {
              fontSize: 14
            }
          },
          splitLine: {
            show: index == 0,
            lineStyle: {
              color: '#57617B'
            }
          }
        })
      })
      this.chart.setOption({
        color: ['#5ad8a6', '#5b8ff9', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#000'
            }
          }
        },
        legend: {
          top: 20,
          icon: 'rect',
          itemWidth: 14,
          itemHeight: 5,
          itemGap: 13,
          data: this.lineTitle,
          show: this.legendShow,
          left: '4%',
          textStyle: {
            fontSize: 12,
            color: '#000'
          }
        },
        grid: {
          top: this.legendShow ? 70 : 20,
          left: '2%',
          right: '2%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          data: xLineDate
        }],
        yAxis: yAxis,
        series: series
      })
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
    }
  }
}
</script>