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
<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, Empty } from 'ant-design-vue';
 
import { getDeviceCountPieChartOptions } from '../chart-options';
 
defineOptions({ name: 'DeviceCountCard' });
 
const props = defineProps<{
  loading?: boolean;
  statsData: IotStatisticsApi.StatisticsSummaryRespVO;
}>();
 
const deviceCountChartRef = ref();
const { renderEcharts } = useEcharts(deviceCountChartRef);
 
/** 是否有数据 */
const hasData = computed(() => {
  if (!props.statsData) {
    return false;
  }
  const categories = Object.entries(
    props.statsData.productCategoryDeviceCounts || {},
  );
  return categories.length > 0 && props.statsData.deviceCount !== -1;
});
 
/** 初始化图表 */
async function initChart() {
  if (!hasData.value) {
    return;
  }
 
  await nextTick();
  const data = Object.entries(props.statsData.productCategoryDeviceCounts).map(
    ([name, value]) => ({ name, value }),
  );
  await renderEcharts(getDeviceCountPieChartOptions(data));
}
 
/** 监听数据变化 */
watch(
  () => props.statsData,
  () => {
    initChart();
  },
  { deep: true },
);
 
/** 组件挂载时初始化图表 */
onMounted(() => {
  initChart();
});
</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>
    <div v-else>
      <EchartsUI ref="deviceCountChartRef" class="h-[400px] w-full" />
    </div>
  </Card>
</template>