<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: []
    },
    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: 10,
          showSymbol: false,
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#0F8DDF'
              },
              {
                offset: 1,
                color: '#ffffff'
              }
            ])
          },
          data: item,
          yAxisIndex: index
        })
        yAxis.push({
          type: 'value',
          // name: '(%)',
          axisTick: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            align: 'right',
            formatter: '{value}%',
            textStyle: {
              fontSize: 10,
              color: '#B6B6B6'
            }
          },
          splitLine: {
            show: index == 0,
            lineStyle: {
              color: '#F6F6F6',
              type: 'dashed',
            }
          }
        })
      })
      this.chart.setOption({
        color: ['#0F8DDF', '#5b8ff9', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
        tooltip: {
          trigger: 'axis',
          formatter: '{b}<br />{a}: {c}%',
          backgroundColor: 'rgba(0, 0, 0, 0.7)',
          borderWidth: 0,
          padding: [8, 6],
          textStyle: {
            color: '#FFFFFF',
            fontSize: 10,
          },
          axisPointer: {
            lineStyle: {
              color: '#F6F6F6'
            }
          }
        },
        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: '7%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              type: 'dashed',
              color: '#F6F6F6'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            fontSize: 10,
            color: '#B6B6B6',
          },
          data: xLineDate
        }],
        yAxis: yAxis,
        series: series
      })
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
    }
  }
}
</script>