Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion chaco/barplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,17 @@ def map_screen(self, data_array):
an array.

Implements the AbstractPlotRenderer interface.

Parameters
----------
data_array: array of shape (N, 2)

Returns
-------
array of shape (N, 2)
"""
# data_array is Nx2 array
if len(data_array) == 0:
if data_array.size == 0:
return []
x_ary, y_ary = transpose(data_array)
sx = self.index_mapper.map_screen(x_ary)
Expand Down
4 changes: 2 additions & 2 deletions chaco/base_1d_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def map_screen(self, data_array):

"""
# data_array is 1D array of length N
if len(data_array) == 0:
return []
if data_array.size == 0:
return np.empty(shape=(0,))
return asarray(self.index_mapper.map_screen(data_array))

def map_data(self, screen_pts):
Expand Down
4 changes: 2 additions & 2 deletions chaco/base_2d_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def map_screen(self, data_pts):
Implements the AbstractPlotRenderer interface.
"""
# data_pts is Nx2 array
if len(data_pts) == 0:
return []
if data_pts.shape == 0:
return np.empty(shape=(0,))
return asarray(self.index_mapper.map_screen(data_pts))

def map_data(self, screen_pts):
Expand Down
10 changes: 9 additions & 1 deletion chaco/base_xy_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,17 @@ def map_screen(self, data_array):
an array.

Implements the AbstractPlotRenderer interface.

Parameters
----------
data_array: array of shape (N, 2)

Returns
-------
array of shape (N, 2)
"""
# data_array is Nx2 array
if len(data_array) == 0:
if data_array.size == 0:
return []

x_ary, y_ary = transpose(data_array)
Expand Down
19 changes: 19 additions & 0 deletions chaco/grid_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def map_screen(self, data_pts):
"""map_screen(data_pts) -> screen_array

Maps values from data space into screen space.

Parameters
----------
data_pts: array of shape (N, 2)

Returns
-------
array of shape (N, 2)
"""
xs, ys = transpose(data_pts)
screen_xs = self._xmapper.map_screen(xs)
Expand All @@ -126,6 +134,14 @@ def map_data(self, screen_pts):
"""map_data(screen_pts) -> data_vals

Maps values from screen space into data space.

Parameters
----------
screen_pts: array of shape (N, 2)

Returns
-------
array of shape (N, 2)
"""
screen_xs, screen_ys = transpose(screen_pts)
xs = self._xmapper.map_data(screen_xs)
Expand All @@ -134,6 +150,9 @@ def map_data(self, screen_pts):
return data_pts

def map_data_array(self, screen_pts):
""" Since map_data is already vectorized, this is equivalent to
map_data.
"""
return self.map_data(screen_pts)

# ------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion chaco/horizon_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def map_screen(self, data_array):
return array([self.low_pos])
else:
# Scale the data by the number of bands
if not isinstance(data_array, ndarray):
data_array = array(data_array, ndmin=1)
return (
data_array * self.bands - self.range.low
) * self._scale + self.low_pos
Expand Down Expand Up @@ -74,7 +76,7 @@ def _render(self, gc, points):
if len(points) == 0:
return

ox, oy = self.map_screen([[0, 0]])[0]
ox, oy = self.map_screen(np.array([[0, 0]]))[0]
ylow, yhigh = self.value_mapper.screen_bounds

y_plus_height = yhigh - oy
Expand Down
4 changes: 2 additions & 2 deletions chaco/image_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def _calc_virtual_screen_bbox(self):
(lower_left, upper_right) = self.index.get_bounds()
# ... but if the origin is not 'bottom left', the data-to-screen
# mapping will flip min and max values.
x_min, y_min = self.map_screen([lower_left])[0]
x_max, y_max = self.map_screen([upper_right])[0]
x_min, y_min = self.map_screen(np.array([lower_left]))[0]
x_max, y_max = self.map_screen(np.array([upper_right]))[0]
if x_min > x_max:
x_min, x_max = x_max, x_min
if y_min > y_max:
Expand Down
6 changes: 3 additions & 3 deletions chaco/linear_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 All @@ -57,9 +59,7 @@ def map_data(self, screen_val):
Overrides AbstractMapper. Maps values from screen space into data space.
"""
self._compute_scale()
if self._null_screen_range:
return array([self.range.low])
elif self._null_data_range:
if self._null_screen_range or self._null_data_range:
return array([self.range.low])
else:
return (screen_val - self.low_pos) / self._scale + self.range.low
Expand Down
13 changes: 7 additions & 6 deletions chaco/lineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,22 @@ def hittest(self, screen_pt, threshold=7.0, return_distance=False):
any data points on the line. If so, then it returns the (x,y) value of
a data point near the screen point. If not, then it returns None.
"""

