Newer
Older
GDT_FRONT / src / views / popup / components / caseBar.vue
wangxitong on 11 Sep 5 KB Default Changelist
<template>
  <div style="height: 100%;width: 100%">
    <div id="case-bar" class="container"/>
    <el-tabs v-model="activeName" @tab-click="changeTime" style="display: flex;position:absolute;right: 30px;top:5px">
      <el-tab-pane label="近7天" name="week"/>
      <el-tab-pane label="近1月" name="month"/>
      <el-tab-pane label="近3月" name="3month"/>
      <el-tab-pane label="近1年" name="year"/>
    </el-tabs>
<!--    <div style="display: flex;position:absolute;right: 10px;top:6px">-->
<!--      <el-button class="casebar-btn" type="primary" plain @click="changeTime('week')">近7天</el-button>-->
<!--      <el-button class="casebar-btn" type="primary" plain @click="changeTime('month')">近1月</el-button>-->
<!--      <el-button class="casebar-btn" type="primary" plain @click="changeTime('3month')">近3月</el-button>-->
<!--      <el-button class="casebar-btn" type="primary" plain @click="changeTime('year')">近1年</el-button>-->
<!--    </div>-->
  </div>
</template>

<script>
import { casePieStatistics } from '@/api/pop'
import * as echarts from 'echarts'
import { getDayTime } from '@/utils/dateutils'
export default {
  name: 'CaseBar',
  data() {
    return {
      isShow: true,
      myChart: null,
      height: '0',
      width: '0',
      timeRange: [],
      chartSettings: {
        labelMap: { date: '时间' },
        metrics: [],
        dimension: ['date']
      },
      activeName: 'week',
      listQuery: {
        startTime: '',
        endTime: ''
      },
      extend: {
        color: ['#0071ff88', '#00afff88', '#00fdc088'],
        legend: {
          textStyle: {
            color: '#cce9ff',
            fontWeight: 'bold'
          },
          lineStyle: {
            width: 10
          },
          data: ['访客控制安防评分']
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#cce9ff'
            }
          }
        },
        grid: {
          left: '1%',
          right: '5%',
          bottom: '1%',
          top: '2%',
          containLabel: true
        },
        yAxis: {
          type: 'category',
          position: 'left',
          axisLine: {
            lineStyle: {
              color: '#cce9ff'
            }
          },
          axisLabel: {
            color: '#cce9ff'
          },
          data: ['离岗', '黑名单', '监控点离线']
        },
        xAxis: {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#cce9ff'
            }
          },
          axisLabel: {
            color: '#cce9ff'
          }
        },
        series: [
          {
            name: '安防事件频率',
            type: 'bar',
            barMaxWidth: 30,
            label: {
              color: '#cce9ff',
              show: true,
              align: 'center',
              verticalAlign: 'middle',
              position: 'right',
              distance: 10
            },
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 1, color: 'rgba(255,190,68,0.4)' },
                // { offset: 0.3, color: 'rgba(0,253,232,0.8)' },
                { offset: 0, color: '#82b4dc' }
              ])
            },
            data: [0, 2, 20]
          }
        ]
      },
      chartData: {
        columns: [],
        rows: []
      }
    }
  },
  mounted() {
    this.myChart = echarts.init(document.getElementById('case-bar'))
    this.height = document.getElementById('case-bar').clientHeight - 10 + 'px'
    this.width = document.getElementById('case-bar').clientWidth - 10 + 'px'
    this.listQuery.endTime = new Date().Format('yyyy-MM-dd hh:mm:ss')
    this.changeTime('week')
    const that = this
    window.addEventListener('resize', function() {
      that.myChart.resize()
    })
  },
  methods: {
    changeTime() {
      let startTime
      switch (this.activeName) {
        case 'year':
          startTime = getDayTime(new Date().getTime() - 24 * 365 * 60 * 60 * 1000)
          this.listQuery.startTime = startTime.Format('yyyy-MM-dd')
          break
        case 'halfyear':
          startTime = getDayTime(new Date().getTime() - 24 * 182 * 60 * 60 * 1000)
          this.listQuery.startTime = startTime.Format('yyyy-MM-dd')
          break
        case '3month':
          startTime = getDayTime(new Date().getTime() - 24 * 90 * 60 * 60 * 1000)
          this.listQuery.startTime = startTime.Format('yyyy-MM-dd')
          break
        case 'month':
          startTime = getDayTime(new Date().getTime() - 24 * 30 * 60 * 60 * 1000)
          this.listQuery.startTime = startTime.Format('yyyy-MM-dd hh:mm:ss')
          break
        case 'week':
          startTime = getDayTime(new Date().getTime() - 24 * 7 * 60 * 60 * 1000)
          this.listQuery.startTime = startTime.Format('yyyy-MM-dd hh:mm:ss')
          break
      }
      const endTime = new Date()
      this.listQuery.endTime = endTime.Format('yyyy-MM-dd hh:mm:ss')
      this.fetchData()
      this.myChart.setOption(this.extend, true)
    },
    async reload() {
      this.isShow = false
      await this.$nextTick()
      this.height = document.getElementById('case-bar').clientHeight - 10 + 'px'
      this.width = document.getElementById('case-bar').clientWidth - 10 + 'px'
      this.isShow = true
    },
    // 获取数据
    fetchData() {
      casePieStatistics(this.listQuery).then(response => {
        if (response.code === 200) {
          this.extend.series[0].data = response.data.map(item => item.value)
          this.extend.yAxis.data = response.data.map(item => item.name)
          this.myChart.setOption(this.extend, true)
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep(.el-tabs__item){
  color: #b8dffd !important;
  height: 20px !important;
  line-height: 20px !important;
}
.container{
  position:relative;
  width: 100%;
  height:100%;
}
.casebar-btn {
  height: 30px;
  padding-top: 6px;
}
</style>