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

Miscellaneous stream improvements #1436

Merged
merged 6 commits into from
May 15, 2017
Merged
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
56 changes: 51 additions & 5 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from ...core import OrderedDict
from ...streams import (Stream, PointerXY, RangeXY, Selection1D, RangeX,
RangeY, PointerX, PointerY, Bounds, Tap,
DoubleTap, MouseEnter, MouseLeave, PlotSize)
RangeY, PointerX, PointerY, Bounds, Tap, SingleTap,
DoubleTap, MouseEnter, MouseLeave, PlotSize, Draw)
from ...streams import PositionX, PositionY, PositionXY # Deprecated: remove in 2.0
from ..comms import JupyterCommJS
from .util import bokeh_version
Expand Down Expand Up @@ -519,7 +519,22 @@ class PointerXYCallback(Callback):

attributes = {'x': 'cb_obj.x', 'y': 'cb_obj.y'}
models = ['plot']
extra_models= ['x_range', 'y_range']

on_events = ['mousemove']
# Clip x and y values to available axis range
code = """
if (x_range.type.endsWith('Range1d')) {
if (cb_obj.x < x_range.start) {
data['x'] = x_range.start }
else if (cb_obj.x > x_range.end) {
data['x'] = x_range.end }}
if (y_range.type.endsWith('Range1d')) {
if (cb_obj.y < y_range.start) {
data['y'] = y_range.start }
else if (cb_obj.y > y_range.end) {
data['y'] = y_range.end }}
"""


class PointerXCallback(PointerXYCallback):
Expand All @@ -538,9 +553,38 @@ class PointerYCallback(PointerXYCallback):
attributes = {'y': 'cb_obj.y'}


class DrawCallback(PointerXYCallback):
on_events = ['pan', 'panstart', 'panend']
models = ['plot']
extra_models=['pan', 'box_zoom', 'x_range', 'y_range']
skip = ['pan && pan.attributes.active', 'box_zoom && box_zoom.attributes.active']
attributes = {'x': 'cb_obj.x', 'y': 'cb_obj.y', 'event': 'cb_obj.event_name'}

def __init__(self, *args, **kwargs):
self.stroke_count = 0
super(DrawCallback, self).__init__(*args, **kwargs)

def _process_msg(self, msg):
event = msg.pop('event')
if event == 'panend':
self.stroke_count += 1
return dict(msg, stroke_count=self.stroke_count)


class TapCallback(PointerXYCallback):
"""
Returns the mouse x/y-position on tap event.

Note: As of bokeh 0.12.5, there is no way to distinguish the
individual tap events within a doubletap event.
"""

on_events = ['tap', 'doubletap']


class SingleTapCallback(PointerXYCallback):
"""
Returns the mouse x/y-position on tap event.
"""

on_events = ['tap']
Expand Down Expand Up @@ -680,10 +724,11 @@ def _process_msg(self, msg):

callbacks = Stream._callbacks['bokeh']

callbacks[PointerXY] = PointerXYCallback
callbacks[PointerX] = PointerXCallback
callbacks[PointerY] = PointerYCallback
callbacks[PointerXY] = PointerXYCallback
callbacks[PointerX] = PointerXCallback
callbacks[PointerY] = PointerYCallback
callbacks[Tap] = TapCallback
callbacks[SingleTap] = SingleTapCallback
callbacks[DoubleTap] = DoubleTapCallback
callbacks[MouseEnter] = MouseEnterCallback
callbacks[MouseLeave] = MouseLeaveCallback
Expand All @@ -693,6 +738,7 @@ def _process_msg(self, msg):
callbacks[Bounds] = BoundsCallback
callbacks[Selection1D] = Selection1DCallback
callbacks[PlotSize] = PlotSizeCallback
callbacks[Draw] = DrawCallback

# Aliases for deprecated streams
callbacks[PositionXY] = PointerXYCallback
Expand Down
16 changes: 16 additions & 0 deletions holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,22 @@ class PointerXY(LinkedStream):
Pointer position along the y-axis in data coordinates""")


class Draw(PointerXY):
"""
A series of updating x/y-positions when drawing, together with the
current stroke count
"""

stroke_count = param.Integer(default=0, constant=True, doc="""
The current drawing stroke count. Increments every time a new
stroke is started.""")

class SingleTap(PointerXY):
"""
The x/y-position of a single tap or click in data coordinates.
"""


class Tap(PointerXY):
"""
The x/y-position of a tap or click in data coordinates.
Expand Down