# First, check screen_pt is directly on a point in the lineplot
ndx = self.map_index(screen_pt, threshold)
if ndx is not None:
# screen_pt is one of the points in the lineplot
data_pt = (self.index.get_data()[ndx], self.value.get_data()[ndx])
data_pt = array(
[[self.index.get_data()[ndx], self.value.get_data()[ndx]]]
)
if return_distance:
scrn_pt = self.map_screen(data_pt)
dist = sqrt(
(screen_pt[0] - scrn_pt[0]) ** 2
+ (screen_pt[1] - scrn_pt[1]) ** 2
(screen_pt[0] - scrn_pt[0, 0]) ** 2
+ (screen_pt[1] - scrn_pt[0, 1]) ** 2
)
return (data_pt[0], data_pt[1], dist)
return (data_pt[0, 0], data_pt[0, 1], dist)
else:
return data_pt
return (data_pt[0, 0], data_pt[0, 1])
else:
# We now must check the lines themselves

Expand Down
22 changes: 11 additions & 11 deletions chaco/tools/cursor_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


# Major library imports
import numpy
import numpy as np

# Enthought library imports
from enable.api import CircleMarker
Expand Down Expand Up @@ -190,7 +190,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(np.array([self.current_position]))[0]
orientation = plot.orientation

if orientation == "h" and sx is not None:
Expand All @@ -211,10 +211,10 @@ 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)
if orientation == "h" and numpy.abs(sx - x) <= self.threshold:
sx, sy = plot.map_screen(np.array([self.current_position]))[0]
if orientation == "h" and np.abs(sx - x) <= self.threshold:
return True
elif orientation == "v" and numpy.abs(sy - y) <= self.threshold:
elif orientation == "v" and np.abs(sy - y) <= self.threshold:
return True
return False

Expand Down Expand Up @@ -258,17 +258,17 @@ 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])[0]
sx, sy = plot.map_screen(np.array([self.current_position]))[0]
self._dragV = self._dragH = False
if orientation == "h":
if numpy.abs(sx - x) <= self.threshold:
if np.abs(sx - x) <= self.threshold:
self._dragH = True
if numpy.abs(sy - y) <= self.threshold:
if np.abs(sy - y) <= self.threshold:
self._dragV = True
else:
if numpy.abs(sx - x) <= self.threshold:
if np.abs(sx - x) <= self.threshold:
self._dragV = True
if numpy.abs(sy - y) <= self.threshold:
if np.abs(sy - y) <= self.threshold:
self._dragH = True
return self._dragV or self._dragH
return False
Expand All @@ -284,7 +284,7 @@ def draw(self, gc, view_bounds=None):
plot = self.component
if plot is None:
return
sx, sy = plot.map_screen([self.current_position])[0]
sx, sy = plot.map_screen(np.array([self.current_position]))[0]
orientation = plot.orientation

if orientation == "h":
Expand Down
2 changes: 1 addition & 1 deletion chaco/tools/data_label_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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(np.array(label.data_point))
self._original_offset = (label.x - pointx, label.y - pointy)
event.window.set_mouse_owner(self, event.net_transform())
event.handled = True
Expand Down
28 changes: 22 additions & 6 deletions examples/demo/cursor_tool_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

# Enthought library imports
from chaco.api import (
ArrayDataSource,
LinePlot,
LogMapper,
DataRange1D,
create_line_plot,
OverlayPlotContainer,
HPlotContainer,
Expand Down Expand Up @@ -52,12 +56,24 @@ def __init__(self):
value = numpy.sin(index)

# create a LinePlot instance and add it to the subcontainer
line = create_line_plot(
[index, value],
add_grid=True,
add_axis=True,
index_sort="ascending",
orientation="h",
#line = create_line_plot(
## [index, value],
# add_grid=True,
# add_axis=True,
# index_sort="ascending",
# orientation="h",
#)
line = LinePlot(
index=ArrayDataSource(index, sort_order='ascending'),
value=ArrayDataSource(value),
index_mapper=LogMapper(range=DataRange1D()),
value_mapper=LogMapper(range=DataRange1D()),
orientation='h',
color='red',
bgcolor='transparent',
line_width=1.0,
line_style='solid',
border_visible=False,
)
subcontainer.add(line)

Expand Down