Newer
Older
GDT_FRONT / src / views / popup / components / camBar.vue
wangxitong on 11 Sep 4 KB Default Changelist
<template>
  <div id="cam-bar" class="container"/>
</template>

<script>

import * as echarts from 'echarts';
import { cameraTypeStatistics } from '@/api/pop'
var myChart
export default {
  name: 'CamBar',
  data() {
    return {
      timer: null,
      height: '0',
      extend: {
        legend: {
          textStyle: {
            color: '#cce9ff',
            fontWeight: 'bold'
          },
          lineStyle: {
            width: 5
          },
          data: ['在线数量', '总数量'],
          orient: 'vertical',
          right: 10,
          top: 10
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            label: {
              backgroundColor: '#82b4dc'
            }
          }
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '1%',
          top: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            color: '#cce9ff'
          },
          axisTick: { show: false },
          axisLine: {
            lineStyle: {
              color: '#cce9ff'
            }
          },
          data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
        },
        yAxis: {
          position: 'left',
          type: 'value',
          axisLabel: {
            color: '#cce9ff'
          },
          axisLine: {
            lineStyle: {
              color: '#cce9ff'
            }
          }
        },
        series: [
          {
            label: {
              color: '#cce9ff',
              show: true,
              align: 'center',
              verticalAlign: 'middle',
              position: 'top',
              distance: 5
            },
            name: '在线数量',
            type: 'bar',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 1, color: 'rgba(195,250,235,0.8)' },
                { offset: 0.3, color: 'rgba(46,253,0,0.8)' },
                { offset: 0, color: 'rgba(12,178,0,0.8)' }
              ])
            },
            emphasis: {
              focus: 'series',
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: 'rgba(46,253,0,0.8)' },
                  { offset: 0.7, color: 'rgba(12,178,0,0.8)' },
                  { offset: 1, color: 'rgba(195,250,235,0.8)' }
                ])
              }
            },
            data: []
          },
          {
            label: {
              color: '#cce9ff',
              show: true,
              align: 'center',
              verticalAlign: 'middle',
              position: 'top',
              distance: 5
            },
            name: '总数量',
            type: 'bar',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 1, color: 'rgba(250,224,198,0.8)' },
                { offset: 0.3, color: '#ff9913cc' },
                { offset: 0, color: '#FFB005CC' }
              ])
            },
            emphasis: {
              focus: 'series',
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#ff9913cc' },
                  { offset: 0.7, color: '#FFB005CC' },
                  { offset: 1, color: 'rgba(250,224,198,0.8)' }
                ])
              }
            },
            data: []
          }
        ]
      }
    }
  },
  mounted() {
    myChart = echarts.init(document.getElementById('cam-bar'))
    // const that = this
    // this.timer = setInterval(function() {
    //   // 每次向后滚动一个,最后一个从头开始。
    //   if (that.extend.dataZoom[0].endValue === 23) {
    //     that.extend.dataZoom[0].endValue = 4
    //     that.extend.dataZoom[0].startValue = 0
    //   } else {
    //     that.extend.dataZoom[0].endValue = that.extend.dataZoom[0].endValue + 1
    //     that.extend.dataZoom[0].startValue = that.extend.dataZoom[0].startValue + 1
    //   }
    //   myChart.setOption(that.extend, true)
    // }, 2000)
    this.fetchData()
    window.addEventListener('resize', function() {
      myChart.resize()
    })
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    fetchData() {
      cameraTypeStatistics().then(response => {
        if (response.code === 200) {
          this.extend.series[0].data = response.data.map(item => item.online)
          this.extend.series[1].data = response.data.map(item => item.all)
          this.extend.xAxis.data = response.data.map(item => item.name.replace('摄像头', ''))
          console.log(this.extend)
          console.log('wxt')
          myChart.clear()
          myChart.setOption(this.extend, true)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 200px;
}
</style>