如何在callb中引用Bokeh四元值悬停

2024-04-19 18:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

当我将鼠标悬停在bokeh四元组上时,我试图从bokeh四元组中提取列数据源值。在

现在我只能在回调范围内找到两个变量。
cb_obj和{}

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

callback = CustomJS(code=code)

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

作为Javascript的新手,我在浏览和理解inspector/in-browser控制台中的cb_obj和{}输出时遇到了困难。在

我想知道如何引用当前悬停的四元曲线的值?在


Tags: logaddobjdataplotcallbackbokehcode
1条回答
网友
1楼 · 发布于 2024-04-19 18:30:25

cb_data包含与当前悬停的glyph相关的索引。然后,您可以使用它为基础数据建立索引,以达到您想要的任何目的。在

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)

相关问题 更多 >