<template>
  <div ref="chartContainer"></div>
</template>
 
<script>
import * as echarts from 'echarts';
 
export default {
  name: 'Chart',
  props: {
    options: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      chartInstance: null
    };
  },
  watch: {
    options: {
      handler(newOption) {
        if (this.chartInstance) {
          this.chartInstance.setOption(newOption);
        }
      },
      deep: true
    }
  },
  mounted() {
    this.chartInstance = echarts.init(this.$refs.chartContainer);
    this.chartInstance.setOption(this.options);
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    if (this.chartInstance) {
      this.chartInstance.dispose();
    }
  },
  methods: {
    handleResize() {
      if (this.chartInstance) {
        this.chartInstance.resize();
      }
    }
  }
};
</script>