<!--各类设备离线情况统计-->
<template>
  <ve-histogram :data="chartData" :grid="grid" :title="title" :extend="extend" :settings="chartSetting" />
</template>

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

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