import * as echarts from 'echarts';
 
const ECharts = {
  install: function (Vue, options) {
    Vue.prototype.$echarts = echarts;
    Vue.component('v-chart', {
      render: function (createElement) {
        return createElement('div', this.$slots.default);
      },
      mounted: function () {
        this.chart = echarts.init(this.$el);
        this.chart.setOption(this.options);
      },
      props: {
        options: Object
      },
      watch: {
        options: {
          deep: true,
          handler: function (newVal) {
            if (this.chart) {
              this.chart.setOption(newVal, true);
            }
          }
        }
      },
      beforeDestroy: function () {
        if (this.chart) {
          this.chart.dispose();
        }
      }
    });
  }
};
 
export default ECharts;