wwf
10 小时以前 a1d7e81859f554f3a53680cc35f0f49bf1f77098
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
  <el-card class="chart-card" shadow="never" :loading="loading">
    <template #header>
      <div class="flex items-center">
        <span class="text-base font-medium text-gray-600">设备数量统计</span>
      </div>
    </template>
    <div v-if="loading && !hasData" class="h-[240px] flex justify-center items-center">
      <el-empty description="加载中..." />
    </div>
    <div v-else-if="!hasData" class="h-[240px] flex justify-center items-center">
      <el-empty description="暂无数据" />
    </div>
    <div v-else ref="deviceCountChartRef" class="h-[240px]"></div>
  </el-card>
</template>
 
<script lang="ts" setup>
import * as echarts from 'echarts/core'
import { PieChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'
import { TooltipComponent, LegendComponent } from 'echarts/components'
import { LabelLayout } from 'echarts/features'
import { IotStatisticsSummaryRespVO } from '@/api/iot/statistics'
import type { PropType } from 'vue'
 
/** 【设备数量】统计卡片 */
defineOptions({ name: 'DeviceCountCard' })
 
const props = defineProps({
  statsData: {
    type: Object as PropType<IotStatisticsSummaryRespVO>,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  }
})
 
const deviceCountChartRef = ref()
 
/** 是否有数据 */
const hasData = computed(() => {
  if (!props.statsData) return false
 
  const categories = Object.entries(props.statsData.productCategoryDeviceCounts || {})
  return categories.length > 0 && props.statsData.deviceCount !== -1
})
 
/** 初始化图表 */
const initChart = () => {
  // 如果没有数据,则不初始化图表
  if (!hasData.value) return
  // 确保 DOM 元素存在且已渲染
  if (!deviceCountChartRef.value) {
    console.warn('图表DOM元素不存在')
    return
  }
 
  echarts.use([TooltipComponent, LegendComponent, PieChart, CanvasRenderer, LabelLayout])
  try {
    const chart = echarts.init(deviceCountChartRef.value)
    chart.setOption({
      tooltip: {
        trigger: 'item'
      },
      legend: {
        top: '5%',
        right: '10%',
        align: 'left',
        orient: 'vertical',
        icon: 'circle'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: ['50%', '80%'],
          avoidLabelOverlap: false,
          center: ['30%', '50%'],
          label: {
            show: false,
            position: 'outside'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 20,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: Object.entries(props.statsData.productCategoryDeviceCounts).map(
            ([name, value]) => ({
              name,
              value
            })
          )
        }
      ]
    })
    return chart
  } catch (error) {
    console.error('初始化图表失败:', error)
    return null
  }
}
 
/** 监听数据变化 */
watch(
  () => props.statsData,
  () => {
    // 使用 nextTick 确保 DOM 已更新
    nextTick(() => {
      initChart()
    })
  },
  { deep: true }
)
 
/** 组件挂载时初始化图表 */
onMounted(async () => {
  // 使用 nextTick 确保 DOM 已更新
  await nextTick(() => {
    initChart()
  })
})
</script>