Skip to content

Commit 8895222

Browse files
committed
Fix label positioning in range selection items and enhance drawing logic
1 parent a050595 commit 8895222

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

plotpy/items/annotation.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

763774
class 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

789811
class 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

815848
class AnnotatedPolygon(AnnotatedShape):
816849
"""

plotpy/tests/items/test_annotated_range.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
def test_annotated_range_selection():
1818
"""Test AnnotatedXRange and AnnotatedYRange items"""
19-
x = np.linspace(-10, 10, 200)
20-
y = np.sin(np.sin(np.sin(x)))
19+
x = np.linspace(-5, 15, 200)
20+
y = np.sin(np.sin(np.sin(x))) + 0.5
2121
with qt_app_context(exec_loop=True):
2222
items = [
2323
make.curve(x, y, color="b"),
24-
make.annotated_xrange(x0=-7.5, x1=-2.5, title="X Range Selection"),
25-
make.annotated_yrange(y0=-0.5, y1=-0.2, title="Y Range Selection"),
24+
make.annotated_xrange(x0=-5, x1=0.0, title="X Range Selection"),
25+
make.annotated_yrange(y0=-0.2, y1=0.1, title="Y Range Selection"),
2626
]
2727
_win = ptv.show_items(
2828
items,

0 commit comments

Comments
 (0)