@@ -693,14 +693,6 @@ def create_shape(self):
693
693
shape = self .SHAPE_CLASS (0 , 0 )
694
694
return shape
695
695
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
-
704
696
def get_info (self ) -> str :
705
697
"""Get informations on current shape
706
698
@@ -741,6 +733,24 @@ def hit_test(self, pos: QPointF) -> tuple[float, float, bool, None]:
741
733
return self .shape .hit_test (pos )
742
734
743
735
# ----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
+
744
754
def attach (self , plot ):
745
755
"""
746
756
Attach the item to a plot.
@@ -758,6 +768,7 @@ def attach(self, plot):
758
768
"""
759
769
super ().attach (plot )
760
770
self .shape .attach (plot )
771
+ self .set_label_position ()
761
772
762
773
763
774
class AnnotatedXRange (BaseAnnotatedRangeSelection ):
@@ -785,6 +796,17 @@ def __init__(
785
796
) -> None :
786
797
super ().__init__ (_min , _max , annotationparam , info_callback )
787
798
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
+
788
810
789
811
class AnnotatedYRange (BaseAnnotatedRangeSelection ):
790
812
"""
@@ -811,6 +833,17 @@ def __init__(
811
833
) -> None :
812
834
super ().__init__ (_min , _max , annotationparam , info_callback )
813
835
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
+
814
847
815
848
class AnnotatedPolygon (AnnotatedShape ):
816
849
"""
0 commit comments