MixChart.vue 5.67 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7
<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts';
import resize from './mixins/resize';
冯超鹏's avatar
冯超鹏 committed
8
import { getcount } from '@/api/homepage';
冯超鹏's avatar
冯超鹏 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    id: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '200px',
    },
    height: {
      type: String,
      default: '200px',
    },
  },
  data() {
    return {
      chart: null,
冯超鹏's avatar
冯超鹏 committed
32 33 34
      devicemover: [], // 设备数量
      policedata: [], // 报警数量
      userdata: [], // 用户数量
冯超鹏's avatar
冯超鹏 committed
35 36
    };
  },
冯超鹏's avatar
冯超鹏 committed
37 38
  created() {
    this.getcount();
冯超鹏's avatar
冯超鹏 committed
39 40 41 42 43 44 45 46 47 48 49 50 51
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      const xData = (function() {
        const data = [];
冯超鹏's avatar
冯超鹏 committed
52 53 54
        const date = new Date();
        for (let i = 1; i < date.getMonth() + 1 + 1; i++) {
          data.push(i + '月');
冯超鹏's avatar
冯超鹏 committed
55 56 57 58 59 60
        }
        return data;
      }());
      this.chart.setOption({
        backgroundColor: '#344b58',
        title: {
冯超鹏's avatar
冯超鹏 committed
61
          text: '统计记录',
冯超鹏's avatar
冯超鹏 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
          x: '20',
          top: '20',
          textStyle: {
            color: '#fff',
            fontSize: '22',
          },
          subtextStyle: {
            color: '#90979c',
            fontSize: '16',
          },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff',
            },
          },
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 150,
          bottom: 95,
          textStyle: {
            color: '#fff',
          },
        },
        legend: {
          x: '5%',
          top: '10%',
          textStyle: {
            color: '#90979c',
          },
冯超鹏's avatar
冯超鹏 committed
97
          data: ['设备数量', '用户数量', '设备报警次数'],
冯超鹏's avatar
冯超鹏 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#90979c',
            },
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitArea: {
            show: false,
          },
          axisLabel: {
            interval: 0,
          },
          data: xData,
        }],
        yAxis: [{
          type: 'value',
          splitLine: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: '#90979c',
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            interval: 0,
          },
          splitArea: {
            show: false,
          },
        }],
        dataZoom: [{
          show: true,
          height: 30,
          xAxisIndex: [
            0,
          ],
          bottom: 30,
          start: 10,
          end: 80,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: '#d3dee5',
          },
          textStyle: {
            color: '#fff' },
          borderColor: '#90979c',
        }, {
          type: 'inside',
          show: true,
          height: 15,
          start: 1,
          end: 35,
        }],
        series: [
          {
冯超鹏's avatar
冯超鹏 committed
167
            name: '设备数量',
冯超鹏's avatar
冯超鹏 committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            type: 'bar',
            stack: 'total',
            barMaxWidth: 35,
            barGap: '10%',
            itemStyle: {
              normal: {
                color: 'rgba(255,144,128,1)',
                label: {
                  show: true,
                  textStyle: {
                    color: '#fff',
                  },
                  position: 'insideTop',
                  formatter(p) {
                    return p.value > 0 ? p.value : '';
                  },
                },
              },
            },
冯超鹏's avatar
冯超鹏 committed
187
            data: this.devicemover,
冯超鹏's avatar
冯超鹏 committed
188 189
          },
          {
冯超鹏's avatar
冯超鹏 committed
190
            name: '用户数量',
冯超鹏's avatar
冯超鹏 committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            type: 'bar',
            stack: 'total',
            itemStyle: {
              normal: {
                color: 'rgba(0,191,183,1)',
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  formatter(p) {
                    return p.value > 0 ? p.value : '';
                  },
                },
              },
            },
冯超鹏's avatar
冯超鹏 committed
206
            data: this.userdata,
冯超鹏's avatar
冯超鹏 committed
207
          }, {
冯超鹏's avatar
冯超鹏 committed
208
            name: '设备报警次数',
冯超鹏's avatar
冯超鹏 committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            type: 'line',
            stack: 'total',
            symbolSize: 10,
            symbol: 'circle',
            itemStyle: {
              normal: {
                color: 'rgba(252,230,48,1)',
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  formatter(p) {
                    return p.value > 0 ? p.value : '';
                  },
                },
              },
            },
冯超鹏's avatar
冯超鹏 committed
226
            data: this.policedata,
冯超鹏's avatar
冯超鹏 committed
227 228 229 230
          },
        ],
      });
    },
冯超鹏's avatar
冯超鹏 committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244
    getcount() {
      getcount()
        .then(response => {
          if (response.code === 200) {
            this.devicemover = response.data.devicedata;
            this.policedata = response.data.devicepolice;
            this.userdata = response.data.policedata;
            this.initChart();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
冯超鹏's avatar
冯超鹏 committed
245 246 247
  },
};
</script>