<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// NOTE: hht 22.1.27 改造成通用组件
// https://echarts.apache.org/examples/zh/
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'mx-chart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    options: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    options: function(opt) {
      if (!this.chart || !opt) return
      console.log('mx-chart set option', this.options)
      this.chart.setOption(opt, true) // true: notMerge
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      if (!this.options) return
      console.log('mx-chart set option', this.options)
      this.chart.setOption(this.options)
    }
  }
}
</script>

<style scoped>

</style>
