@@ -693,14 +693,6 @@ def create_shape(self):
693693 shape = self .SHAPE_CLASS (0 , 0 )
694694 return shape
695695
696- def set_label_position (self ) -> None :
697- """Set label position, for instance based on shape position"""
698- _min , _max = self .get_range ()
699- x , y = 0.5 * (_min + _max ), 0
700- if isinstance (self .shape , YRangeSelection ):
701- x , y = y , x
702- self .label .set_pos (x , y )
703-
704696 def get_info (self ) -> str :
705697 """Get informations on current shape
706698
@@ -741,6 +733,24 @@ def hit_test(self, pos: QPointF) -> tuple[float, float, bool, None]:
741733 return self .shape .hit_test (pos )
742734
743735 # ----QwtPlotItem API--------------------------------------------------------
736+ def draw (
737+ self ,
738+ painter : QPainter ,
739+ xMap : qwt .scale_map .QwtScaleMap ,
740+ yMap : qwt .scale_map .QwtScaleMap ,
741+ canvasRect : QRectF ,
742+ ) -> None :
743+ """Draw the item
744+
745+ Args:
746+ painter: Painter
747+ xMap: X axis scale map
748+ yMap: Y axis scale map
749+ canvasRect: Canvas rectangle
750+ """
751+ self .set_label_position ()
752+ super ().draw (painter , xMap , yMap , canvasRect )
753+
744754 def attach (self , plot ):
745755 """
746756 Attach the item to a plot.
@@ -758,6 +768,7 @@ def attach(self, plot):
758768 """
759769 super ().attach (plot )
760770 self .shape .attach (plot )
771+ self .set_label_position ()
761772
762773
763774class AnnotatedXRange (BaseAnnotatedRangeSelection ):
@@ -785,6 +796,17 @@ def __init__(
785796 ) -> None :
786797 super ().__init__ (_min , _max , annotationparam , info_callback )
787798
799+ # ----AnnotatedShape API-----------------------------------------------------
800+ def set_label_position (self ) -> None :
801+ """Set label position, for instance based on shape position"""
802+ plot = self .plot ()
803+ if plot is not None :
804+ x0 , x1 , y = self .shape .get_handles_pos ()
805+ x = 0.5 * (x0 + x1 )
806+ x = plot .invTransform (self .xAxis (), x )
807+ y = plot .invTransform (self .yAxis (), y )
808+ self .label .set_pos (x , y )
809+
788810
789811class AnnotatedYRange (BaseAnnotatedRangeSelection ):
790812 """
@@ -811,6 +833,17 @@ def __init__(
811833 ) -> None :
812834 super ().__init__ (_min , _max , annotationparam , info_callback )
813835
836+ # ----AnnotatedShape API-----------------------------------------------------
837+ def set_label_position (self ) -> None :
838+ """Set label position, for instance based on shape position"""
839+ plot = self .plot ()
840+ if plot is not None :
841+ y0 , y1 , x = self .shape .get_handles_pos ()
842+ y = 0.5 * (y0 + y1 )
843+ x = plot .invTransform (self .xAxis (), x )
844+ y = plot .invTransform (self .yAxis (), y )
845+ self .label .set_pos (x , y )
846+
814847
815848class AnnotatedPolygon (AnnotatedShape ):
816849 """
0 commit comments