|
@@ -1,7 +1,10 @@
|
1
|
1
|
<template>
|
2
|
2
|
<div class="temperature-chart">
|
3
|
3
|
<div class="chart-container">
|
4
|
|
- <div ref="chartRef" class="chart" style="width: 100%; height: 500px;"></div>
|
|
4
|
+ <!-- 趋势图 -->
|
|
5
|
+ <div ref="trendChartRef" class="trend-chart" style="width: 60%; height: 600px;"></div>
|
|
6
|
+ <!-- 时刻温度对比图 -->
|
|
7
|
+ <div ref="momentChartRef" class="moment-chart" style="width: 40%; height: 600px;"></div>
|
5
|
8
|
</div>
|
6
|
9
|
</div>
|
7
|
10
|
</template>
|
|
@@ -23,13 +26,21 @@ const props = defineProps({
|
23
|
26
|
})
|
24
|
27
|
|
25
|
28
|
// 响应式数据
|
26
|
|
-const chartRef = ref(null)
|
27
|
|
-let chartInstance = null
|
|
29
|
+const trendChartRef = ref(null)
|
|
30
|
+const momentChartRef = ref(null)
|
|
31
|
+let trendChartInstance = null
|
|
32
|
+let momentChartInstance = null
|
|
33
|
+
|
|
34
|
+// 当前选中的时刻数据
|
|
35
|
+const selectedMoment = ref(null)
|
28
|
36
|
|
29
|
37
|
// 暴露方法给父组件调用
|
30
|
38
|
const resizeChart = () => {
|
31
|
|
- if (chartInstance) {
|
32
|
|
- chartInstance.resize()
|
|
39
|
+ if (trendChartInstance) {
|
|
40
|
+ trendChartInstance.resize()
|
|
41
|
+ }
|
|
42
|
+ if (momentChartInstance) {
|
|
43
|
+ momentChartInstance.resize()
|
33
|
44
|
}
|
34
|
45
|
}
|
35
|
46
|
|
|
@@ -40,59 +51,58 @@ defineExpose({
|
40
|
51
|
|
41
|
52
|
// 初始化图表
|
42
|
53
|
const initChart = () => {
|
43
|
|
- if (chartRef.value) {
|
44
|
|
- chartInstance = echarts.init(chartRef.value)
|
45
|
|
- updateChart()
|
|
54
|
+ if (trendChartRef.value && momentChartRef.value) {
|
|
55
|
+ trendChartInstance = echarts.init(trendChartRef.value)
|
|
56
|
+ momentChartInstance = echarts.init(momentChartRef.value)
|
|
57
|
+ updateCharts()
|
46
|
58
|
}
|
47
|
59
|
}
|
48
|
60
|
|
49
|
|
- // 更新图表数据
|
50
|
|
- const updateChart = () => {
|
51
|
|
- if (!chartInstance || !props.chartData || props.chartData.length === 0) {
|
52
|
|
- return
|
53
|
|
- }
|
|
61
|
+// 更新图表数据
|
|
62
|
+const updateCharts = () => {
|
|
63
|
+ if (!trendChartInstance || !momentChartInstance || !props.chartData || props.chartData.length === 0) {
|
|
64
|
+ return
|
|
65
|
+ }
|
54
|
66
|
|
55
|
|
- // 准备图表数据 - 按时间排序
|
56
|
|
- const sortedData = [...props.chartData].sort((a, b) => new Date(a.datetime) - new Date(b.datetime))
|
57
|
|
- const series = []
|
58
|
|
-
|
59
|
|
- // 为每个传感器创建数据系列
|
60
|
|
- props.sensorColumns.forEach(sensor => {
|
61
|
|
- // 过滤出有效的数据点
|
62
|
|
- const validDataPoints = sortedData
|
63
|
|
- .map(item => {
|
64
|
|
- const value = item[sensor.sensorName]
|
65
|
|
- return {
|
66
|
|
- datetime: item.datetime,
|
67
|
|
- value: value !== null && value !== undefined && !isNaN(value) ? parseFloat(value) : null
|
68
|
|
- }
|
69
|
|
- })
|
70
|
|
- .filter(point => point.value !== null)
|
71
|
|
-
|
72
|
|
- // 只有当传感器有有效数据时才添加到系列中
|
73
|
|
- if (validDataPoints.length > 0) {
|
74
|
|
- series.push({
|
75
|
|
- name: sensor.sensorName,
|
76
|
|
- type: 'line',
|
77
|
|
- data: validDataPoints.map(point => [point.datetime, point.value]),
|
78
|
|
- showSymbol: false,
|
79
|
|
- smooth: true,
|
80
|
|
- symbol: 'circle',
|
81
|
|
- symbolSize: 6,
|
82
|
|
- lineStyle: {
|
83
|
|
- width: 1
|
84
|
|
- },
|
85
|
|
- itemStyle: {
|
86
|
|
- borderWidth: 1
|
87
|
|
- }
|
88
|
|
- })
|
89
|
|
- }
|
90
|
|
- })
|
|
67
|
+ // 准备图表数据 - 按时间排序
|
|
68
|
+ const sortedData = [...props.chartData].sort((a, b) => new Date(a.datetime) - new Date(b.datetime))
|
|
69
|
+ const series = []
|
91
|
70
|
|
|
71
|
+ // 为每个传感器创建数据系列
|
|
72
|
+ props.sensorColumns.forEach(sensor => {
|
|
73
|
+ // 过滤出有效的数据点
|
|
74
|
+ const validDataPoints = sortedData
|
|
75
|
+ .map(item => {
|
|
76
|
+ const value = item[sensor.sensorName]
|
|
77
|
+ return {
|
|
78
|
+ datetime: item.datetime,
|
|
79
|
+ value: value !== null && value !== undefined && !isNaN(value) ? parseFloat(value) : null
|
|
80
|
+ }
|
|
81
|
+ })
|
|
82
|
+ .filter(point => point.value !== null)
|
92
|
83
|
|
|
84
|
+ // 只有当传感器有有效数据时才添加到系列中
|
|
85
|
+ if (validDataPoints.length > 0) {
|
|
86
|
+ series.push({
|
|
87
|
+ name: sensor.sensorName,
|
|
88
|
+ type: 'line',
|
|
89
|
+ data: validDataPoints.map(point => [point.datetime, point.value]),
|
|
90
|
+ showSymbol: false,
|
|
91
|
+ smooth: true,
|
|
92
|
+ symbol: 'circle',
|
|
93
|
+ symbolSize: 6,
|
|
94
|
+ lineStyle: {
|
|
95
|
+ width: 1
|
|
96
|
+ },
|
|
97
|
+ itemStyle: {
|
|
98
|
+ borderWidth: 1
|
|
99
|
+ }
|
|
100
|
+ })
|
|
101
|
+ }
|
|
102
|
+ })
|
93
|
103
|
|
94
|
|
- // 图表配置
|
95
|
|
- const option = {
|
|
104
|
+ // 趋势图配置
|
|
105
|
+ const trendOption = {
|
96
|
106
|
title: {
|
97
|
107
|
text: '温度监控趋势图',
|
98
|
108
|
left: 'center',
|
|
@@ -203,6 +213,33 @@ const initChart = () => {
|
203
|
213
|
type: 'dashed'
|
204
|
214
|
}
|
205
|
215
|
},
|
|
216
|
+ // 根据实际数据范围设置坐标轴,最小温度刻度比实际最小温度小2度,最大温度刻度比实际最大温度高2度
|
|
217
|
+ min: function(value) {
|
|
218
|
+ const allTemps = []
|
|
219
|
+ series.forEach(s => {
|
|
220
|
+ s.data.forEach(point => {
|
|
221
|
+ if (Array.isArray(point) && point[1] !== null && point[1] !== undefined) {
|
|
222
|
+ allTemps.push(point[1])
|
|
223
|
+ }
|
|
224
|
+ })
|
|
225
|
+ })
|
|
226
|
+ if (allTemps.length === 0) return 0
|
|
227
|
+ const minTemp = Math.min(...allTemps)
|
|
228
|
+ return parseInt(minTemp - 2)
|
|
229
|
+ },
|
|
230
|
+ max: function(value) {
|
|
231
|
+ const allTemps = []
|
|
232
|
+ series.forEach(s => {
|
|
233
|
+ s.data.forEach(point => {
|
|
234
|
+ if (Array.isArray(point) && point[1] !== null && point[1] !== undefined) {
|
|
235
|
+ allTemps.push(point[1])
|
|
236
|
+ }
|
|
237
|
+ })
|
|
238
|
+ })
|
|
239
|
+ if (allTemps.length === 0) return 100
|
|
240
|
+ const maxTemp = Math.max(...allTemps)
|
|
241
|
+ return parseInt(maxTemp + 2)
|
|
242
|
+ }
|
206
|
243
|
},
|
207
|
244
|
series: series,
|
208
|
245
|
dataZoom: [
|
|
@@ -226,7 +263,227 @@ const initChart = () => {
|
226
|
263
|
]
|
227
|
264
|
}
|
228
|
265
|
|
229
|
|
- chartInstance.setOption(option, true)
|
|
266
|
+ // 设置趋势图点击事件
|
|
267
|
+ trendChartInstance.off('click')
|
|
268
|
+ trendChartInstance.on('click', (params) => {
|
|
269
|
+ // 处理点击事件,获取点击的时间点
|
|
270
|
+ let clickedTime = null
|
|
271
|
+
|
|
272
|
+ if (params.componentType === 'series') {
|
|
273
|
+ // 点击在数据系列上
|
|
274
|
+ clickedTime = params.value[0]
|
|
275
|
+ } else if (params.componentType === 'xAxis') {
|
|
276
|
+ // 点击在X轴上
|
|
277
|
+ clickedTime = params.value
|
|
278
|
+ } else if (params.componentType === 'grid') {
|
|
279
|
+ // 点击在图表网格上,需要根据鼠标位置计算时间
|
|
280
|
+ const pointInPixel = [params.event.offsetX, params.event.offsetY]
|
|
281
|
+ const pointInGrid = trendChartInstance.convertFromPixel({seriesIndex: 0}, pointInPixel)
|
|
282
|
+ if (pointInGrid && pointInGrid.length > 0) {
|
|
283
|
+ clickedTime = pointInGrid[0]
|
|
284
|
+ }
|
|
285
|
+ }
|
|
286
|
+
|
|
287
|
+ if (clickedTime) {
|
|
288
|
+ // 找到最接近的时间点
|
|
289
|
+ const closestDataPoint = sortedData.reduce((closest, current) => {
|
|
290
|
+ const currentDiff = Math.abs(new Date(current.datetime) - new Date(clickedTime))
|
|
291
|
+ const closestDiff = Math.abs(new Date(closest.datetime) - new Date(clickedTime))
|
|
292
|
+ return currentDiff < closestDiff ? current : closest
|
|
293
|
+ })
|
|
294
|
+
|
|
295
|
+ updateMomentChart(closestDataPoint.datetime)
|
|
296
|
+ }
|
|
297
|
+ })
|
|
298
|
+
|
|
299
|
+ trendChartInstance.setOption(trendOption, true)
|
|
300
|
+
|
|
301
|
+ // 初始化时刻图表,默认显示第一个时刻的数据
|
|
302
|
+ if (sortedData.length > 0) {
|
|
303
|
+ const firstTime = sortedData[0].datetime
|
|
304
|
+ updateMomentChart(firstTime)
|
|
305
|
+ }
|
|
306
|
+}
|
|
307
|
+
|
|
308
|
+// 更新时刻温度对比图
|
|
309
|
+const updateMomentChart = (selectedTime) => {
|
|
310
|
+ if (!momentChartInstance || !props.chartData || props.chartData.length === 0) {
|
|
311
|
+ return
|
|
312
|
+ }
|
|
313
|
+
|
|
314
|
+ // 找到对应时刻的数据
|
|
315
|
+ const momentData = props.chartData.find(item => item.datetime === selectedTime)
|
|
316
|
+ if (!momentData) {
|
|
317
|
+ return
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ selectedMoment.value = selectedTime
|
|
321
|
+
|
|
322
|
+ // 准备时刻数据
|
|
323
|
+ const momentSeries = []
|
|
324
|
+ const colors = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4']
|
|
325
|
+
|
|
326
|
+ props.sensorColumns.forEach((sensor, index) => {
|
|
327
|
+ const value = momentData[sensor.sensorName]
|
|
328
|
+ if (value !== null && value !== undefined && !isNaN(value)) {
|
|
329
|
+ momentSeries.push({
|
|
330
|
+ name: sensor.sensorName,
|
|
331
|
+ value: parseFloat(value),
|
|
332
|
+ itemStyle: {
|
|
333
|
+ color: colors[index % colors.length]
|
|
334
|
+ }
|
|
335
|
+ })
|
|
336
|
+ }
|
|
337
|
+ })
|
|
338
|
+
|
|
339
|
+ // 格式化时间显示
|
|
340
|
+ const formatTime = (timeStr) => {
|
|
341
|
+ const date = new Date(timeStr)
|
|
342
|
+ return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ // 如果没有有效数据,显示空状态
|
|
346
|
+ if (momentSeries.length === 0) {
|
|
347
|
+ const emptyOption = {
|
|
348
|
+ title: {
|
|
349
|
+ text: `时刻温度对比 - ${formatTime(selectedTime)}`,
|
|
350
|
+ left: 'center',
|
|
351
|
+ textStyle: {
|
|
352
|
+ fontSize: 14,
|
|
353
|
+ fontWeight: 'bold',
|
|
354
|
+ color: '#303133'
|
|
355
|
+ }
|
|
356
|
+ },
|
|
357
|
+ graphic: {
|
|
358
|
+ type: 'text',
|
|
359
|
+ left: 'center',
|
|
360
|
+ top: 'middle',
|
|
361
|
+ style: {
|
|
362
|
+ text: '该时刻暂无温度数据',
|
|
363
|
+ fontSize: 16,
|
|
364
|
+ fill: '#909399'
|
|
365
|
+ }
|
|
366
|
+ }
|
|
367
|
+ }
|
|
368
|
+ momentChartInstance.setOption(emptyOption, true)
|
|
369
|
+ return
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ // 时刻图表配置 - 折线图,y轴为legend,x轴为温度
|
|
373
|
+ const momentOption = {
|
|
374
|
+ title: {
|
|
375
|
+ text: `时刻温度对比 - ${formatTime(selectedTime)}`,
|
|
376
|
+ left: 'center',
|
|
377
|
+ textStyle: {
|
|
378
|
+ fontSize: 14,
|
|
379
|
+ fontWeight: 'bold',
|
|
380
|
+ color: '#303133'
|
|
381
|
+ }
|
|
382
|
+ },
|
|
383
|
+ tooltip: {
|
|
384
|
+ trigger: 'axis',
|
|
385
|
+ backgroundColor: 'rgba(255, 255, 255, 0.95)',
|
|
386
|
+ borderColor: '#e4e7ed',
|
|
387
|
+ borderWidth: 1,
|
|
388
|
+ textStyle: {
|
|
389
|
+ color: '#303133'
|
|
390
|
+ },
|
|
391
|
+ formatter: function (params) {
|
|
392
|
+ return `<div style="font-weight: bold; margin-bottom: 8px; color: #303133;">${params[0].name}</div>
|
|
393
|
+ <div style="margin: 5px 0; display: flex; align-items: center;">
|
|
394
|
+ <span style="display: inline-block; width: 12px; height: 12px; background: ${params[0].color}; margin-right: 8px; border-radius: 2px;"></span>
|
|
395
|
+ <span style="color: #606266;">温度: </span>
|
|
396
|
+ <span style="font-weight: bold; color: #303133; margin-left: 4px;">${params[0].value.toFixed(2)}°C</span>
|
|
397
|
+ </div>`
|
|
398
|
+ }
|
|
399
|
+ },
|
|
400
|
+ grid: {
|
|
401
|
+ left: '15%',
|
|
402
|
+ right: '15%',
|
|
403
|
+ bottom: '5%',
|
|
404
|
+ top: '8%',
|
|
405
|
+ containLabel: true
|
|
406
|
+ },
|
|
407
|
+ xAxis: {
|
|
408
|
+ type: 'value',
|
|
409
|
+ name: '温度 (°C)',
|
|
410
|
+ nameLocation: 'middle',
|
|
411
|
+ nameGap: 30,
|
|
412
|
+ nameTextStyle: {
|
|
413
|
+ color: '#606266'
|
|
414
|
+ },
|
|
415
|
+ axisLine: {
|
|
416
|
+ lineStyle: {
|
|
417
|
+ color: '#e4e7ed'
|
|
418
|
+ }
|
|
419
|
+ },
|
|
420
|
+ axisLabel: {
|
|
421
|
+ color: '#606266',
|
|
422
|
+ formatter: '{value} °C'
|
|
423
|
+ },
|
|
424
|
+ axisTick: {
|
|
425
|
+ lineStyle: {
|
|
426
|
+ color: '#e4e7ed'
|
|
427
|
+ }
|
|
428
|
+ },
|
|
429
|
+ splitLine: {
|
|
430
|
+ lineStyle: {
|
|
431
|
+ color: '#f0f0f0',
|
|
432
|
+ type: 'dashed'
|
|
433
|
+ }
|
|
434
|
+ },
|
|
435
|
+ // 根据实际数据范围设置坐标轴,最小温度刻度比实际最小温度小2度,最大温度刻度比实际最大温度高2度
|
|
436
|
+ min: function(value) {
|
|
437
|
+ const minTemp = Math.min(...momentSeries.map(s => s.value))
|
|
438
|
+ return parseInt(minTemp - 2)
|
|
439
|
+ },
|
|
440
|
+ max: function(value) {
|
|
441
|
+ const maxTemp = Math.max(...momentSeries.map(s => s.value))
|
|
442
|
+ return parseInt(maxTemp + 2)
|
|
443
|
+ }
|
|
444
|
+ },
|
|
445
|
+ yAxis: {
|
|
446
|
+ type: 'category',
|
|
447
|
+ data: momentSeries.map(s => s.name),
|
|
448
|
+ axisLine: {
|
|
449
|
+ lineStyle: {
|
|
450
|
+ color: '#e4e7ed'
|
|
451
|
+ }
|
|
452
|
+ },
|
|
453
|
+ axisLabel: {
|
|
454
|
+ color: '#606266',
|
|
455
|
+ show: true
|
|
456
|
+ },
|
|
457
|
+ axisTick: {
|
|
458
|
+ lineStyle: {
|
|
459
|
+ color: '#e4e7ed'
|
|
460
|
+ }
|
|
461
|
+ }
|
|
462
|
+ },
|
|
463
|
+ series: [
|
|
464
|
+ {
|
|
465
|
+ name: '温度',
|
|
466
|
+ type: 'line',
|
|
467
|
+ data: momentSeries.map(s => s.value),
|
|
468
|
+ symbol: 'circle',
|
|
469
|
+ symbolSize: 8,
|
|
470
|
+ lineStyle: {
|
|
471
|
+ width: 3
|
|
472
|
+ },
|
|
473
|
+ itemStyle: {
|
|
474
|
+ color: '#409eff'
|
|
475
|
+ },
|
|
476
|
+ label: {
|
|
477
|
+ show: true,
|
|
478
|
+ position: 'right',
|
|
479
|
+ formatter: '{c}°C',
|
|
480
|
+ color: '#606266'
|
|
481
|
+ }
|
|
482
|
+ }
|
|
483
|
+ ]
|
|
484
|
+ }
|
|
485
|
+
|
|
486
|
+ momentChartInstance.setOption(momentOption, true)
|
230
|
487
|
}
|
231
|
488
|
|
232
|
489
|
// 监听数据变化
|
|
@@ -234,11 +491,14 @@ watch(
|
234
|
491
|
() => [props.chartData, props.sensorColumns],
|
235
|
492
|
() => {
|
236
|
493
|
nextTick(() => {
|
237
|
|
- updateChart()
|
|
494
|
+ updateCharts()
|
238
|
495
|
// 延迟重新调整大小,确保DOM已更新
|
239
|
496
|
setTimeout(() => {
|
240
|
|
- if (chartInstance) {
|
241
|
|
- chartInstance.resize()
|
|
497
|
+ if (trendChartInstance) {
|
|
498
|
+ trendChartInstance.resize()
|
|
499
|
+ }
|
|
500
|
+ if (momentChartInstance) {
|
|
501
|
+ momentChartInstance.resize()
|
242
|
502
|
}
|
243
|
503
|
}, 100)
|
244
|
504
|
})
|
|
@@ -248,8 +508,11 @@ watch(
|
248
|
508
|
|
249
|
509
|
// 监听窗口大小变化
|
250
|
510
|
const handleResize = () => {
|
251
|
|
- if (chartInstance) {
|
252
|
|
- chartInstance.resize()
|
|
511
|
+ if (trendChartInstance) {
|
|
512
|
+ trendChartInstance.resize()
|
|
513
|
+ }
|
|
514
|
+ if (momentChartInstance) {
|
|
515
|
+ momentChartInstance.resize()
|
253
|
516
|
}
|
254
|
517
|
}
|
255
|
518
|
|
|
@@ -260,9 +523,13 @@ onMounted(() => {
|
260
|
523
|
})
|
261
|
524
|
|
262
|
525
|
onUnmounted(() => {
|
263
|
|
- if (chartInstance) {
|
264
|
|
- chartInstance.dispose()
|
265
|
|
- chartInstance = null
|
|
526
|
+ if (trendChartInstance) {
|
|
527
|
+ trendChartInstance.dispose()
|
|
528
|
+ trendChartInstance = null
|
|
529
|
+ }
|
|
530
|
+ if (momentChartInstance) {
|
|
531
|
+ momentChartInstance.dispose()
|
|
532
|
+ momentChartInstance = null
|
266
|
533
|
}
|
267
|
534
|
window.removeEventListener('resize', handleResize)
|
268
|
535
|
})
|
|
@@ -278,15 +545,26 @@ onUnmounted(() => {
|
278
|
545
|
.chart-container {
|
279
|
546
|
width: 100%;
|
280
|
547
|
height: 100%;
|
281
|
|
- min-height: 500px;
|
|
548
|
+ min-height: 600px;
|
282
|
549
|
flex: 1;
|
283
|
550
|
display: flex;
|
284
|
|
- flex-direction: column;
|
|
551
|
+ flex-direction: row;
|
|
552
|
+ gap: 20px;
|
|
553
|
+
|
|
554
|
+ .trend-chart {
|
|
555
|
+ width: 60% !important;
|
|
556
|
+ height: 600px !important;
|
|
557
|
+ border: 1px solid #e4e7ed;
|
|
558
|
+ border-radius: 8px;
|
|
559
|
+ background: #ffffff;
|
|
560
|
+ }
|
285
|
561
|
|
286
|
|
- .chart {
|
287
|
|
- width: 100% !important;
|
288
|
|
- height: 100% !important;
|
289
|
|
- flex: 1;
|
|
562
|
+ .moment-chart {
|
|
563
|
+ width: 40% !important;
|
|
564
|
+ height: 600px !important;
|
|
565
|
+ border: 1px solid #e4e7ed;
|
|
566
|
+ border-radius: 8px;
|
|
567
|
+ background: #ffffff;
|
290
|
568
|
}
|
291
|
569
|
}
|
292
|
570
|
}
|