<template>
    <div :class="className" :style="{height:height,width:width}" />
  </template>
  
  <script>
  import * as echarts from 'echarts';
  require('echarts/theme/macarons')  
  
  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      opt:{
        type:Object,
        default:()=>{}
      },
      chgOpt:{
        type:Function,
        default:null
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(()=>{
        this.chart = echarts.init(this.$el, 'macarons')
      });
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {       
        if(this.chgOpt){
            this.chart.setOption(this.chgOpt(this.opt||{}))
        }else{
            this.chart.setOption(this.opt||{})
        }        
      }
    }
  }
  </script>
  