两个交互式散景图(II - 与JSCallback)

问题描述 投票:0回答:1

所以,我只是将优秀的帖子Two interactive bokeh plots: select a value in one graph and change the other与另外的问题联系起来,如果/如何独立完成(没有Bokeh服务器),即仅使用JSCallbacks?

非常感激!

javascript python callback bokeh
1个回答
0
投票

这是独立文档的JS回调版本(在Bokeh 1.0.4上测试):

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(5) for i in range(3)]

plot1 = figure(tools = 'tap')
bars = plot1.vbar(x = 'x', top = 'y', source = source_bars, bottom = 0, width = 0.5)

plot2 = figure()
lines = plot2.line(x = 'x', y = 'y', source = ColumnDataSource({'x': np.arange(5), 'y': lines_y[0]}))
lines.visible = False

code = '''if (cb_data.source.selected.indices.length > 0){
            lines.visible = true;
            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': lines_y}, code = code)
show(plots)

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.