<template>
  <div ref="rtLineChart" :id="id" class="chart"></div>
</template>

<script>
export default {
  name: 'rtLineChart',
  props: {
    id: {
      type: String,
      default: 'lineChart'
    },
    lineChartColumn: {
      type: Array,
      default: []
    },
    // [{name: '今年', data: [1, 2, 3]}, {name: '去年', data: [2, 3, 4]}]
    lineSeriesData: {
      type: Array,
      default: []
    },
    toolTipFormatter: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      series: [],
      // TODO: 颜色需要扩展
      color: ['#FE8D19', '#1286FD'],
      shadowColor: ['#FFC07F', '#7EBCFF'],
      legendData: []
    };
  },
  methods: {
    loadChart() {
      this.lineSeriesData.forEach((item, index) => {
        this.series.push({
          name: item.name,
          type: 'line',
          data: item.data,
          lineStyle: {
            normal: {
              width: 3,
              color: {
                type: 'linear',
                colorStops: [
                  {
                    offset: 1,
                    color: this.color[index]
                  }
                ],
                globalCoord: false
              },
              shadowColor: this.shadowColor[index],
              shadowBlur: 10,
              shadowOffsetX: 1,
              shadowOffsetY: 1
            }
          },
          smooth: true
        });
        this.legendData.push(item.name);
      });
      const myChart = this.$echarts.init(document.getElementById(this.id), 'shine');
      const option = {
        color: ['#FE8D19', '#1286FD'],
        legend: {
          data: this.legendData,
          itemHeight: 10,
          itemWidth: 20,
          textStyle: {
            color: '#888888',
            fontSize: 11
          }
        },
        grid: {
          top: 30,
          left: '16%',
          height: 190
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: this.lineChartColumn,
          axisLine: {
            show: false,
            lineStyle: {
              color: '#E8E8E8',
              width: 1
            }
          },
          axisTick: {
            show: true,
            inside: true
          },
          axisLabel: {
            color: '#888888',
            fontSize: 11,
            margin: 15,
            interval: 0,
            formatter: function(value, index) {
              if (index % 2 !== 0) {
                return value;
              }
              return '';
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false,
            lineStyle: {
              color: '#E8E8E8',
              width: 1
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#888888',
            fontSize: 11
          }
        },
        series: this.series
      };
      if (!rt.isNullOrWhiteSpace(this.toolTipFormatter)) {
        option.tooltip.formatter = this.toolTipFormatter;
      }
      myChart.clear();
      myChart.setOption(option);
    }
  }
};
</script>

<style scoped>
.chart {
  width: 100%;
  height: 250px;
}
</style>
