<template>
  <div  id="chartContent" :style="chartStyle"></div>
</template>
<script >
import * as echarts from 'echarts'
export default {
  name: 'conditionChart',
  props: {
    options: {
      type: Object,
      required: true,
      default() {
        return {};
      }
    },
    height: {
      type: String,
      default: "100%"
    },
    width: {
      type: String,
      default: "100%"
    }
  },

  data() {
    return {
      chart: null, // 这里声明了图表实例的变量

      // options: {}
    };
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById('chartContent')); // 这里创建了一个ECharts图表实例
      this.chart.setOption(this.options);
      // console.log(  this.chart)
      this.$emit('chat-init', this.chart)
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize();
      }
    },
  },
  watch: {
    options: {
      handler(newOption) {

        if (this.chart) {
          console.log("newOption ↓↓↓")
          console.log(newOption)
          this.chart.setOption(newOption, true);
        }
      },
      deep: true
    }
  },
  computed: {
    chartStyle() {
      return {
        height: this.height,
        width: this.width,
        background: "white"
      };
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart(); // 这里初始化图表
      window.addEventListener("resize", this.resizeChart);
    });
  },
  beforeDestroy() {
    if (this.chart) {
      window.removeEventListener("resize", this.resizeChart);
      this.chart.dispose();
    }
  },
}
</script>


<style scoped lang="less">

</style>
