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

<script>

import 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: '1400px'
    },
    height: {
      type: String,
      default: '500px'
    },
    title: {
      type: String,
      default: ''
    },
    desc: {
      type: String,
      default: ''
    },
    showTitle: {
      type: String,
      default: ''
    },
    seriesData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  computed: {
    legendData() {
      const data = Object.assign([], this.seriesData)
      const out = []
      data.map(item => {
        out.push(item.name)
      })
      return out
    },
    valueData() {
      const data = Object.assign([], this.seriesData)
      const out = []
      data.map(item => {
        out.push(item.total)
      })
      return out
    }
  },
  watch: {
    seriesData: {
      handler(newVal, oldVal) {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.clear()
      this.chart.setOption({
        title: {
          text: this.title,
          subtext: this.desc
        },
        tooltip: {
          trigger: 'axis'
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: this.legendData,
            axisLabel: {
              interval: 0,
              formatter: function(value) {
                var ret = ''
                var maxLength = 4
                var valLength = value.length
                var rowN = Math.ceil(valLength / maxLength)
                if (rowN > 1) {
                  for (var i = 0; i < rowN; i++) {
                    var temp = ''
                    var start = i * maxLength
                    var end = start + maxLength
                    temp = value.substring(start, end) + '\n'
                    ret += temp
                  }
                  return ret
                } else {
                  return value
                }
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: this.showTitle,
            type: 'bar',
            data: this.valueData,
            label: {
              show: true,
              position: 'inside'
            }
          }
        ]
      })
      window.onresize = this.chart.resize
    }
  }
}
</script>

