<template> <div style="height: 100%"> <el-date-picker v-model="timeRange" :picker-options="pickerOptions" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="margin-bottom: 10px;z-index: 10000" size="small"/> <ve-histogram :data="chartData" :settings="settings" :extend="chartExtend" style="margin-top: -50px"height="93%"/> </div> </template> <script> import { formatToString, getDate, oneDayTime } from '@/utils/calendarUtil' import { inOutDoorCount } from '@/api/statistics' const now = new Date() export default { name: 'InOutDoorCount', data() { return { params: { beginDate: '', endDate: '' }, chartExtend: { legend:{ // 隐藏图例 x: 'center', y: 'bottom' }, grid: { containLabel: true, bottom: '10%' }, series: { barMaxWidth: 50, center: ['50%', '50%'] } }, chartData: { columns: ['name', 'person', 'car'], rows: [] }, settings: { radius: [60, 100], labelMap: { 'person': '人员', 'car': '车辆' }, max: [4], metrics: ['person', 'car'], dimension: ['name'] }, 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() { inOutDoorCount(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 personList = obj.person const carList = obj.car const rowDatas = [] // 返回的数据 let maxCount = 4 // 返回的最大的计数值 let indexS = 0 let indexV = 0 // 遍历内部人员(车辆)和来访人员(车辆),生成图表所需要的数据格式 while (indexS < personList.length || indexV < carList.length) { let person = {} let car = {} // 获取内部人员(车辆)对象 if (indexS < personList.length) { person = personList[indexS] } else { person = {} } // 获取来访人员(车辆)对象 if (indexV < carList.length) { car = carList[indexV] } else { car = {} } // 如果内部人员(车辆)的队列已经取完则直接添加来访人员(车辆) if (typeof person.name === 'undefined') { rowDatas.push({ name: person.name, car: car.count }) indexV++ maxCount = Math.max(maxCount, car.count) continue } // 如果来访人员(车辆)的队列已经取完则直接添加内部人员(车辆) if (typeof person.name === 'undefined') { rowDatas.push({ name: person.name, person: person.count }) indexS++ maxCount = Math.max(maxCount, person.count) continue } // 队列都还没有处理完成则比较日期的大小 const carVal = car.name const personVal = person.name if (personVal < carVal) { // 人员列表的日期比较小 rowDatas.push({ name: person.name, person: person.count }) indexS++ maxCount = Math.max(maxCount, person.count) } else if (personVal > carVal) { // 访客列表的日期比较小 rowDatas.push({ name: car.name, car: car.count }) indexV++ maxCount = Math.max(maxCount, car.count) } else { rowDatas.push({ name: person.name, person: person.count, car: car.count }) indexS++ indexV++ maxCount = Math.max(maxCount, person.count, car.count) } } // 设置最大值用以不显示小数 maxCount = Math.ceil(maxCount * 1.1) // 返回生成好的图表数据 return { rowDatas: rowDatas, maxCount: maxCount } } } } </script> <style scoped> </style>