<template>
  <div 
    id="analysischartLine"
    style="width: 100%"
    :style="{ height: chartHeight + 'px' }"
  ></div>
</template>
<script>
import * as echarts from 'echarts'
import resize from '@/utils/chartMixins/resize'
export default {
  name: "LineChart",
  mixins: [resize],
  data() {
    return {
      chartDom: '',
      chart: '',
      chartHeight: this.tableHeight(214) - 70
    }
  },
  props: {
    chartData: {
      type: Array,
      required: true,
      default: () => {
        return []
      }
    }
  },
  watch: {
    chartData: function () {
      this.getChart()
    }
  },
  mounted() {
    window.addEventListener('resize', () => {
      this.chartHeight = this.tableHeight(214) - 70
    })
  },
  methods: {
    getChart() {
      if (
        this.chart !== null &&
        this.chart !== '' &&
        this.chart !== undefined
      ) {
        this.chart.dispose() // 页面多次刷新会出现警告，Dom已经初始化了一个实例，这是销毁实例
      }
      this.chartDom = document.getElementById('analysischartLine')
      this.chart = echarts.init(this.chartDom)
      let xData = []
      let yData = []
      for (let i = 0; i < this.chartData.length; i++) {
        xData.push(this.chartData[i].time)
        yData.push(this.chartData[i].useNum)
      }

      var option = {
        xAxis: {
          type: 'category',
          data: xData
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: yData,
            type: 'line'
          }
        ]
      };

      option && this.chart.setOption(option);
    }
  }
}
</script>