Skip to content

Commit dacc9a5

Browse files
committed
fix: Exception on Removing Ignored Plot #40
feat: Adjusting text in vertical leading lines #41
1 parent dee3bc1 commit dacc9a5

File tree

8 files changed

+88
-68
lines changed

8 files changed

+88
-68
lines changed

pglive/examples_pyqt5/leading_line.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pyqtgraph as pg # type: ignore
77

8-
from pglive.kwargs import LeadingLine
8+
from pglive.kwargs import LeadingLine, Orientation
99
from pglive.sources.data_connector import DataConnector
1010
from pglive.sources.live_plot import LiveLinePlot, LiveScatterPlot, LiveHBarPlot, LiveVBarPlot
1111
from pglive.sources.live_plot_widget import LivePlotWidget
@@ -22,7 +22,8 @@
2222
# Line plot displaying Vertical Leading line and using Y axis value
2323
plot_widget_1 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with Y")
2424
plot = LiveLinePlot()
25-
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y)
25+
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y, text_color="white",
26+
text_orientation=Orientation.HORIZONTAL)
2627
plot_widget_1.addItem(plot)
2728
data_connector = DataConnector(plot, max_points=300, update_rate=50)
2829
layout.addWidget(plot_widget_1)
@@ -38,7 +39,8 @@
3839

3940
plot_widget_3 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with X")
4041
plot = LiveHBarPlot()
41-
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y)
42+
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y, text_color="blue",
43+
text_orientation=Orientation.VERTICAL)
4244
plot_widget_3.addItem(plot)
4345
data_connector3 = DataConnector(plot, max_points=300, update_rate=50)
4446
layout.addWidget(plot_widget_3)

pglive/examples_pyqt6/leading_line.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pyqtgraph as pg # type: ignore
77

8-
from pglive.kwargs import LeadingLine
8+
from pglive.kwargs import LeadingLine, Orientation
99
from pglive.sources.data_connector import DataConnector
1010
from pglive.sources.live_plot import LiveLinePlot, LiveScatterPlot, LiveHBarPlot, LiveVBarPlot
1111
from pglive.sources.live_plot_widget import LivePlotWidget
@@ -22,7 +22,8 @@
2222
# Line plot displaying Vertical Leading line and using Y axis value
2323
plot_widget_1 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with Y")
2424
plot = LiveLinePlot()
25-
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y)
25+
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y, text_color="white",
26+
text_orientation=Orientation.HORIZONTAL)
2627
plot_widget_1.addItem(plot)
2728
data_connector = DataConnector(plot, max_points=300, update_rate=50)
2829
layout.addWidget(plot_widget_1)
@@ -38,7 +39,8 @@
3839

3940
plot_widget_3 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with X")
4041
plot = LiveHBarPlot()
41-
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y)
42+
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y, text_color="blue",
43+
text_orientation=Orientation.VERTICAL)
4244
plot_widget_3.addItem(plot)
4345
data_connector3 = DataConnector(plot, max_points=300, update_rate=50)
4446
layout.addWidget(plot_widget_3)

pglive/examples_pyside6/leading_line.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pyqtgraph as pg # type: ignore
77

8-
from pglive.kwargs import LeadingLine
8+
from pglive.kwargs import LeadingLine, Orientation
99
from pglive.sources.data_connector import DataConnector
1010
from pglive.sources.live_plot import LiveLinePlot, LiveScatterPlot, LiveHBarPlot, LiveVBarPlot
1111
from pglive.sources.live_plot_widget import LivePlotWidget
@@ -22,7 +22,8 @@
2222
# Line plot displaying Vertical Leading line and using Y axis value
2323
plot_widget_1 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with Y")
2424
plot = LiveLinePlot()
25-
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y)
25+
plot.set_leading_line(LeadingLine.VERTICAL, pen=mkPen("red"), text_axis=LeadingLine.AXIS_Y, text_color="white",
26+
text_orientation=Orientation.HORIZONTAL)
2627
plot_widget_1.addItem(plot)
2728
data_connector = DataConnector(plot, max_points=300, update_rate=50)
2829
layout.addWidget(plot_widget_1)
@@ -38,7 +39,8 @@
3839

3940
plot_widget_3 = LivePlotWidget(title="Line Plot @ 50Hz, Leading with X")
4041
plot = LiveHBarPlot()
41-
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y)
42+
plot.set_leading_line(LeadingLine.HORIZONTAL, pen=mkPen("yellow"), text_axis=LeadingLine.AXIS_Y, text_color="blue",
43+
text_orientation=Orientation.VERTICAL)
4244
plot_widget_3.addItem(plot)
4345
data_connector3 = DataConnector(plot, max_points=300, update_rate=50)
4446
layout.addWidget(plot_widget_3)

pglive/kwargs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class Axis:
2525
class LeadingLine:
2626
HORIZONTAL = "Horizontal"
2727
VERTICAL = "Vertical"
28-
TEXT_COLOR = "TextColor"
2928
AXIS_X = "x"
3029
AXIS_Y = "y"
30+
31+
class Orientation:
32+
AUTO = "Auto"
33+
HORIZONTAL = "Horizontal"
34+
VERTICAL = "Vertical"

pglive/sources/live_axis_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def ignore_connector(self, data_connector, flag: bool) -> None:
215215

