import * as echarts from 'echarts'
export const commonMixin = {
  methods: {}
}

export const echartMixin = {
  data() {
    return {
      chartObj: null
    }
  },
  mounted() {
    this.chartObj = echarts.init(this.$refs.chart, 'dark')
    window.addEventListener('resize', this.resizeChart)
    this.$nextTick(() => this.initChart())
  },
  beforeDestroy() {
    if (this.chartObj) {
      this.chartObj.dispose() // 销毁图表实例
    }
    window.removeEventListener('resize', this.resizeChart)
  },
  methods: {
    resizeChart() {
      if (this.chartObj) {
        this.chartObj.resize() // 调整图表大小
      }
    }
  },
  watch: {
    optionsData() {
      if (this.chartObj) {
        this.chartObj.dispose()
        this.chartObj = null
      }
      this.chartObj = echarts.init(this.$refs.chart, 'dark')
      this.initChart()
      setTimeout(() => window.dispatchEvent(new Event('resize')), 200)
    }
  }
}
