<template>
  <div>
    <div class="chart-title">用例-缺陷数量直方图</div>
    <div id="testCaseCountChart" :class="className" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '380px'
    },
    counts: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  watch: {
    counts: {
      handler (nv, ov) {
        if (nv) {
          this.YValue[0] = nv['testCaseCount']
          this.YValue[1] = nv['validTestCaseCount']
          this.YValue[2] = nv['invalidTestCaseCount']
          this.YValue[3] = nv['noExamedTestCaseCount']
          this.YValue[4] = nv['defectCount']
        }
        this.initChart()
      },
      deep: true
    }
  },
  data () {
    return {
      chart: null,
      XData: ['用例总数', '有效用例数', '无效用例数', '待审用例数', '缺陷总数'],
      YValue: [0, 0, 0, 0, 0]
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods:{
    initChart() {
      this.chart = echarts.init(document.getElementById('testCaseCountChart'), 'macarons')
      this.chart.setOption({
        // title:{
        //   text:'众测成绩分布直方图',
        //   padding:20
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          name: '严重程度',
          type: 'category',
          data: Array.from(this.XData),
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {interval: 0}
        }],
        yAxis: [{
          name: '数量',
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: Array.from(this.YValue),
          animationDuration
        }]
      })
    }
  }
}
</script>
