如何在回调中引用散景四边形值 on-select
How to reference Bokeh quad values on-select within callback
当我使用 TapTool
单击它们时,我正在尝试从四边形中提取值
以下代码适用于 hover
工具,但不适用于 tap
。
from bokeh.plotting import figure, show
from bokeh.models import HoverTool, CustomJS, ColumnDataSource
source = generate_column_data_source()
quad_plot = generate_quad_plot(source )
code = ''' var hovered_ind = cb_data.index['1d'].indices[0];
var data = source.data
console.log(hovered_ind)
if(hovered_ind != undefined){
console.log('inside', hovered_ind)
var top = data['top'][hovered_ind]
var bottom = data['bottom'][hovered_ind]
var left = data['left'][hovered_ind]
var right = data['right'][hovered_ind]
console.log(top, bottom, left, right)
} '''
callback = CustomJS(code=code, args={'source': source})
quad_plot.add_tools(TapTool( callback=callback))
show(quad_plot)
似乎 cb_data.index['1d'].indices[0];
存在于悬停交互中,但不存在于点击选择交互中。
对于点击选择,cb_data
有一个名为 geometry
的属性,它给了我 x
、y
、vx
和 vy
点。
我认为这些参数不够精确,无法保证来自我的来源的有效 indexing/lookup 值。
有什么方法可以用 TapTool
获得如此精确的索引?
更新:在现代散景中它只是 source.selected.indices
(不再是 ['1d']
等)
可以通过 source.selected 属性访问使用 taptool 选择的字形。
回答您关于 0d、1d 和 2d 的最后评论:对于点字形,您可以通过 source.selected['0d'].indices
访问它,对于像对象“1d”这样的线,然后通过 ['2d'] 访问 multiline/patches 字形。
http://docs.bokeh.org/en/latest/docs/reference/models/sources.html(向下滚动到所选属性)
from bokeh.plotting import figure, show
from bokeh.models import HoverTool, CustomJS, ColumnDataSource, TapTool
top = [2, 3, 4]
bottom = [1, 2, 3]
left = [1, 2, 3]
right = [1.2, 2.5, 3.7]
data = {'top':top, 'bottom':bottom, 'left':left, 'right':right}
source = ColumnDataSource(data)
quad_plot = figure(plot_width=300, plot_height=300)
quad_plot.quad(top="top", bottom="bottom", left="left",
right="right",source=source, color="#B3DE69")
tap_code = """
var selected= source.selected['1d'].indices
console.log('tap, you selected:', selected)
"""
tap_callback = CustomJS(code = tap_code, args={'source': source})
quad_plot.add_tools(TapTool(callback=tap_callback))
show(quad_plot)
当我使用 TapTool
以下代码适用于 hover
工具,但不适用于 tap
。
from bokeh.plotting import figure, show
from bokeh.models import HoverTool, CustomJS, ColumnDataSource
source = generate_column_data_source()
quad_plot = generate_quad_plot(source )
code = ''' var hovered_ind = cb_data.index['1d'].indices[0];
var data = source.data
console.log(hovered_ind)
if(hovered_ind != undefined){
console.log('inside', hovered_ind)
var top = data['top'][hovered_ind]
var bottom = data['bottom'][hovered_ind]
var left = data['left'][hovered_ind]
var right = data['right'][hovered_ind]
console.log(top, bottom, left, right)
} '''
callback = CustomJS(code=code, args={'source': source})
quad_plot.add_tools(TapTool( callback=callback))
show(quad_plot)
似乎 cb_data.index['1d'].indices[0];
存在于悬停交互中,但不存在于点击选择交互中。
对于点击选择,cb_data
有一个名为 geometry
的属性,它给了我 x
、y
、vx
和 vy
点。
我认为这些参数不够精确,无法保证来自我的来源的有效 indexing/lookup 值。
有什么方法可以用 TapTool
获得如此精确的索引?
更新:在现代散景中它只是 source.selected.indices
(不再是 ['1d']
等)
可以通过 source.selected 属性访问使用 taptool 选择的字形。
回答您关于 0d、1d 和 2d 的最后评论:对于点字形,您可以通过 source.selected['0d'].indices
访问它,对于像对象“1d”这样的线,然后通过 ['2d'] 访问 multiline/patches 字形。
http://docs.bokeh.org/en/latest/docs/reference/models/sources.html(向下滚动到所选属性)
from bokeh.plotting import figure, show
from bokeh.models import HoverTool, CustomJS, ColumnDataSource, TapTool
top = [2, 3, 4]
bottom = [1, 2, 3]
left = [1, 2, 3]
right = [1.2, 2.5, 3.7]
data = {'top':top, 'bottom':bottom, 'left':left, 'right':right}
source = ColumnDataSource(data)
quad_plot = figure(plot_width=300, plot_height=300)
quad_plot.quad(top="top", bottom="bottom", left="left",
right="right",source=source, color="#B3DE69")
tap_code = """
var selected= source.selected['1d'].indices
console.log('tap, you selected:', selected)
"""
tap_callback = CustomJS(code = tap_code, args={'source': source})
quad_plot.add_tools(TapTool(callback=tap_callback))
show(quad_plot)