5
5
import pyqtgraph as pg # type: ignore
6
6
from pyqtgraph .Qt import QtGui , QtCore # type: ignore
7
7
8
- from pglive .kwargs import LeadingLine
8
+ from pglive .kwargs import LeadingLine , Orientation
9
9
from pglive .sources .live_plot_widget import LivePlotWidget
10
10
from pglive .sources .utils import NUM_LIST
11
11
@@ -74,7 +74,8 @@ class MixinLeadingLine(SupportsLivePlot):
74
74
_vl_kwargs = None
75
75
76
76
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 :
78
79
text_axis = text_axis .lower ()
79
80
assert text_axis in (LeadingLine .AXIS_X , LeadingLine .AXIS_Y )
80
81
@@ -84,19 +85,21 @@ def set_leading_line(self, orientation: str = LeadingLine.VERTICAL,
84
85
pen = self .opts .get ("pen" )
85
86
if orientation == LeadingLine .VERTICAL :
86
87
_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 ())
88
90
_v_leading_line .setZValue (999 )
89
91
_v_leading_text .setZValue (999 )
90
92
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 }
92
94
return self ._vl_kwargs
93
95
elif orientation == LeadingLine .HORIZONTAL :
94
96
_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 ())
96
99
_h_leading_text .setZValue (999 )
97
100
_h_leading_text .setZValue (999 )
98
101
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 }
100
103
return self ._hl_kwargs
101
104
else :
102
105
raise TypeError ("Unsupported LeadingLine type" )
@@ -127,14 +130,21 @@ def update_leading_text(self, x: float, y: float, x_text: Optional[str] = None,
127
130
self ._vl_kwargs ["text" ].setText (text_axis )
128
131
pixel_pos = vb .mapViewToScene (QtCore .QPointF (x , y ))
129
132
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 ))
131
138
self ._vl_kwargs ["text" ].setPos (new_pos .x (), new_pos .y ())
132
139
133
140
if self ._hl_kwargs is not None :
134
141
text_axis = x_text if self ._hl_kwargs ["text_axis" ] == LeadingLine .AXIS_X else y_text
135
142
self ._hl_kwargs ["text" ].setText (text_axis )
136
143
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 ()
138
148
new_pos = vb .mapSceneToView (QtCore .QPointF (x_pos , pixel_pos .y ()))
139
149
self ._hl_kwargs ["text" ].setPos (new_pos .x (), new_pos .y ())
140
150
0 commit comments