wwf
7 小时以前 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
  <el-card class="chart-card" shadow="never" :loading="loading">
    <template #header>
      <div class="flex items-center justify-between">
        <span class="text-base font-medium text-gray-600">消息量统计</span>
        <div class="flex flex-wrap items-center gap-4">
          <el-form-item label="时间范围" class="!mb-0">
            <el-date-picker
              v-model="queryParams.times"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              :shortcuts="defaultShortcuts"
              class="!w-240px"
              end-placeholder="结束日期"
              start-placeholder="开始日期"
              type="daterange"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleQuery"
            />
          </el-form-item>
          <el-form-item label="时间间隔" class="!mb-0">
            <el-select
              v-model="queryParams.interval"
              class="!w-120px"
              placeholder="间隔类型"
              @change="handleQuery"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.DATE_INTERVAL)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </div>
      </div>
    </template>
    <div v-if="loading && !hasData" class="h-[300px] flex justify-center items-center">
      <el-empty description="加载中..." />
    </div>
    <div v-else-if="!hasData" class="h-[300px] flex justify-center items-center">
      <el-empty description="暂无数据" />
    </div>
    <div v-else ref="messageChartRef" class="h-[300px]"></div>
  </el-card>
</template>
 
<script lang="ts" setup>
import * as echarts from 'echarts/core'
import { LineChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { UniversalTransition } from 'echarts/features'
import {
  StatisticsApi,
  IotStatisticsDeviceMessageSummaryByDateRespVO,
  IotStatisticsDeviceMessageReqVO
} from '@/api/iot/statistics'
import { formatDate, beginOfDay, endOfDay, defaultShortcuts } from '@/utils/formatTime'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
 
/** 消息趋势统计卡片 */
defineOptions({ name: 'MessageTrendCard' })
 
const messageChartRef = ref()
const loading = ref(false)
const messageData = ref<IotStatisticsDeviceMessageSummaryByDateRespVO[]>([])
 
const queryParams = reactive<IotStatisticsDeviceMessageReqVO>({
  interval: 1, // DAY, 日
  times: [
    // 默认显示最近一周的数据
    formatDate(beginOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24 * 7))),
    formatDate(endOfDay(new Date(new Date().getTime() - 3600 * 1000 * 24)))
  ]
}) // 查询参数
 
// 是否有数据
const hasData = computed(() => {
  return messageData.value && messageData.value.length > 0
})
 
// 处理查询操作
const handleQuery = () => {
  fetchMessageData()
}
 
// 获取消息统计数据
const fetchMessageData = async () => {
  loading.value = true
  try {
    messageData.value = await StatisticsApi.getDeviceMessageSummaryByDate(queryParams)
 
    // 使用 nextTick 确保数据更新后重新渲染图表
    await nextTick()
    initChart()
  } catch (error) {
    console.error('获取消息统计数据失败:', error)
    messageData.value = []
  } finally {
    loading.value = false
  }
}
 
// 初始化图表
const initChart = () => {
  // 检查是否有数据可以绘制
  if (!hasData.value) return
  // 确保 DOM 元素存在且已渲染
  if (!messageChartRef.value) {
    console.warn('图表 DOM 元素不存在')
    return
  }
 
  // 配置图表
  echarts.use([
    LineChart,
    CanvasRenderer,
    GridComponent,
    LegendComponent,
    TooltipComponent,
    UniversalTransition
  ])
  try {
    const chart = echarts.init(messageChartRef.value)
    chart.setOption({
      tooltip: {
        trigger: 'axis',
        backgroundColor: 'rgba(255, 255, 255, 0.9)',
        borderColor: '#E5E7EB',
        textStyle: {
          color: '#374151'
        }
      },
      legend: {
        data: ['上行消息量', '下行消息量'],
        textStyle: {
          color: '#374151',
          fontWeight: 500
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: messageData.value.map((item) => item.time),
        axisLine: {
          lineStyle: {
            color: '#E5E7EB'
          }
        },
        axisLabel: {
          color: '#6B7280'
        }
      },
      yAxis: {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#E5E7EB'
          }
        },
        axisLabel: {
          color: '#6B7280'
        },
        splitLine: {
          lineStyle: {
            color: '#F3F4F6'
          }
        }
      },
      series: [
        {
          name: '上行消息量',
          type: 'line',
          smooth: true,
          data: messageData.value.map((item) => item.upstreamCount),
          itemStyle: {
            color: '#3B82F6'
          },
          lineStyle: {
            width: 2
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(59, 130, 246, 0.2)' },
              { offset: 1, color: 'rgba(59, 130, 246, 0)' }
            ])
          }
        },
        {
          name: '下行消息量',
          type: 'line',
          smooth: true,
          data: messageData.value.map((item) => item.downstreamCount),
          itemStyle: {
            color: '#10B981'
          },
          lineStyle: {
            width: 2
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(16, 185, 129, 0.2)' },
              { offset: 1, color: 'rgba(16, 185, 129, 0)' }
            ])
          }
        }
      ]
    })
    return chart
  } catch (error) {
    console.error('初始化图表失败:', error)
    return null
  }
}
 
/** 组件挂载时初始化 */
onMounted(() => {
  fetchMessageData()
})
</script>