55import pyqtgraph as pg # type: ignore
66from pyqtgraph .Qt import QtGui , QtCore # type: ignore
77
8- from pglive .kwargs import LeadingLine
8+ from pglive .kwargs import LeadingLine , Orientation
99from pglive .sources .live_plot_widget import LivePlotWidget
1010from 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