<template>
  <div :class="className" style="height: 300px" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
  name: 'HotDataTrend',
  props: {
    className: {
      type: String,
      default: 'chart'
    }
  },
  data() {
    return {
      chart: null,
      option: null,
      color: ['#92CAD6', '#FDDD8D', '#D7D7A2', '#F99C6C', '#E1E6EB', '#988C7D', '#FEF5BC', '#D2687B', '#BEF1E6', '#D8CEAC']
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.option = {
        grid: {
          top: '2%',
          left: '0%',
          right: '3%',
          bottom: 70,
          containLabel: true
        },
        legend: {
          data: [],
          bottom: 15,
          textStyle: {
            color: this.$store.state.settings.darkTheme ? '#fff' : '#999'
          }
        },
        tooltip: {
          trigger: 'axis',
          confine: true
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: this.$store.state.settings.darkTheme ? '#fff' : '#90979c'
            }
          },
          axisLabel: { color: this.$store.state.settings.darkTheme ? '#fff' : '#000' },
          boundaryGap: false
        },
        yAxis: [{
          type: 'value',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          },
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: {
            color: this.$store.state.settings.darkTheme ? '#fff' : '#000',
            interval: 0
          },
          splitArea: { show: false }
        }],
        series: []
      }
    },
    setData(chartData) {
      if (!chartData || !chartData.records || chartData.records.length === 0) {
        if (this.chart) {
          this.chart.clear()
        }
        return
      }
      this.option.xAxis.data = chartData.dates
      var chartDataNum = chartData.records.length - 1
      this.option.legend.data = []
      this.option.series = []
      chartData.records.forEach((item, index) => {
        this.option.legend.data.push(item.resourceName)
        this.option.series.push({
          name: item.resourceName,
          data: item.counts,
          type: 'line',
          stack: 'total',
          color: this.color[chartDataNum - index],
          areaStyle: {
            color: this.color[chartDataNum - index],
            opacity: 1
          },
          smooth: false,
          showSymbol: false
        })
      })
      this.chart.setOption(this.option, true)
    }
  }
}
</script>

<style scoped>

</style>
