<!--各类设备统计柱状图-->
<template>
  <ve-bar :data="chartData" :grid="grid" :title="title" :extend="extend" :settings="chartSetting" />
</template>

<script>
// 引入视觉映射组件
import 'echarts/lib/component/visualMap'
import { deviceStaticsByType } from '@/api/data/dataStatics'

export default {
  name: 'DeviceCountByTypeBar',
  props: {
    chartTitle: {
      type: String,
      default: '设备在线情况统计'
    }
  },
  data() {
    return {
      title: {
        text: '设备数量统计'
      },
      extend: {
        series: {
          label: { show: true, position: 'right' }
        }
      },
      grid: {
        right: 60
      },
      chartSetting: {
        itemStyle: {
          'barCategoryGap': 5
        },
        barWidth: 15,
        labelMap: {
          'deviceType': '设备类型',
          'deviceCount': '设备数量'
        },
        dimension: ['deviceType'],
        metrics: ['deviceCount']
      },
      chartData: {
        columns: ['deviceType', 'deviceCount'],
        rows: []
      }
    }
  },
  mounted() {
    this.title = {
      text: this.chartTitle
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      deviceStaticsByType().then(response => {
        this.chartData.rows = response.data
        const maxValue = Math.max.apply(Math, response.data.map(function(item) {
          return parseInt(item.deviceCount)
        }))
        if (maxValue < 10) {
          this.extend.xAxis = { type: 'value', minInterval: 1, max: 10 }
        } else {
          this.extend.xAxis = { type: 'value', minInterval: 1 }
        }
      })
    }
  }
}
</script>
