I am trying to use the taptool for other plot callback, I have been wrecking my head around it for a good while. I have two plots and two different sources. Find a minimal example below:
I have a 7 circles plot in a figure, I would like to be able to tap them and use them as callback to other plots. I tried with source.indices, but so far I am stuck at it.
Based on bigredot link.
https://www.py4u.net/discuss/194426
from bokeh.layouts import row
from bokeh.models import ColumnDataSource, CustomJS, TapTool
from bokeh.plotting import figure, show
import numpy as np
source_bars = ColumnDataSource({'x': [1, 2, 3], 'y': [2, 4, 1] , 'colnames': ['A', 'B', 'C']})
lines_y = [np.random.random(3) for i in range(3)]
src1 = ColumnDataSource(data=dict(x= [1, 2, 3],
y=lines_y[0],
y2=lines_y[1],
y3=lines_y[2]))
plot1 = figure(tools = 'tap')
bars = plot1.vbar(x = 'x', top = 'y', source = source_bars, bottom = 0, width = 0.5)
plot2 = figure()
lines = plot2.vbar(x = 'x', top = 'y', source = src1)
code = '''if (cb_data.source.selected.indices.length > 0){
var data = lines_y.data
var selected_index = cb_data.source.selected.indices[0];
lines.data_source.data['y'] = lines_y[selected_index]
lines.data_source.change.emit();
}'''
plots = row(plot1, plot2)
plot1.select(TapTool).callback = CustomJS(args = {'lines': lines, 'lines_y': src1}, code = code)
show(plots)
A different approach with this code, but!!!
code3 = """
var col = 'Muara_Teweh_';
// Get data from CDS
var data = source.data;
var data1 = source1.data;
var selected = source.selected.indices;
// Iterate over all entries in the CDS
for (var i=0; i<data[col].length; i++){
if(data[col][i]!=selected){
data1[selected];
}
}
}
// Save changes to CDS
source.change.emit();
source1.change.emit();
"""
Thanks.