控制哪些元素在散景中接收悬停工具提示

Controlling which elements receive hover tooltip in Bokeh

我目前正在尝试寻找一种方法来使用布尔数组来确定哪些元素在散景中接收 HoverTooltips,同时仅对情节使用一个字典。

我已经尝试使用散景渲染功能,但效果不佳。

source.data = dict(
        x=data_source.loc[:, 'x_col_name'],
        y=data_source.loc[:, 'y_col_name'],
        color=color_selection(selected), # Translates bool to color
        alpha=alpha_selection(selected), # Translates bool to transparency
        active=selection                 # boolean array of selected elements
     )  

只有活动数据点应该收到 HoverTooltip

从 Bokeh 1.x 开始,没有过滤悬停结果的内置功能。该功能计划在 2.0 版本中发布,您可以在此处关注此问题:https://github.com/bokeh/bokeh/issues/9087

作为临时解决方法,您可以像这样在回调中隐藏工具提示(适用于 Bokeh v1.3.0):

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

data = dict(
        x=[1, 2, 3, 4, 5],
        y=[1, 2, 3, 4, 5],
        color=['red', 'green', 'red', 'green', 'red'], 
        active=[False, True, False, True, False],
     ) 

p = figure(tooltips=[('x','@x'),('y', '@y'), ('active', '@active')])
p.circle('x', 'y', color='color', size=10, source = data)

code_hover = '''
if (cb_data.index.indices.length > 0) {
    var active_index = cb_data.index.indices[0]   
    var data = cb_data.renderer.data_source.data
    var show_tooltip = data['active'][active_index]
    var tooltip_index = 0

    if (show_tooltip) {
        document.getElementsByClassName('bk-tooltip')[tooltip_index].style.display = 'block';
    }
    else {
        document.getElementsByClassName('bk-tooltip')[tooltip_index].style.display = 'none';       
    }
}
'''

p.hover.callback = CustomJS(code = code_hover)

show(p)

请注意回调中的tooltip_index。如果您有更多工具提示,则需要更改该索引。另见 this post

结果: