gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<script lang="ts" setup>
import type { IotStatisticsApi } from '#/api/iot/statistics';
 
import { computed, nextTick, onMounted, ref, watch } from 'vue';
 
import { EchartsUI, useEcharts } from '..\..\..\..\packages\effects\plugins\src\echarts';
 
import { Card, Col, Empty, Row } from 'ant-design-vue';
 
import { getDeviceStateGaugeChartOptions } from '../chart-options';
 
defineOptions({ name: 'DeviceStateCountCard' });
 
const props = defineProps<{
  loading?: boolean;
  statsData: IotStatisticsApi.StatisticsSummaryRespVO;
}>();
 
const deviceOnlineChartRef = ref();
const deviceOfflineChartRef = ref();
const deviceInactiveChartRef = ref();
 
const { renderEcharts: renderOnlineChart } = useEcharts(deviceOnlineChartRef);
const { renderEcharts: renderOfflineChart } = useEcharts(deviceOfflineChartRef);
const { renderEcharts: renderInactiveChart } = useEcharts(
  deviceInactiveChartRef,
);
 
/** 是否有数据 */
const hasData = computed(() => {
  if (!props.statsData) return false;
  return props.statsData.deviceCount !== -1;
});
 
/** 初始化图表 */
async function initCharts() {
  if (!hasData.value) {
    return;
  }
 
  await nextTick();
  const max = props.statsData.deviceCount || 100;
  // 在线设备
  await renderOnlineChart(
    getDeviceStateGaugeChartOptions(
      props.statsData.deviceOnlineCount,
      max,
      '#52c41a',
      '在线设备',
    ),
  );
  // 离线设备
  await renderOfflineChart(
    getDeviceStateGaugeChartOptions(
      props.statsData.deviceOfflineCount,
      max,
      '#ff4d4f',
      '离线设备',
    ),
  );
  // 待激活设备
  await renderInactiveChart(
    getDeviceStateGaugeChartOptions(
      props.statsData.deviceInactiveCount,
      max,
      '#1890ff',
      '待激活设备',
    ),
  );
}
 
/** 监听数据变化 */
watch(
  () => props.statsData,
  () => {
    initCharts();
  },
  { deep: true },
);
 
/** 组件挂载时初始化图表 */
onMounted(() => {
  initCharts();
});
</script>
 
<template>
  <Card title="设备状态统计" :loading="loading" class="h-full">
    <div
      v-if="loading && !hasData"
      class="flex h-[300px] items-center justify-center"
    >
      <Empty description="加载中..." />
    </div>
    <div
      v-else-if="!hasData"
      class="flex h-[300px] items-center justify-center"
    >
      <Empty description="暂无数据" />
    </div>
    <Row v-else class="h-[280px]">
      <Col :span="8" class="flex items-center justify-center">
        <EchartsUI ref="deviceOnlineChartRef" class="h-[250px] w-full" />
      </Col>
      <Col :span="8" class="flex items-center justify-center">
        <EchartsUI ref="deviceOfflineChartRef" class="h-[250px] w-full" />
      </Col>
      <Col :span="8" class="flex items-center justify-center">
        <EchartsUI ref="deviceInactiveChartRef" class="h-[250px] w-full" />
      </Col>
    </Row>
  </Card>
</template>