Bokeh:如何在 HoverTool 中使用自定义回调访问一行的索引?

Bokeh: How to access Index with custom callback in HoverTool for a line?

我尝试为一行创建 JSCallback 并访问它的索引。
对于 circle() 字形,如果我精确地将鼠标悬停在点上,这将起作用。在这种情况下,悬停注释有效并且 cb_data['index'].indices 与悬停注释显示的值相同。但是,对于 line()mode='vline',这不起作用。即使内部悬停注释显示索引,cb_data['index'].indices 也是空的。
下面是一个最小的工作示例,在浏览器的控制台日志中显示了这种情况:

import pandas as pd

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource, HoverTool, CustomJS
from bokeh.layouts import layout

plot1 = figure(plot_width=1000, plot_height=250)
plot2 = figure(plot_width=1000, plot_height=250)

df = pd.DataFrame({"ID":[0, 1, 2, 3, 4, 5, 6, 7], 
                   "Value1":[0, 100, 200, 300, 400, 500, 600, 700], 
                   "Value2":[0, 1, 2, 4,8 , 16, 32, 64]})
source = ColumnDataSource(df)

line = plot1.line(x='ID', y='Value1', source=source)
circle = plot2.circle(x='ID', y='Value2', source=source)

callback = CustomJS(args=dict(source=source), 
                        code="""
                        console.log("Indices: "+ cb_data['index'].indices);
                        console.log("Mouse X: "+ cb_data['geometry'].x);
                        console.log("Mouse Y: "+ cb_data['geometry'].y);
                        """)

hover_tool_plot = HoverTool(mode='vline', callback=callback)

plot1.add_tools(hover_tool_plot)
plot2.add_tools(hover_tool_plot)

layout_ = layout([[plot1],[plot2]])
curdoc().add_root(layout_)

当我悬停在点 5 和 6 之间时,悬停注释捕捉到索引 6。但是,输出如下所示,这意味着没有选择索引:

Indices: 
VM612:6 Mouse X: 5.642537313432836
VM612:7 Mouse Y: 469.12556053811653

对于圆圈情况,这确实有效(但正如我所说,只有当我将鼠标悬停在该点上时)。 日志的输出确实给了我索引 6.

Indices: 6
VM746:6 Mouse X: 5.995974576271187
VM746:7 Mouse Y: 32.473542600896856

使用 mode='vline' 将鼠标悬停在线上时,是否有任何方法可以获取正确的索引?
我确实需要回调中的索引来为我的应用程序在另一个图中设置一条线。
PS:我是 运行 Bokeh 作为服务器应用程序。

对于线条字形,您要查看 .line_indices,而不是 .indices

请注意,对于应该返回哪个索引(上一个?下一个?最近的?)没有一个“正确”的答案。悬停工具有一个 line_policy 设置,您可以自定义您的需要。