I am developing an interactive bokeh application (0.12.2) that updates graphics based on specific interactions.
I am currently using sliders to change the position of the glyph in the plot, but I really want to access the position of my mouse in a specific plot.
A data set is a multidimensional matrix (tensor), dense data, and each graph displays one dimension in a specific place. If I changed the position of the marker glyph on one chart, the other charts need to be updated, which means that I have to cut my data set in accordance with the updated position.
Here is a simple example: I tried to get mouse data in my bokeh server update function using the freeze tool:
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import CustomJS, HoverTool
from bokeh.io import curdoc
s = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1]))
callback = CustomJS(args=dict(s=s), code="""
var geometry = cb_data['geometry'];
var mouse_x = geometry.x;
var mouse_y = geometry.y;
var x = s.get('data')['x'];
var y = s.get('data')['y'];
x[0] = mouse_x;
y[0] = mouse_y;
s.trigger('change');
""")
hover_tool = HoverTool(callback=callback)
p = figure(x_range=(0, 1), y_range=(0, 1), tools=[hover_tool])
p.circle(x='x', y='y', source=s)
def update():
print s.data
curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)
Unfortunately, the server only displays:
{'y': [0, 1], 'x': [0, 1]}
{'y': [0, 1], 'x': [0, 1]}
{'y': [0, 1], 'x': [0, 1]}
{'y': [0, 1], 'x': [0, 1]}
( python)? ( javascript).
EDIT. , tool_events.on_change(), . , TapTool, LassoSelectTool BoxSelectTool, HoverTool:
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.tools import BoxSelectTool, TapTool, HoverTool, LassoSelectTool
from bokeh.models.ranges import Range1d
TOOLS = [TapTool(), LassoSelectTool(), BoxSelectTool(), HoverTool()]
p = figure(tools=TOOLS,
x_range=Range1d(start=0.0, end=10.0),
y_range=Range1d(start=0.0, end=10.0))
def tool_events_callback(attr, old, new):
print attr, 'callback', new
p.tool_events.on_change('geometries', tool_events_callback)
curdoc().add_root(p)
, : ColumnDataSource, Bokeh CustomJS?. , pan tool_events. (TapTool) (Lasso/BoxSelectTool). .