Skip to content

Commit 782397b

Browse files
committed
Add some documentation
1 parent 56d65e3 commit 782397b

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

robot_log_visualizer/plotter/pyqtgraph_viewer_canvas.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@
66

77
class PyQtGraphViewerCanvas(QtWidgets.QWidget):
88
def __init__(self, parent, signal_provider, period):
9+
"""
10+
Initialize the PyQtGraphViewerCanvas.
11+
Parameters:
12+
parent (QWidget): The parent widget.
13+
signal_provider (SignalProvider): The signal provider for data.
14+
period (float): The update period in seconds.
15+
"""
916
super().__init__(parent)
1017

1118
self.signal_provider = signal_provider
1219
self.period_in_ms = int(period * 1000)
13-
self.active_paths = {}
14-
self.annotations = []
20+
21+
self.active_paths = {} # Plotted curves
22+
self.annotations = [] # Text annotations
1523

1624
self.layout = QtWidgets.QVBoxLayout(self)
1725
self.plot_widget = pg.PlotWidget()
18-
self.layout.addWidget(self.plot_widget)
1926
self.plot_widget.setBackground('w')
27+
self.layout.addWidget(self.plot_widget)
2028

2129
# Font styles
2230
label_style = {'color': '#000', 'font-size': '14px', 'font-weight': 'bold'}
@@ -42,6 +50,13 @@ def __init__(self, parent, signal_provider, period):
4250
self.plot_widget.scene().sigMouseClicked.connect(self.on_click)
4351

4452
def update_plots(self, paths, legends):
53+
"""
54+
Update plots based on provided data paths and their corresponding legends.
55+
56+
Parameters:
57+
paths (list): List of paths representing data series to plot.
58+
legends (list): Corresponding legend labels for the paths.
59+
"""
4560
for path, legend in zip(paths, legends):
4661
path_string = "/".join(path)
4762
legend_string = "/".join(legend[1:])
@@ -64,26 +79,37 @@ def update_plots(self, paths, legends):
6479
)
6580
self.active_paths[path_string] = curve
6681

82+
# Remove plots that are no longer active
6783
paths_to_remove = [p for p in self.active_paths if p.split('/') not in paths]
6884
for path in paths_to_remove:
6985
self.plot_widget.removeItem(self.active_paths[path])
7086
del self.active_paths[path]
7187

88+
# Set the x-axis range to the full time range of the data
7289
self.plot_widget.setXRange(0, self.signal_provider.end_time - self.signal_provider.initial_time)
7390

7491
def update_vertical_line(self):
92+
"""
93+
Update the position of the vertical line based on the current time.
94+
"""
7595
current_time = self.signal_provider.current_time
7696
self.vertical_line.setValue(current_time)
7797

7898
def on_click(self, event):
99+
"""
100+
Handle mouse click events to add annotations to the plot.
101+
Clicking on a data point will display its coordinates.
102+
"""
79103
pos = event.scenePos()
80104
if self.plot_widget.sceneBoundingRect().contains(pos):
105+
# Convert scene coordinates to plot coordinates
81106
mouse_point = self.plot_widget.plotItem.vb.mapSceneToView(pos)
82107
x_click = mouse_point.x()
83108
y_click = mouse_point.y()
84109

85110
closest_curve, closest_point, min_dist = None, None, float('inf')
86111

112+
# Find the closest curve to the clicked point
87113
for curve in self.active_paths.values():
88114
xdata, ydata = curve.getData()
89115
distances = np.sqrt((xdata - x_click)**2 + (ydata - y_click)**2)
@@ -94,6 +120,7 @@ def on_click(self, event):
94120
closest_curve = curve
95121
closest_point = (xdata[index], ydata[index])
96122

123+
# If the closest point is within a certain distance, add an annotation
97124
if min_dist < 0.01 * (self.plot_widget.viewRange()[0][1] - self.plot_widget.viewRange()[0][0]):
98125
text = f"{closest_point[0]:.3f}, {closest_point[1]:.3f}"
99126
annotation = pg.TextItem(text, anchor=(0,1))
@@ -102,15 +129,27 @@ def on_click(self, event):
102129
self.annotations.append(annotation)
103130

104131
def clear_annotations(self):
132+
"""
133+
Clear all annotations from the plot.
134+
"""
105135
for annotation in self.annotations:
106136
self.plot_widget.removeItem(annotation)
107137
self.annotations.clear()
108138

109139
def quit_animation(self):
140+
"""
141+
Stop the animation and clear all plots.
142+
"""
110143
self.timer.stop()
111144

112145
def pause_animation(self):
146+
"""
147+
Pause the animation by stopping the timer.
148+
"""
113149
self.timer.stop()
114150

115151
def resume_animation(self):
152+
"""
153+
Resume the animation by restarting the timer.
154+
"""
116155
self.timer.start(self.period_in_ms)

0 commit comments

Comments
 (0)