DeviceStateCountCard.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <el-card class="chart-card" shadow="never" :loading="loading">
  3. <template #header>
  4. <div class="flex items-center">
  5. <span class="text-base font-medium text-gray-600">设备状态统计</span>
  6. </div>
  7. </template>
  8. <div v-if="loading && !hasData" class="h-[240px] flex justify-center items-center">
  9. <el-empty description="加载中..." />
  10. </div>
  11. <div v-else-if="!hasData" class="h-[240px] flex justify-center items-center">
  12. <el-empty description="暂无数据" />
  13. </div>
  14. <el-row v-else class="h-[240px]">
  15. <el-col :span="8" class="flex flex-col items-center">
  16. <div ref="deviceOnlineCountChartRef" class="h-[160px] w-full"></div>
  17. <div class="text-center mt-2">
  18. <span class="text-sm text-gray-600">在线设备</span>
  19. </div>
  20. </el-col>
  21. <el-col :span="8" class="flex flex-col items-center">
  22. <div ref="deviceOfflineChartRef" class="h-[160px] w-full"></div>
  23. <div class="text-center mt-2">
  24. <span class="text-sm text-gray-600">离线设备</span>
  25. </div>
  26. </el-col>
  27. <el-col :span="8" class="flex flex-col items-center">
  28. <div ref="deviceActiveChartRef" class="h-[160px] w-full"></div>
  29. <div class="text-center mt-2">
  30. <span class="text-sm text-gray-600">待激活设备</span>
  31. </div>
  32. </el-col>
  33. </el-row>
  34. </el-card>
  35. </template>
  36. <script lang="ts" setup>
  37. import * as echarts from 'echarts/core'
  38. import { GaugeChart } from 'echarts/charts'
  39. import { CanvasRenderer } from 'echarts/renderers'
  40. import { IotStatisticsSummaryRespVO } from '@/api/iot/statistics'
  41. import type { PropType } from 'vue'
  42. /** 【设备状态】统计卡片 */
  43. defineOptions({ name: 'DeviceStateCountCard' })
  44. const props = defineProps({
  45. statsData: {
  46. type: Object as PropType<IotStatisticsSummaryRespVO>,
  47. required: true
  48. },
  49. loading: {
  50. type: Boolean,
  51. default: false
  52. }
  53. })
  54. const deviceOnlineCountChartRef = ref()
  55. const deviceOfflineChartRef = ref()
  56. const deviceActiveChartRef = ref()
  57. /** 是否有数据 */
  58. const hasData = computed(() => {
  59. if (!props.statsData) return false
  60. return props.statsData.deviceCount !== -1
  61. })
  62. /** 初始化仪表盘图表 */
  63. const initGaugeChart = (el: any, value: number, color: string) => {
  64. // 确保 DOM 元素存在且已渲染
  65. if (!el) {
  66. console.warn('图表DOM元素不存在')
  67. return
  68. }
  69. echarts.use([GaugeChart, CanvasRenderer])
  70. try {
  71. const chart = echarts.init(el)
  72. chart.setOption({
  73. series: [
  74. {
  75. type: 'gauge',
  76. startAngle: 360,
  77. endAngle: 0,
  78. min: 0,
  79. max: props.statsData.deviceCount || 100, // 使用设备总数作为最大值
  80. progress: {
  81. show: true,
  82. width: 12,
  83. itemStyle: {
  84. color: color
  85. }
  86. },
  87. axisLine: {
  88. lineStyle: {
  89. width: 12,
  90. color: [[1, '#E5E7EB']]
  91. }
  92. },
  93. axisTick: { show: false },
  94. splitLine: { show: false },
  95. axisLabel: { show: false },
  96. pointer: { show: false },
  97. anchor: { show: false },
  98. title: { show: false },
  99. detail: {
  100. valueAnimation: true,
  101. fontSize: 24,
  102. fontWeight: 'bold',
  103. fontFamily: 'Inter, sans-serif',
  104. color: color,
  105. offsetCenter: [0, '0'],
  106. formatter: (value: number) => {
  107. return `${value} 个`
  108. }
  109. },
  110. data: [{ value: value }]
  111. }
  112. ]
  113. })
  114. return chart
  115. } catch (error) {
  116. console.error('初始化图表失败:', error)
  117. return null
  118. }
  119. }
  120. /** 初始化所有图表 */
  121. const initCharts = () => {
  122. // 如果没有数据,则不初始化图表
  123. if (!hasData.value) return
  124. // 使用 nextTick 确保 DOM 已更新
  125. nextTick(() => {
  126. // 在线设备统计
  127. if (deviceOnlineCountChartRef.value) {
  128. initGaugeChart(deviceOnlineCountChartRef.value, props.statsData.deviceOnlineCount, '#0d9')
  129. }
  130. // 离线设备统计
  131. if (deviceOfflineChartRef.value) {
  132. initGaugeChart(deviceOfflineChartRef.value, props.statsData.deviceOfflineCount, '#f50')
  133. }
  134. // 待激活设备统计
  135. if (deviceActiveChartRef.value) {
  136. initGaugeChart(deviceActiveChartRef.value, props.statsData.deviceInactiveCount, '#05b')
  137. }
  138. })
  139. }
  140. /** 监听数据变化 */
  141. watch(
  142. () => props.statsData,
  143. () => {
  144. initCharts()
  145. },
  146. { deep: true }
  147. )
  148. /** 组件挂载时初始化图表 */
  149. onMounted(() => {
  150. initCharts()
  151. })
  152. </script>