-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_widget.py
More file actions
352 lines (292 loc) · 12 KB
/
plot_widget.py
File metadata and controls
352 lines (292 loc) · 12 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# plot_widget.py
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QWidget, QVBoxLayout
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLabel, QDoubleSpinBox, QFrame
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QColor
class GasPlotWidget(QWidget):
"""
气体监控组件
包含:图表 + 当前值显示 + 上下限报警设置 + 报警状态指示
"""
def __init__(self, gas_name="Gas", unit="ppm", color='b', max_points=100):
super().__init__()
self.gas_name = gas_name
self.unit = unit
self.color = color
self.max_points = max_points
self.current_value = 0.0
# 默认报警值
self.upper_limit = self.get_default_upper(gas_name)
self.lower_limit = self.get_default_lower(gas_name)
self.setup_ui()
self.setup_plot()
def get_default_upper(self, gas):
defaults = {
'O2': 23.5, 'CO2': 5000, 'CH4': 1000, 'H2': 1000,
'C2H4': 100, 'CH3OH': 200
}
return defaults.get(gas, 1000)
def get_default_lower(self, gas):
defaults = {
'O2': 19.5, 'CO2': 0, 'CH4': 0, 'H2': 0,
'C2H4': 0, 'CH3OH': 0
}
return defaults.get(gas, 0)
def setup_ui(self):
# 主布局:水平布局(左:控制区,右:图表)
main_layout = QHBoxLayout(self)
main_layout.setContentsMargins(8, 8, 8, 8)
main_layout.setSpacing(10)
# 左侧控制区
control_widget = QWidget()
control_layout = QVBoxLayout(control_widget)
control_layout.setSpacing(12)
control_widget.setFixedWidth(200)
# 气体名称
self.title_label = QLabel(f"<b>{self.gas_name}</b>")
self.title_label.setAlignment(Qt.AlignCenter)
self.title_label.setStyleSheet("font-size: 20px; color: #333;")
control_layout.addWidget(self.title_label)
# 当前值显示
self.value_label = QLabel("0.00")
self.value_label.setAlignment(Qt.AlignCenter)
self.value_label.setStyleSheet(f"font-size: 24px; font-weight: bold; color: {self.color};")
self.value_label.setFont(QFont("Arial", 24, QFont.Bold))
control_layout.addWidget(self.value_label)
# 单位
unit_label = QLabel(self.unit)
unit_label.setAlignment(Qt.AlignCenter)
unit_label.setStyleSheet("font-size: 12px; color: #666;")
control_layout.addWidget(unit_label)
# 报警状态指示灯
self.status_label = QLabel("Normal")
self.status_label.setAlignment(Qt.AlignCenter)
self.status_label.setStyleSheet("""
background: #dff0d8; color: #3c763d; padding: 6px;
border-radius: 4px; font-weight: bold;
""")
control_layout.addWidget(self.status_label)
# 报警值设置
alarm_layout = QVBoxLayout()
alarm_layout.setSpacing(6)
# 上限设置
upper_layout = QHBoxLayout()
upper_layout.addWidget(QLabel("Upper limit:"))
self.upper_spin = QDoubleSpinBox()
self.upper_spin.setRange(0, 100000)
self.upper_spin.setValue(self.upper_limit)
self.upper_spin.setDecimals(2)
self.upper_spin.valueChanged.connect(self.on_upper_changed)
upper_layout.addWidget(self.upper_spin)
alarm_layout.addLayout(upper_layout)
# 下限设置
lower_layout = QHBoxLayout()
lower_layout.addWidget(QLabel("Lower limit:"))
self.lower_spin = QDoubleSpinBox()
self.lower_spin.setRange(0, 100000)
self.lower_spin.setValue(self.lower_limit)
self.lower_spin.setDecimals(2)
self.lower_spin.valueChanged.connect(self.on_lower_changed)
lower_layout.addWidget(self.lower_spin)
alarm_layout.addLayout(lower_layout)
control_layout.addLayout(alarm_layout)
control_layout.addStretch()
# 右侧图表区
chart_widget = QWidget()
chart_layout = QVBoxLayout(chart_widget)
self.canvas = FigureCanvas(Figure(dpi=100))
chart_layout.addWidget(self.canvas)
# 添加左右两部分到主布局
main_layout.addWidget(control_widget)
main_layout.addWidget(chart_widget, stretch=1)
# 添加边框
self.setStyleSheet("""
GasPlotWidget {
border: 1px solid #ddd;
border-radius: 8px;
background: white;
}
""")
def setup_plot(self):
self.ax = self.canvas.figure.add_subplot(111)
self.ax.set_title(f"{self.gas_name} trend", fontsize=10, pad=5)
self.ax.set_ylabel(f"{self.unit}", fontsize=8)
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.tick_params(axis='both', which='major', labelsize=7)
self.x_data = []
self.y_data = []
def update_plot(self, x, y):
self.current_value = y
# 更新数据
self.x_data.append(x)
self.y_data.append(y)
if len(self.x_data) > self.max_points:
self.x_data.pop(0)
self.y_data.pop(0)
# 清除重绘
self.ax.clear()
self.ax.plot(self.x_data, self.y_data, f"{self.color}o-", markersize=4, linewidth=1.5)
self.ax.relim()
self.ax.autoscale_view()
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.set_ylabel(f"{self.unit}", fontsize=8)
self.ax.tick_params(axis='both', which='major', labelsize=7)
self.canvas.draw()
# 更新当前值显示
self.value_label.setText(f"{y:.2f}")
# 检查报警状态
self.check_alarm_status(y)
def check_alarm_status(self, value):
if value > self.upper_limit:
self.set_alarm_state("⚠️ too high", "#f2dede", "#a94442")
elif value < self.lower_limit:
self.set_alarm_state("⚠️ too low", "#f2dede", "#a94442")
else:
self.set_alarm_state("✅ Normal", "#dff0d8", "#3c763d")
def set_alarm_state(self, text, bg_color, text_color):
self.status_label.setText(text)
self.status_label.setStyleSheet(f"""
background: {bg_color};
color: {text_color};
padding: 6px;
border-radius: 4px;
font-weight: bold;
""")
def on_upper_changed(self, value):
self.upper_limit = value
self.check_alarm_status(self.current_value)
def on_lower_changed(self, value):
self.lower_limit = value
self.check_alarm_status(self.current_value)
def get_upper_limit(self):
return self.upper_limit
def get_lower_limit(self):
return self.lower_limit
def get_current_value(self):
return self.current_value
class TempPlotWidget(QWidget):
def __init__(self, max_points=100):
super().__init__()
self.max_points = max_points
self.current_temp = 0.0
self.setup_ui()
self.setup_plot()
def setup_ui(self):
# 主布局:水平布局
main_layout = QHBoxLayout(self)
main_layout.setContentsMargins(8, 8, 8, 8)
main_layout.setSpacing(10)
# 左侧控制区
control_widget = QWidget()
control_layout = QVBoxLayout(control_widget)
control_layout.setSpacing(12)
control_widget.setFixedWidth(200) # 设置固定宽度以保证控件区不会过宽
# 当前值显示
self.value_label = QLabel("0.0°C")
self.value_label.setAlignment(Qt.AlignCenter)
self.value_label.setStyleSheet("font-size: 45px; font-weight: bold;")
control_layout.addWidget(self.value_label)
main_layout.addWidget(control_widget)
# 右侧图表区
chart_widget = QWidget()
chart_layout = QVBoxLayout(chart_widget)
self.canvas = FigureCanvas(Figure(dpi=100)) # 调整图表大小
chart_layout.addWidget(self.canvas)
main_layout.addWidget(chart_widget, stretch=1) # 图表占据剩余空间
self.setStyleSheet("""
border: 1px solid #ccc;
border-radius: 8px;
background: white;
""")
def setup_plot(self):
self.ax = self.canvas.figure.add_subplot(111)
self.ax.set_title("Temperature", fontsize=12, color='r')
self.ax.set_ylabel("°C", fontsize=9)
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.tick_params(axis='both', which='major', labelsize=8)
self.ax.set_xlabel("seconds", fontsize=8)
self.x_data = []
self.y_data = []
def update_plot(self, x, temp):
self.current_temp = temp
self.x_data.append(x)
self.y_data.append(temp)
if len(self.x_data) > self.max_points:
self.x_data.pop(0)
self.y_data.pop(0)
self.ax.clear()
self.ax.plot(self.x_data, self.y_data, 'r.-', markersize=4, linewidth=1.5, label='Temperature')
self.ax.set_title(f"Temperature: {temp:.1f}°C", fontsize=12, color='r')
self.ax.set_ylabel("°C", fontsize=9)
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.tick_params(axis='both', which='major', labelsize=8)
self.ax.set_xlabel("seconds", fontsize=8)
self.canvas.draw()
self.value_label.setText(f"{temp:.1f}°C")
class RhPlotWidget(QWidget):
def __init__(self, max_points=100):
super().__init__()
self.max_points = max_points
self.current_rh = 0.0
self.setup_ui()
self.setup_plot()
def setup_ui(self):
# 主布局:水平布局
main_layout = QHBoxLayout(self)
main_layout.setContentsMargins(8, 8, 8, 8)
main_layout.setSpacing(10)
# 左侧控制区
control_widget = QWidget()
control_layout = QVBoxLayout(control_widget)
control_layout.setSpacing(12)
control_widget.setFixedWidth(200) # 设置固定宽度以保证控件区不会过宽
# 当前值显示
self.value_label = QLabel("0.0%")
self.value_label.setAlignment(Qt.AlignCenter)
self.value_label.setStyleSheet("font-size: 50px; font-weight: bold;")
control_layout.addWidget(self.value_label)
main_layout.addWidget(control_widget)
# 右侧图表区
chart_widget = QWidget()
chart_layout = QVBoxLayout(chart_widget)
self.canvas = FigureCanvas(Figure(dpi=100)) # 调整图表大小
chart_layout.addWidget(self.canvas)
main_layout.addWidget(chart_widget, stretch=1) # 图表占据剩余空间
self.setStyleSheet("""
border: 1px solid #ddd;
border-radius: 8px;
background: white;
""")
def setup_plot(self):
self.ax = self.canvas.figure.add_subplot(111)
self.ax.set_title("Humidity", fontsize=12, color='b')
self.ax.set_ylabel("%", fontsize=9)
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.tick_params(axis='both', which='major', labelsize=8)
self.ax.set_xlabel("seconds", fontsize=8)
self.x_data = []
self.y_data = []
def update_plot(self, x, rh):
self.current_rh = rh
self.x_data.append(x)
self.y_data.append(rh)
if len(self.x_data) > self.max_points:
self.x_data.pop(0)
self.y_data.pop(0)
self.ax.clear()
self.ax.plot(self.x_data, self.y_data, 'b.--', markersize=4, linewidth=1.5, label='Humidity')
self.ax.set_title(f"Humidity: {rh:.1f}%", fontsize=12, color='b')
self.ax.set_ylabel("%", fontsize=9)
self.ax.grid(True, alpha=0.3, linestyle='--', linewidth=0.5)
self.ax.tick_params(axis='both', which='major', labelsize=8)
self.ax.set_xlabel("seconds", fontsize=8)
self.canvas.draw()
self.value_label.setText(f"{rh:.1f}%")