Skip to content

Instantly share code, notes, and snippets.

@canavandl
Created June 1, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canavandl/e8e168541824a4a21ca51fa6f032a7f3 to your computer and use it in GitHub Desktop.
Save canavandl/e8e168541824a4a21ca51fa6f032a7f3 to your computer and use it in GitHub Desktop.
from bokeh.models import Tool, Plot, Range1d, LinearAxis, Span
from bokeh.core.properties import Instance
from bokeh.io import output_file, show
DEFAULT_OVERLAY = lambda: Span(
render_mode='css',
level="overlay",
location_units="screen",
)
class DragTool(Tool):
__implementation__ = """
_ = require "underscore"
GestureTool = require "models/tools/gestures/gesture_tool"
Span = require "models/annotations/span"
p = require "core/properties"
class DragToolView extends GestureTool.View
initialize: (options) ->
super(options)
@listenTo(@model, 'change:active', @_active_change)
_active_change: () ->
if not @mget('active')
@mget('overlay').unset('location')
_pan_start: (e) ->
return null
_pan: (e) ->
frame = @plot_model.get('frame')
canvas = @plot_view.canvas
overlay = @mget('overlay')
vx = canvas.sx_to_vx(e.bokeh.sx)
vy = canvas.sy_to_vy(e.bokeh.sy)
if not frame.contains(vx, vy)
overlay.unset('location')
return null
if overlay.get('dimension') == 'width'
overlay.set({location: vy})
else
overlay.set({location: vx})
_pan_end: (e) ->
return null
DEFAULT_OVERLAY = () -> new Span.Model({
render_mode: "css"
level: "overlay"
location_units: "screen"
})
class DragTool extends GestureTool.Model
default_view: DragToolView
type: "DragTool"
tool_name: "Drag Span"
icon: "bk-tool-icon-lasso-select"
event_type: "pan"
default_order: 12
@define {
overlay: [ p.Instance, DEFAULT_OVERLAY ]
}
module.exports =
Model: DragTool
View: DragToolView
"""
overlay = Instance(Span, default=DEFAULT_OVERLAY, help="""
Span instance to to be dragged
""")
output_file('out.html')
plot = Plot(plot_width=600, plot_height=600,
x_range=Range1d(0,10), y_range=Range1d(0,10))
plot.add_tools(DragTool())
plot.add_layout(LinearAxis(), 'below')
plot.add_layout(LinearAxis(), 'left')
show(plot)
@samirak93
Copy link

Hi. I'm trying to run this code. I'm getting an error. "RuntimeError: no such module: underscore"
Any solution? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment