<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 '@/views/dashboard/mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default() {
        return {}
      }
    },
  },
  
  data() {
    return {
      chart: null
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },

  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },

  watch: {
    // 监听 chartData 的变化
    chartData: {
      handler(newData) {
        if (this.chart) {
          this.chart.setOption(newData) // 更新图表数据
        }
      },
      immediate: true  // 确保在初始化时也能渲染一次
    }
  },

  methods: {
    initChart() {
      // 初始化图表实例
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(this.chartData) // 设置图表的初始配置
    }
  }
}
</script>
