<template>
  <div ref="dom" class="charts chart-bar"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'MyChartBar',
  props: {
    run: String,
    value: Object,
    text: String,
    subtext: String
  },
  data () {
    return {
      dom: null
    }
  },
  methods: {
    resize () {
      this.dom.resize()
    }
  },
  watch: {
    run (ret) {
      this.$nextTick(() => {
        let option = {
          title: {
            text: this.text,
            subtext: this.subtext,
            x: 'left'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: this.value.legend,
            x: 'right'
          },
          xAxis: {
            type: 'category',
            data: this.value.xAxis,
            axisLabel: {
              interval: 0
            }
          },
          yAxis: {
            type: 'value'
          },
          series: this.value.seriesData
        }
        this.dom = echarts.init(this.$refs.dom, 'tdTheme')
        this.dom.setOption(option, true)
        on(window, 'resize', this.resize)
      })
    }
  },
  mounted () {
    // console.log(this.value, '图标组件获取的值')
    this.$nextTick(() => {
      let option = {
        title: {
          text: this.text,
          subtext: this.subtext,
          x: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: this.value.legend,
          x: 'right'
        },
        xAxis: {
          type: 'category',
          data: this.value.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: this.value.seriesData
      }
      this.dom = echarts.init(this.$refs.dom, 'tdTheme')
      this.dom.setOption(option)
      on(window, 'resize', this.resize)
    })
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
