<template>
  <div :id="id" :style="{height:height,width:width}"/>
</template>
<script>
import * as echarts from 'echarts'
export default {
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    smooth: {
      default: true
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    list: {
      default() {
        return []
      }
    }
  },
  data () {
    return {
      chart: null,
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOption() {
      this.chart.setOption({
        grid: {
          top: '0',
          left: '0',
          bottom: '0',
          right: '0'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          show: false,
        },
        yAxis: {
          animation: false,
          scale: true,
          show: false,
          type: 'value',
          splitNumber: 1,
          // min: function (value) {
          //   return value.min - (value.max-value.min);
          // },
          // max: function (value) {
          //   return value.max + (value.max-value.min);
          // }
        },
        tooltip: {
          show: false,
        },
        series: [
          {
            data: this.list,
            animation: false,
            type: 'line',
            showSymbol: false,
            smooth: this.smooth,
            lineStyle: {
              // color: this.info.spread < 0 ? '#53B949' :'#D54037'
              color: '#0F8DDF'
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  // color: this.info.spread < 0 ? 'rgba(83, 185, 73, 0.6)' : 'rgba(213, 64, 55, 0.6)'
                  color: 'rgba(15, 141, 223, 0.8)'
                },
                {
                  offset: 1,
                  // color: this.info.spread < 0 ? 'rgba(83, 185, 73, 0.1)' : 'rgba(213, 64, 55, 0.1)'
                  color:'rgba(255,255,255,0.3)'
                }
              ])
            }
          }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.setOption()
    },
  }
}
</script>