<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
  
  <script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from '../../dashboard/mixins/resize'
  var chart

  export default {
    name: "ECharts",
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '400px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true,
        default: null
      }
    },
    data() {
      return {
        // chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    watch: {
      chartData: {
        handler(val) {
          if (chart) { 
            chart.dispose();
            chart = null;
          }
          this.$nextTick(() => {
            this.initChart()
            console.log("width",this.width);
            // this.setOptions(val)
          })
        },
        deep: true,
        immediate: true
      }
    },
    beforeDestroy() {
      if (chart) {
        chart.dispose()
        chart = null
      }
    },
    methods: {
      initChart() {
        if(chart != null && chart != "" && chart != undefined){
        	chart.dispose();//解决echarts dom已经加载的报错
        }
        chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.chartData)
      },
      setOptions(chartData) {
        chart.setOption(chartData)
      }
    }
  }
</script>
  