Newer
Older
securityFront / src / views / ctrl / statCharts / inOutCount.vue
wangxitong on 20 Feb 2021 5 KB bug修改
<template>
  <div style="height: 100%">
    <el-date-picker
      v-model="timeRange"
      :picker-options="pickerOptions"
      type="daterange"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      size="small"
      style="margin-bottom: 10px;z-index: 10000"/>
    <ve-line :data="chartData" :settings="settings" :extend="barExtend" style="margin-top: -50px;" height="93%"/>
  </div>
</template>

<script>
import { formatToString, parseToDate, getDate, oneDayTime } from '@/utils/calendarUtil'
import { inOutCount } from '@/api/statistics'

const now = new Date()

export default {
  name: 'InOutCount',
  data() {
    return {
      params: {
        beginDate: '',
        endDate: ''
      },
      chartData: {
        columns: ['date', 'staff', 'visitor', 'car', 'visitorCar'],
        rows: []
      },

      barExtend:{
        legend:{  // 隐藏图例
          x: 'center',
          y: 'bottom'
        },
        grid: {
          containLabel: true,
          bottom: '10%'
        },
      },
      settings: {
        labelMap: {
          'staff': '内部人员',
          'visitor': '来访人员',
          'car': '内部车辆',
          'visitorCar': '来访车辆'
        },
        // xAxisType: 'time',
        max: [4],
        area: false,
        metrics: ['staff', 'visitor', 'car', 'visitorCar'],
        dimension: ['date'],
      },
      timeRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近七日',
          onClick(picker) {
            const end = now
            const start = new Date()
            start.setTime(start.getTime() - oneDayTime * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近十日',
          onClick(picker) {
            const end = now
            const start = new Date()
            start.setTime(start.getTime() - oneDayTime * 10)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三十日',
          onClick(picker) {
            const end = now
            const start = new Date()
            start.setTime(start.getTime() - oneDayTime * 30)
            picker.$emit('pick', [start, end])
          }
        }],
        disabledDate(time) {
          const oneMonthLater = new Date(now.getTime() - (oneDayTime * 31))
          return time.getTime() < oneMonthLater
        }
      }
    }
  },
  watch: {
    timeRange: function(newV) {
      if (newV) {
        this.params.beginDate = formatToString(newV[0], 'DATE')
        this.params.endDate = formatToString(newV[1], 'DATE')
      } else {
        this.initTime()
      }
      this.fetchData()
    }
  },
  created() {
    this.initTime()
  },
  activated() {
    this.fetchData()
  },
  methods: {
    initTime() {
      this.params.beginDate = getDate(-6, 'DATE')
      this.params.endDate = getDate(0, 'DATE')

      this.timeRange = [this.params.beginDate, this.params.endDate]
    },
    fetchData() {
      inOutCount(this.params).then(response => {
        if (response.code === 200) {
          const data = this.parseStatisticsData(response.data)
          this.chartData.rows = data.rowDatas
          this.settings.max = [data.maxCount]
        }
      })
    },
    parseStatisticsData(obj) {
      const staffList = obj.person
      const visitorList = obj.visitorPerson
      const carList = obj.car
      const visitorCarList = obj.visitorCar

      const rowDatas = [] // 返回的数据
      let maxCount = 4 // 返回的最大的计数值

      let start = parseToDate(this.params.beginDate, 'DATE').valueOf() // 开始日期的Date对象
      const end = parseToDate(this.params.endDate, 'DATE').valueOf() // 结束日期的Date对象

      let indexS = 0 // 内部人员索引
      let indexV = 0 // 访客索引
      let indexC = 0 // 内部车辆索引
      let indexVc = 0 // 访客车辆索引

      // 遍历时间
      while (start < end) {
        const staff = staffList[indexS]
        const visitor = visitorList[indexV]
        const car = carList[indexC]
        const visitorCar = visitorCarList[indexVc]

        let staffCount = 0
        let visitorCount = 0
        let carCount = 0
        let visitorCarCount = 0

        const date = formatToString(start, 'DATE')

        if (typeof staff !== 'undefined') {
          if (staff.date === date) {
            staffCount = staff.count
            indexS++
          }
        }

        if (typeof visitor !== 'undefined') {
          if (visitor.date === date) {
            visitorCount = visitor.count
            indexV++
          }
        }

        if (typeof car !== 'undefined') {
          if (car.date === date) {
            carCount = car.count
            indexC++
          }
        }

        if (typeof visitorCar !== 'undefined') {
          if (visitorCar.date === date) {
            visitorCarCount = visitorCar.count
            indexVc++
          }
        }

        if (staffCount + visitorCount + carCount + visitorCarCount > 0) {
          rowDatas.push({ date: date, staff: staffCount, visitor: visitorCount, car: carCount, visitorCar: visitorCarCount })
        }
        if(maxCount<staffCount){
          maxCount = staffCount
        }
        if(maxCount<visitorCount){
          maxCount = visitorCount
        }
        if(maxCount<carCount){
          maxCount = carCount
        }
        if(maxCount<visitorCarCount){
          maxCount = visitorCarCount
        }
        start += oneDayTime
      }

      // 设置最大值用以不显示小数
      maxCount = Math.ceil(maxCount * 1.1)

      // 返回生成好的图表数据
      return {
        rowDatas: rowDatas,
        maxCount: maxCount
      }
    }
  }
}
</script>

<style scoped>

</style>