diff --git a/chaco/linear_mapper.py b/chaco/linear_mapper.py index 9ee446def..1b5a7e150 100644 --- a/chaco/linear_mapper.py +++ b/chaco/linear_mapper.py @@ -59,6 +59,8 @@ def map_screen(self, data_array): else: return array([self.low_pos]) else: + if not isinstance(data_array, ndarray): + data_array = array(data_array, ndmin=1) return (data_array - self.range.low) * self._scale + self.low_pos def map_data(self, screen_val): diff --git a/chaco/tools/cursor_tool.py b/chaco/tools/cursor_tool.py index 3bdfb499d..8eda0728c 100644 --- a/chaco/tools/cursor_tool.py +++ b/chaco/tools/cursor_tool.py @@ -200,7 +200,7 @@ def draw(self, gc, view_bounds=None): if plot is None: return - sx, sy = plot.map_screen(self.current_position) + sx, sy = plot.map_screen(self.current_position)[0] orientation = plot.orientation if orientation == "h" and sx is not None: @@ -221,7 +221,7 @@ def is_draggable(self, x, y): plot = self.component if plot is not None: orientation = plot.orientation - sx, sy = plot.map_screen(self.current_position) + sx, sy = plot.map_screen(self.current_position)[0] if orientation == "h" and numpy.abs(sx - x) <= self.threshold: return True elif orientation == "v" and numpy.abs(sy - y) <= self.threshold: diff --git a/chaco/tools/tests/test_cursor_tool.py b/chaco/tools/tests/test_cursor_tool.py new file mode 100644 index 000000000..7f7a443da --- /dev/null +++ b/chaco/tools/tests/test_cursor_tool.py @@ -0,0 +1,96 @@ +# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! +import unittest + +import numpy as np + +from enable.api import ComponentEditor +from enable.testing import EnableTestAssistant +from traits.api import HasTraits, Instance +from traits.etsconfig.api import ETSConfig +from traitsui.api import Item, View +from traitsui.testing.api import UITester + +from chaco.api import ArrayPlotData, Plot +from chaco.tools.cursor_tool import CursorTool + + +class TestCursorTool(unittest.TestCase, EnableTestAssistant): + + # regression test for enthought/chaco#289 + @unittest.skipIf(ETSConfig.toolkit == "null", "Skip on 'null' toolkit") + def test_use_with_log_mappers(self): + class TestCursor(HasTraits): + plot = Instance(Plot) + + traits_view = View( + Item('plot', editor=ComponentEditor(), show_label=False), + width=500, + height=500, + resizable=True + ) + + def _plot_default(self): + arr = np.logspace(0, 10, num=10) + data = ArrayPlotData(x=arr, y=arr) + plot = Plot(data) + renderer = plot.plot( + ("x", "y"), + index_scale="log", + value_scale="log" + )[0] + + cursor = CursorTool(renderer) + + renderer.overlays.append(cursor) + + return plot + + tester = UITester() + test_cursor = TestCursor() + with tester.create_ui(test_cursor): + # should not fail + self.mouse_move( + test_cursor.plot, 10, 10 + ) + + @unittest.skipIf(ETSConfig.toolkit == "null", "Skip on 'null' toolkit") + def test_use_with_linear_mappers(self): + class TestCursor(HasTraits): + plot = Instance(Plot) + + traits_view = View( + Item('plot', editor=ComponentEditor(), show_label=False), + width=500, + height=500, + resizable=True + ) + + def _plot_default(self): + arr = np.logspace(0, 10, num=10) + data = ArrayPlotData(x=arr, y=arr) + plot = Plot(data) + renderer = plot.plot( + ("x", "y"), + )[0] + + cursor = CursorTool(renderer) + + renderer.overlays.append(cursor) + + return plot + + tester = UITester() + test_cursor = TestCursor() + with tester.create_ui(test_cursor): + # should not fail + self.mouse_move( + test_cursor.plot, 10, 10 + )