216216
def remove_data_connector(self, data_connector):
217217
if data_connector.__hash__() in self.ignored_data_connectors:
218-
del self.ignored_data_connectors[data_connector.__hash__()]
218+
self.ignored_data_connectors.remove(data_connector.__hash__())
219219
if data_connector.__hash__() in self.y_range:
220220
del self.y_range[data_connector.__hash__()]
221221
if data_connector.__hash__() in self.x_range:

pglive/sources/live_mixins.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pyqtgraph as pg # type: ignore
66
from pyqtgraph.Qt import QtGui, QtCore # type: ignore
77

8-
from pglive.kwargs import LeadingLine
8+
from pglive.kwargs import LeadingLine, Orientation
99
from pglive.sources.live_plot_widget import LivePlotWidget
1010
from pglive.sources.utils import NUM_LIST
1111

@@ -74,7 +74,8 @@ class MixinLeadingLine(SupportsLivePlot):
7474
_vl_kwargs = None
7575

7676
def set_leading_line(self, orientation: str = LeadingLine.VERTICAL,
77-
pen: QtGui.QPen = None, text_axis: str = LeadingLine.AXIS_X, **kwargs) -> Dict:
77+
pen: QtGui.QPen = None, text_axis: str = LeadingLine.AXIS_X,
78+
text_color: str = "black", text_orientation: Orientation = Orientation.AUTO, **kwargs) -> Dict:
7879
text_axis = text_axis.lower()
7980
assert text_axis in (LeadingLine.AXIS_X, LeadingLine.AXIS_Y)
8081

@@ -84,19 +85,21 @@ def set_leading_line(self, orientation: str = LeadingLine.VERTICAL,
8485
pen = self.opts.get("pen")
8586
if orientation == LeadingLine.VERTICAL:
8687
_v_leading_line = pg.InfiniteLine(angle=90, movable=False, pen=pen)
87-
_v_leading_text = pg.TextItem(color="black", angle=-90, fill=pen.color())
88+
text_angle = -90 if text_orientation in [Orientation.AUTO, Orientation.VERTICAL] else 0
89+
_v_leading_text = pg.TextItem(color=text_color, angle=text_angle, fill=pen.color())
8890
_v_leading_line.setZValue(999)
8991
_v_leading_text.setZValue(999)
9092
self._vl_kwargs = {"line": _v_leading_line, "text": _v_leading_text, "pen": pen, "text_axis": text_axis,
91-
**kwargs}
93+
"text_color": text_color, "text_orientation": text_orientation, **kwargs}
9294
return self._vl_kwargs
9395
elif orientation == LeadingLine.HORIZONTAL:
9496
_h_leading_line = pg.InfiniteLine(angle=0, movable=False, pen=pen)
95-
_h_leading_text = pg.TextItem(color="black", fill=pen.color())
97+
text_angle = 0 if text_orientation in [Orientation.AUTO, Orientation.HORIZONTAL] else -90
98+
_h_leading_text = pg.TextItem(color=text_color, angle=text_angle, fill=pen.color())
9699
_h_leading_text.setZValue(999)
97100
_h_leading_text.setZValue(999)
98101
self._hl_kwargs = {"line": _h_leading_line, "text": _h_leading_text, "pen": pen, "text_axis": text_axis,
99-
**kwargs}
102+
"text_color": text_color, "text_orientation": text_orientation, **kwargs}
100103
return self._hl_kwargs
101104
else:
102105
raise TypeError("Unsupported LeadingLine type")
@@ -127,14 +130,21 @@ def update_leading_text(self, x: float, y: float, x_text: Optional[str] = None,
127130
self._vl_kwargs["text"].setText(text_axis)
128131
pixel_pos = vb.mapViewToScene(QtCore.QPointF(x, y))
129132
y_pos = 0 + self._vl_kwargs["text"].boundingRect().height() + 10
130-
new_pos = vb.mapSceneToView(QtCore.QPointF(pixel_pos.x(), y_pos))
133+
if self._vl_kwargs["text_orientation"] in [Orientation.AUTO, Orientation.VERTICAL]:
134+
new_pos = vb.mapSceneToView(QtCore.QPointF(pixel_pos.x(), y_pos))
135+
else:
136+
x_pos = pixel_pos.x() - self._vl_kwargs["text"].boundingRect().width()
137+
new_pos = vb.mapSceneToView(QtCore.QPointF(x_pos, y_pos))
131138
self._vl_kwargs["text"].setPos(new_pos.x(), new_pos.y())
132139

133140
if self._hl_kwargs is not None:
134141
text_axis = x_text if self._hl_kwargs["text_axis"] == LeadingLine.AXIS_X else y_text
135142
self._hl_kwargs["text"].setText(text_axis)
136143
pixel_pos = vb.mapViewToScene(QtCore.QPointF(x, y))
137-
x_pos = width - self._hl_kwargs["text"].boundingRect().width() + 21
144+
if self._hl_kwargs["text_orientation"] in [Orientation.AUTO, Orientation.HORIZONTAL]:
145+
x_pos = width - self._hl_kwargs["text"].boundingRect().width() + 21
146+
else:
147+
x_pos = width + self._hl_kwargs["text"].boundingRect().height()
138148
new_pos = vb.mapSceneToView(QtCore.QPointF(x_pos, pixel_pos.y()))
139149
self._hl_kwargs["text"].setPos(new_pos.x(), new_pos.y())
140150

0 commit comments

Comments
 (0)