Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CursorTool issue with LogMapper #803

Merged
merged 8 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chaco/linear_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion chaco/plots/lineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def hittest(self, screen_pt, threshold=7.0, return_distance=False):
# screen_pt is one of the points in the lineplot
data_pt = (self.index.get_data()[ndx], self.value.get_data()[ndx])
if return_distance:
scrn_pt = self.map_screen(data_pt)
scrn_pt = self.map_screen(data_pt)[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this change is also made in #802

Both the changes to BaseXYPlot1.map_screen done there and LinearMapper.map_screen here can cause the return type to be an an array of shape (N,2).

dist = sqrt(
(screen_pt[0] - scrn_pt[0]) ** 2
+ (screen_pt[1] - scrn_pt[1]) ** 2
Expand Down
4 changes: 2 additions & 2 deletions chaco/tools/cursor_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
87 changes: 87 additions & 0 deletions chaco/tools/tests/test_cursor_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import unittest
aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved

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
)