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

<script>

  import * as echarts from 'echarts'
  import resizeMixins from '@/utils/resizeMixins'
  import 'echarts/lib/component/legendScroll'
  import 'echarts/lib/component/legend'
  import DatavChartsBuild from '@/utils/DatavChartsBuild.js'
  export default {
    name: 'echart',
    mixins: [resizeMixins],
    props: {
      className: {
        type: String,
        default: 'chart',
      },
      id: {
        type: String,
        default: 'chart',
      },
      width: {
        type: String,
        default: '100%',
      },
      height: {
        type: String,
        default: '2.5rem',
      },
      options: {
        type: Object,
        default: () => ({}),
      },
    },
    data() {
      return {
        chart: null,
      }
    },
    watch: {
      'options.option': {
        handler(option) {
          // 设置true清空echart缓存
          this.chart.setOption(option, true)
        },
        deep: true,
      },
    },
    mounted() {
      //echarts.registerTheme(tdTheme) // 覆盖默认主题
      this.initChart()
    },
    methods: {
      initChart() {
        let theme = 'default'
        if (this.options.deepColor) {
          theme = 'dark'
        }
        // 初始化echart
        this.chart = echarts.init(this.$el, theme)
        DatavChartsBuild.addDynamic(this.options.option,this)
        this.chart.setOption(this.options.option || this.options, true)
      },
    },
  }
</script>

<style>
</style>
