From 435c9510b40766564d91eb488b7f583ce819a6d3 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Mon, 28 Jun 2021 16:18:12 -0500 Subject: [PATCH 1/4] add a regression test --- chaco/tools/tests/test_data_label_tool.py | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 chaco/tools/tests/test_data_label_tool.py diff --git a/chaco/tools/tests/test_data_label_tool.py b/chaco/tools/tests/test_data_label_tool.py new file mode 100644 index 000000000..fff5a477f --- /dev/null +++ b/chaco/tools/tests/test_data_label_tool.py @@ -0,0 +1,50 @@ +import unittest + +import numpy as np + +from enable.api import ComponentEditor +from enable.testing import EnableTestAssistant +from traits.api import HasTraits, Instance +from traitsui.api import Item, View + +from chaco.api import ArrayPlotData, DataLabel, Plot +from chaco.tools.api import DataLabelTool + +IMAGE = np.random.random_integers(0, 255, size=(100, 200)).astype(np.uint8) +RGB = np.dstack([IMAGE] * 3) + + +class TestDataLabelTool(unittest.TestCase, EnableTestAssistant): + + # regression test for enthought/chaco#550 + def test_use_with_2d_plot(self): + class Test2DPlot(HasTraits): + plot = Instance(Plot) + + traits_view = View( + Item('plot', editor=ComponentEditor(), show_label=False), + width=500, + height=500, + resizable=True + ) + + def _plot_default(self): + pd = ArrayPlotData(image=RGB) + plot = Plot(pd) + self.renderer = plot.img_plot('image')[0] + + label = DataLabel( + component=self.renderer, + data_point=(25, 12) + ) + plot.overlays.append(label) + tool = DataLabelTool(label) + label.tools.append(tool) + return plot + + test_2d_plot = Test2DPlot() + # should not fail + self.press_move_release( + test_2d_plot.plot, + [(0, 0), (200, 200), (300, 300)], + ) From bf17b7d19f9cceb6cff6a88a3611a95a73a07742 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Mon, 28 Jun 2021 16:19:05 -0500 Subject: [PATCH 2/4] update data_label_tool.py to make test pass --- chaco/tools/data_label_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chaco/tools/data_label_tool.py b/chaco/tools/data_label_tool.py index 2579ee6e5..06fc19419 100644 --- a/chaco/tools/data_label_tool.py +++ b/chaco/tools/data_label_tool.py @@ -71,7 +71,7 @@ def drag_start(self, event): """ if self.component: label = self.component - pointx, pointy = label.component.map_screen(label.data_point) + pointx, pointy = label.component.map_screen(label.data_point)[0] self._original_offset = (label.x - pointx, label.y - pointy) event.window.set_mouse_owner(self, event.net_transform()) event.handled = True From 49cb30e6ef17b717d23b4c35e3f03e25f4be5b07 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Mon, 28 Jun 2021 16:34:10 -0500 Subject: [PATCH 3/4] add a failing test for use of DataLabelTool with BaseXYPlot --- chaco/tools/tests/test_data_label_tool.py | 38 ++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/chaco/tools/tests/test_data_label_tool.py b/chaco/tools/tests/test_data_label_tool.py index fff5a477f..a327f0632 100644 --- a/chaco/tools/tests/test_data_label_tool.py +++ b/chaco/tools/tests/test_data_label_tool.py @@ -7,7 +7,7 @@ from traits.api import HasTraits, Instance from traitsui.api import Item, View -from chaco.api import ArrayPlotData, DataLabel, Plot +from chaco.api import ArrayPlotData, create_line_plot, DataLabel, Plot from chaco.tools.api import DataLabelTool IMAGE = np.random.random_integers(0, 255, size=(100, 200)).astype(np.uint8) @@ -48,3 +48,39 @@ def _plot_default(self): test_2d_plot.plot, [(0, 0), (200, 200), (300, 300)], ) + + def test_use_with_xy_plot(self): + class TestXYPlot(HasTraits): + plot = Instance(Plot) + + traits_view = View( + Item('plot', editor=ComponentEditor(), show_label=False), + width=500, + height=500, + resizable=True + ) + + def _plot_default(self): + values = np.arange(10) + plotdata = ArrayPlotData(x=values, y=values) + + plot = Plot(plotdata) + self.renderer = plot.plot( + ("x", "y"), type="line", color="blue" + )[0] + + label = DataLabel( + component=self.renderer, + data_point=(5, 5) + ) + plot.overlays.append(label) + tool = DataLabelTool(label) + label.tools.append(tool) + return plot + + test_xy_plot = TestXYPlot() + # should not fail + self.press_move_release( + test_xy_plot.plot, + [(0, 0), (200, 200), (300, 300)], + ) From 9b73891ee33252a87be37d74de72f56145b1a03e Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Mon, 28 Jun 2021 16:37:23 -0500 Subject: [PATCH 4/4] remove unused import --- chaco/tools/tests/test_data_label_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chaco/tools/tests/test_data_label_tool.py b/chaco/tools/tests/test_data_label_tool.py index a327f0632..c4deabb85 100644 --- a/chaco/tools/tests/test_data_label_tool.py +++ b/chaco/tools/tests/test_data_label_tool.py @@ -7,7 +7,7 @@ from traits.api import HasTraits, Instance from traitsui.api import Item, View -from chaco.api import ArrayPlotData, create_line_plot, DataLabel, Plot +from chaco.api import ArrayPlotData, DataLabel, Plot from chaco.tools.api import DataLabelTool IMAGE = np.random.random_integers(0, 255, size=(100, 200)).astype(np.uint8)