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

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    series: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      const data = [this.series]
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{b}'
        },
        series: [{
          type: 'tree',
          name: '治安管理工作',
          data,
          top: '1%',
          right: '50%',
          symbolSize: 1,
          initialTreeDepth: 10,
          roma: true,
          label: {
            normal: {
              position: 'center',
              verticalAlign: 'middle',
              align: 'left',
              backgroundColor: '#409EFF',
              color: '#fff',
              padding: 3,
              formatter: [
                '{box|{b}}'
              ].join('\n'),
              rich: {
                box: {
                  height: 10,
                  color: '#fff',
                  padding: [5, 5],
                  align: 'center'
                }
              }
            }
          }
        }]
      }
      this.chart.setOption(option, true)
    }
  }
}
</script>
