Skip to content

Commit

Permalink
Handle invert_xaxis and invert_yaxis
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmmease committed Sep 3, 2019
1 parent d5ea54b commit 9fd4b60
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions holoviews/element/chart.py
Expand Up @@ -63,6 +63,13 @@ def _get_selection_expr_for_stream_value(self, **kwargs):

if kwargs.get('bounds', None):
x0, y0, x1, y1 = kwargs['bounds']

# Handle invert_xaxis/invert_yaxis
if y0 > y1:
y0, y1 = y1, y0
if x0 > x1:
x0, x1 = x1, x0

if invert_axes:
ydim = self.kdims[0]
xdim = self.vdims[0]
Expand Down Expand Up @@ -281,6 +288,12 @@ def _get_selection_expr_for_stream_value(self, **kwargs):
else:
x0, y0, x1, y1 = kwargs['bounds']

# Handle invert_xaxis/invert_yaxis
if y0 > y1:
y0, y1 = y1, y0
if x0 > x1:
x0, x1 = x1, x0

xdim = self.kdims[0]
ydim = self.vdims[0]

Expand Down
6 changes: 6 additions & 0 deletions holoviews/element/geom.py
Expand Up @@ -42,6 +42,12 @@ def _get_selection_expr_for_stream_value(self, **kwargs):
if kwargs.get('bounds', None):
x0, y0, x1, y1 = kwargs['bounds']

# Handle invert_xaxis/invert_yaxis
if y0 > y1:
y0, y1 = y1, y0
if x0 > x1:
x0, x1 = x1, x0

if invert_axes:
ydim, xdim = self.kdims[:2]
else:
Expand Down
64 changes: 64 additions & 0 deletions holoviews/tests/teststreams.py
Expand Up @@ -958,6 +958,36 @@ def test_selection_expr_stream_invert_axes(self):
{'y': (1, 3), 'x': (1, 4)}
)

def test_selection_expr_stream_invert_xaxis_yaxis(self):
for element_type in [Scatter, Points, Curve]:
print(element_type)
# Create SelectionExpr on element
element = element_type(([1, 2, 3], [1, 5, 10])).opts(
invert_xaxis=True,
invert_yaxis=True,
)
expr_stream = SelectionExpr(element)

# Check stream properties
self.assertEqual(len(expr_stream._source_streams), 1)
self.assertIsInstance(expr_stream._source_streams[0], BoundsXY)
self.assertIsNone(expr_stream.bbox)
self.assertIsNone(expr_stream.selection_expr)

# Simulate interactive update by triggering source stream
expr_stream._source_streams[0].event(bounds=(3, 4, 1, 1))

# Check SelectionExpr values
self.assertEqual(
repr(expr_stream.selection_expr),
repr((dim('x') >= 1) & (dim('x') <= 3) &
(dim('y') >= 1) & (dim('y') <= 4))
)
self.assertEqual(
expr_stream.bbox,
{'x': (1, 3), 'y': (1, 4)}
)

def test_selection_expr_stream_hist(self):
# Create SelectionExpr on element
hist = Histogram(([1, 2, 3, 4, 5], [1, 5, 2, 3, 7]))
Expand Down Expand Up @@ -1021,3 +1051,37 @@ def test_selection_expr_stream_hist_invert_axes(self):
(dim('x') == hist.edges[-1]))
)
self.assertEqual(expr_stream.bbox, {'x': (2.5, 5.5)})

def test_selection_expr_stream_hist_invert_xaxis_yaxis(self):
# Create SelectionExpr on element
hist = Histogram(([1, 2, 3, 4, 5], [1, 5, 2, 3, 7])).opts(
invert_xaxis=True,
invert_yaxis=True,
)
expr_stream = SelectionExpr(hist)

# Check stream properties
self.assertEqual(len(expr_stream._source_streams), 1)
self.assertIsInstance(expr_stream._source_streams[0], BoundsXY)
self.assertIsNone(expr_stream.bbox)
self.assertIsNone(expr_stream.selection_expr)

# Simulate interactive update by triggering source stream.
# Select second and forth bar.
expr_stream._source_streams[0].event(bounds=(4.6, 6, 1.5, 2.5))
self.assertEqual(
repr(expr_stream.selection_expr),
repr(dim('x').digitize(hist.edges).isin([2, 4]))
)
self.assertEqual(expr_stream.bbox, {'x': (1.5, 4.5)})

# Select third, forth, and fifth bar. Make sure there is special
# handling when last bar is selected to include values exactly on the
# upper edge in the selection
expr_stream._source_streams[0].event(bounds=(8, 10, 2.5, -10))
self.assertEqual(
repr(expr_stream.selection_expr),
repr((dim('x').digitize(hist.edges).isin([3, 4, 5])) |
(dim('x') == hist.edges[-1]))
)
self.assertEqual(expr_stream.bbox, {'x': (2.5, 5.5)})

0 comments on commit 9fd4b60

Please sign in to comment.