import * as echarts from 'echarts';

export default {
  data() {
    return {
      chartInstance: null,
      timer: null,
      interval: 5000,
    };
  },
  mounted() {
    if(this.refresh !== 'custom') {
      this.timer = setInterval(() => {
        this.init();
      }, this.interval);
    }
    setTimeout(() => {
      this.init();
    }, 3000)
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    init() {
      if (!this.chartInstance) {
        this.initCharts();
        if (!this.chartInstance) return;
      }
      this.chartInstance.clear();
      this.chartInstance.setOption(this.getOption());
      this.finished && this.finished();
    },
    initCharts() {
      const el = this.$refs.chartRef;
      if (!el) {
        return;
      }
      this.chartInstance = this.$echarts.init(el);
    },
  },
};
