<template>
  <div
    ref="echarts"
    class="app-echarts"
  />
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    options: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    options: {
      immediate: true,
      handler(n, o) {
        this.setOption(n);
      },
      deep: true,
    },
  },
  mounted() {
    this.chart = echarts.init(this.$refs.echarts);
    this.setOption(this.options);
    window.onresize = () => {
      this.chart && this.chart.resize();
    };
  },
  methods: {
    setOption(options) {
      if (options && this.chart) {
        this.chart.setOption(options);
        this.chart.resize();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.app-echarts {
  width: 100%;
  height: 100%;
}
</style>
