|
@@ -0,0 +1,293 @@
|
|
1
|
+<template>
|
|
2
|
+ <div class="temperature-chart">
|
|
3
|
+ <div class="chart-container">
|
|
4
|
+ <div ref="chartRef" class="chart" style="width: 100%; height: 500px;"></div>
|
|
5
|
+ </div>
|
|
6
|
+ </div>
|
|
7
|
+</template>
|
|
8
|
+
|
|
9
|
+<script setup>
|
|
10
|
+import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
|
|
11
|
+import * as echarts from 'echarts'
|
|
12
|
+
|
|
13
|
+// Props
|
|
14
|
+const props = defineProps({
|
|
15
|
+ chartData: {
|
|
16
|
+ type: Array,
|
|
17
|
+ default: () => []
|
|
18
|
+ },
|
|
19
|
+ sensorColumns: {
|
|
20
|
+ type: Array,
|
|
21
|
+ default: () => []
|
|
22
|
+ }
|
|
23
|
+})
|
|
24
|
+
|
|
25
|
+// 响应式数据
|
|
26
|
+const chartRef = ref(null)
|
|
27
|
+let chartInstance = null
|
|
28
|
+
|
|
29
|
+// 暴露方法给父组件调用
|
|
30
|
+const resizeChart = () => {
|
|
31
|
+ if (chartInstance) {
|
|
32
|
+ chartInstance.resize()
|
|
33
|
+ }
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+// 暴露方法
|
|
37
|
+defineExpose({
|
|
38
|
+ resizeChart
|
|
39
|
+})
|
|
40
|
+
|
|
41
|
+// 初始化图表
|
|
42
|
+const initChart = () => {
|
|
43
|
+ if (chartRef.value) {
|
|
44
|
+ chartInstance = echarts.init(chartRef.value)
|
|
45
|
+ updateChart()
|
|
46
|
+ }
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+ // 更新图表数据
|
|
50
|
+ const updateChart = () => {
|
|
51
|
+ if (!chartInstance || !props.chartData || props.chartData.length === 0) {
|
|
52
|
+ return
|
|
53
|
+ }
|
|
54
|
+
|
|
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
|
+ })
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ // 图表配置
|
|
95
|
+ const option = {
|
|
96
|
+ title: {
|
|
97
|
+ text: '温度监控趋势图',
|
|
98
|
+ left: 'center',
|
|
99
|
+ textStyle: {
|
|
100
|
+ fontSize: 16,
|
|
101
|
+ fontWeight: 'bold',
|
|
102
|
+ color: '#303133'
|
|
103
|
+ }
|
|
104
|
+ },
|
|
105
|
+ tooltip: {
|
|
106
|
+ trigger: 'axis',
|
|
107
|
+ backgroundColor: 'rgba(255, 255, 255, 0.95)',
|
|
108
|
+ borderColor: '#e4e7ed',
|
|
109
|
+ borderWidth: 1,
|
|
110
|
+ textStyle: {
|
|
111
|
+ color: '#303133'
|
|
112
|
+ },
|
|
113
|
+ axisPointer: {
|
|
114
|
+ type: 'cross',
|
|
115
|
+ label: {
|
|
116
|
+ backgroundColor: '#6a7985'
|
|
117
|
+ }
|
|
118
|
+ },
|
|
119
|
+ formatter: function (params) {
|
|
120
|
+ // 格式化时间显示
|
|
121
|
+ const formatTime = (timeStr) => {
|
|
122
|
+ const date = new Date(timeStr)
|
|
123
|
+ 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')}`
|
|
124
|
+ }
|
|
125
|
+ let result = `<div style="font-weight: bold; margin-bottom: 8px; color: #303133;">${formatTime(params[0].axisValue)}</div>`
|
|
126
|
+ params.forEach(param => {
|
|
127
|
+ if (param.value !== null && param.value !== undefined) {
|
|
128
|
+ // 数据格式为 [datetime, value],所以取第二个元素作为温度值
|
|
129
|
+ const temperature = Array.isArray(param.value) ? param.value[1] : param.value
|
|
130
|
+ result += `<div style="margin: 5px 0; display: flex; align-items: center;">
|
|
131
|
+ <span style="display: inline-block; width: 12px; height: 12px; background: ${param.color}; margin-right: 8px; border-radius: 2px;"></span>
|
|
132
|
+ <span style="color: #606266;">${param.seriesName}: </span>
|
|
133
|
+ <span style="font-weight: bold; color: #303133; margin-left: 4px;">${temperature.toFixed(2)}°C</span>
|
|
134
|
+ </div>`
|
|
135
|
+ }
|
|
136
|
+ })
|
|
137
|
+ return result
|
|
138
|
+ }
|
|
139
|
+ },
|
|
140
|
+ legend: {
|
|
141
|
+ data: series.map(s => s.name),
|
|
142
|
+ top: 30,
|
|
143
|
+ type: 'scroll',
|
|
144
|
+ textStyle: {
|
|
145
|
+ color: '#606266'
|
|
146
|
+ }
|
|
147
|
+ },
|
|
148
|
+ grid: {
|
|
149
|
+ left: '3%',
|
|
150
|
+ right: '4%',
|
|
151
|
+ bottom: '15%',
|
|
152
|
+ top: '15%',
|
|
153
|
+ containLabel: true
|
|
154
|
+ },
|
|
155
|
+ xAxis: {
|
|
156
|
+ type: 'time',
|
|
157
|
+ boundaryGap: false,
|
|
158
|
+ axisLine: {
|
|
159
|
+ lineStyle: {
|
|
160
|
+ color: '#e4e7ed'
|
|
161
|
+ }
|
|
162
|
+ },
|
|
163
|
+ axisLabel: {
|
|
164
|
+ color: '#606266',
|
|
165
|
+ formatter: function (value) {
|
|
166
|
+ // 格式化时间显示
|
|
167
|
+ const date = new Date(value)
|
|
168
|
+ return `${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`
|
|
169
|
+ },
|
|
170
|
+ rotate: 45
|
|
171
|
+ },
|
|
172
|
+ axisTick: {
|
|
173
|
+ lineStyle: {
|
|
174
|
+ color: '#e4e7ed'
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+ },
|
|
178
|
+ yAxis: {
|
|
179
|
+ type: 'value',
|
|
180
|
+ name: '温度 (°C)',
|
|
181
|
+ nameLocation: 'middle',
|
|
182
|
+ nameGap: 40,
|
|
183
|
+ nameTextStyle: {
|
|
184
|
+ color: '#606266'
|
|
185
|
+ },
|
|
186
|
+ axisLine: {
|
|
187
|
+ lineStyle: {
|
|
188
|
+ color: '#e4e7ed'
|
|
189
|
+ }
|
|
190
|
+ },
|
|
191
|
+ axisLabel: {
|
|
192
|
+ color: '#606266',
|
|
193
|
+ formatter: '{value} °C'
|
|
194
|
+ },
|
|
195
|
+ axisTick: {
|
|
196
|
+ lineStyle: {
|
|
197
|
+ color: '#e4e7ed'
|
|
198
|
+ }
|
|
199
|
+ },
|
|
200
|
+ splitLine: {
|
|
201
|
+ lineStyle: {
|
|
202
|
+ color: '#f0f0f0',
|
|
203
|
+ type: 'dashed'
|
|
204
|
+ }
|
|
205
|
+ },
|
|
206
|
+ },
|
|
207
|
+ series: series,
|
|
208
|
+ dataZoom: [
|
|
209
|
+ {
|
|
210
|
+ type: 'inside',
|
|
211
|
+ start: 0,
|
|
212
|
+ end: 100
|
|
213
|
+ },
|
|
214
|
+ {
|
|
215
|
+ type: 'slider',
|
|
216
|
+ start: 0,
|
|
217
|
+ end: 100,
|
|
218
|
+ bottom: 10,
|
|
219
|
+ height: 20,
|
|
220
|
+ borderColor: '#e4e7ed',
|
|
221
|
+ fillerColor: 'rgba(64, 158, 255, 0.1)',
|
|
222
|
+ handleStyle: {
|
|
223
|
+ color: '#409eff'
|
|
224
|
+ }
|
|
225
|
+ }
|
|
226
|
+ ]
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ chartInstance.setOption(option, true)
|
|
230
|
+}
|
|
231
|
+
|
|
232
|
+// 监听数据变化
|
|
233
|
+watch(
|
|
234
|
+ () => [props.chartData, props.sensorColumns],
|
|
235
|
+ () => {
|
|
236
|
+ nextTick(() => {
|
|
237
|
+ updateChart()
|
|
238
|
+ // 延迟重新调整大小,确保DOM已更新
|
|
239
|
+ setTimeout(() => {
|
|
240
|
+ if (chartInstance) {
|
|
241
|
+ chartInstance.resize()
|
|
242
|
+ }
|
|
243
|
+ }, 100)
|
|
244
|
+ })
|
|
245
|
+ },
|
|
246
|
+ { deep: true }
|
|
247
|
+)
|
|
248
|
+
|
|
249
|
+// 监听窗口大小变化
|
|
250
|
+const handleResize = () => {
|
|
251
|
+ if (chartInstance) {
|
|
252
|
+ chartInstance.resize()
|
|
253
|
+ }
|
|
254
|
+}
|
|
255
|
+
|
|
256
|
+// 生命周期
|
|
257
|
+onMounted(() => {
|
|
258
|
+ initChart()
|
|
259
|
+ window.addEventListener('resize', handleResize)
|
|
260
|
+})
|
|
261
|
+
|
|
262
|
+onUnmounted(() => {
|
|
263
|
+ if (chartInstance) {
|
|
264
|
+ chartInstance.dispose()
|
|
265
|
+ chartInstance = null
|
|
266
|
+ }
|
|
267
|
+ window.removeEventListener('resize', handleResize)
|
|
268
|
+})
|
|
269
|
+</script>
|
|
270
|
+
|
|
271
|
+<style lang="scss" scoped>
|
|
272
|
+.temperature-chart {
|
|
273
|
+ width: 100%;
|
|
274
|
+ height: 100%;
|
|
275
|
+ display: flex;
|
|
276
|
+ flex-direction: column;
|
|
277
|
+
|
|
278
|
+ .chart-container {
|
|
279
|
+ width: 100%;
|
|
280
|
+ height: 100%;
|
|
281
|
+ min-height: 500px;
|
|
282
|
+ flex: 1;
|
|
283
|
+ display: flex;
|
|
284
|
+ flex-direction: column;
|
|
285
|
+
|
|
286
|
+ .chart {
|
|
287
|
+ width: 100% !important;
|
|
288
|
+ height: 100% !important;
|
|
289
|
+ flex: 1;
|
|
290
|
+ }
|
|
291
|
+ }
|
|
292
|
+}
|
|
293
|
+</style>
|