<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from "@/views/dashboard/mixins/resize";
require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    data: {
      type: Array,
      required: true
    },
    name: {
      type: String,
      default: null
    },
    color: {
      type: String,
      default: '#165DFF'
    },
    type: {
      type: String,
      default: 'line'
    },
    yAxisMin: {
      type: String,
      default: 'dataMin'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.data)
    },
    setOptions(data) {
      let option = {
        grid: {
          top: 42,
          left: 16,
          right: 16,
          bottom: 24,
          show: false,
        },
        tooltip: {
          trigger: 'axis',
          show: true,
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          show: false,
          data: data.map(item => item.x)
        },
        yAxis: {
          show: false,
          type: 'value',
          min: this.yAxisMin,
          max: 'dataMax',
        },
        series: [{
          name: this.name,
          color: this.color,
          data: data.map(item => item.value),
          type: this.type,
          smooth: true,
          // label: {
          //   show: true,
          //   position: 'top'
          // }
          markPoint: {
            symbol: 'pin',
            symbolSize: 10,
            data: [
              { type: 'max', name: '最大值' },
              { type: 'min', name: '最小值' }
            ]
          },
          // markLine: {
          //   data: [
          //     {type: 'max', name: '最大值'},
          //     {type: 'min', name: '最小值'}
          //   ]
          // }
        }]
      };
      this.chart.setOption(option)
    }
  }
}
</script>
