【问题标题】:Controlling which elements receive hover tooltip in Bokeh控制哪些元素在 Bokeh 中接收悬停工具提示
【发布时间】:2019-10-22 15:53:26
【问题描述】:

我目前正在尝试找到一种使用布尔数组的方法,以确定哪些元素在散景中接收 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

【问题讨论】:

    标签: python-3.x hover tooltip bokeh


    【解决方案1】:

    作为临时解决方法,您可以像这样在回调中隐藏工具提示(适用于 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

    结果:

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多