如何在回调中引用Bokeh quad值

时间:2017-05-26 16:02:56

标签: python bokeh

当我将鼠标悬停在它上面时,我正试图从散景四边形中提取列数据源值。

现在我只能在回调范围内找到两个变量 cb_objcb_data

code = """
    console.log(cb_obj);
    console.log(cb_data)
""" 

callback = CustomJS(code=code)

quad_plot.add_tools(HoverTool(tooltips=None, callback=callback))  

相对较新的Javascript,我无法在检查器/浏览器控制台中遍历和理解cb_objcb_data输出。

我想知道如何引用我正在徘徊的四边形的值?

1 个答案:

答案 0 :(得分:2)

cb_data包含与当前悬停字形相关的索引。然后,您可以使用它来为您想要的目的索引基础数据。

from bokeh.plotting import figure, show
from bokeh.models import HoverTool, CustomJS, ColumnDataSource

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")

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(HoverTool(tooltips=None, callback=callback))  

show(quad_plot)