散景:如何通过回调打开/关闭HoverTool?

塞布

有没有一种方法可以在回调中激活/停用悬停工具?

在尝试使用复选框切换线可见性的同时,我尝试执行此操作。每行都有一个悬停工具,当一行具有“ visible = false”时,我将相应工具的“名称”属性设置为虚拟字符串;否则,我将其设置为等于该行的初始“名称”属性。

它在第一次选中任何框时都有效,另外两行消失,并且悬停工具未显示在它们上面。但是重新激活悬浮工具不起作用。

这是我的尝试:

from bokeh.plotting import figure, output_file
from bokeh.models import CustomJS, HoverTool, CheckboxGroup, ColumnDataSource
from bokeh.layouts import gridplot
from bokeh.resources import CDN
from bokeh.embed import file_html

from collections import OrderedDict

TOOLTIPS = [ ("x", "$~x"), ("y", "$~y") ]

TOOLS = ['crosshair']

source = ColumnDataSource(data={'x':range(10),'y0':range(10),'y1':range(10)[::-1],'y2':[i**0.5 for i in range(10)]})

fig = figure(tools=TOOLS)

fig.tools[0].dimensions = 'height'

plots = []
plots.append( fig.line(x='x',y='y0', name="0", source=source) )
plots.append( fig.line(x='x',y='y1', name="1", source=source) )
plots.append( fig.line(x='x',y='y2', name="2", source=source) )


names = ['first','second','third']
ID=0
for plot in plots:
    fig.add_tools( HoverTool(mode='vline',line_policy='interp',renderers=[plot],names=[str(ID)],tooltips=OrderedDict( [('name',names[ID])]+TOOLTIPS )) )
    ID+=1

N_plots = range(len(plots))

checkbox = CheckboxGroup(labels=names,active=[],width=200)
checkbox_iterable =[('p'+str(i),plots[i]) for i in N_plots]+[('hover'+str(i),fig.tools[1:][i]) for i in N_plots]+[('checkbox',checkbox)]
checkbox_code = """var indexOf = [].indexOf;"""+''.join(['p'+str(i)+'.visible = indexOf.call(checkbox.active, '+str(i)+') >= 0;' for i in N_plots])
checkbox_code += ''.join(['if(p'+str(i)+'.visible) hover'+str(i)+'.names = ["'+str(i)+'"];' for i in N_plots])+''.join(['if(p'+str(i)+'.visible==false) hover'+str(i)+'.names = ["no"];' for i in N_plots])
checkbox_code += ''.join(['console.log(hover'+str(i)+'.names);' for i in N_plots])
checkbox.callback = CustomJS(args={key: value for key,value in checkbox_iterable}, code=checkbox_code)

grid = gridplot([[fig,checkbox]],toolbar_location='left')

outfile=open('hovtest.html','w')
outfile.write(file_html(grid,CDN,'test'))
outfile.close()

我使用bokeh 0.12.4

塞布

bokeh讨论小组的某个人找到了一种很好的方法(bokeh讨论线程)。

我们可以基于checkbox.active列表更改悬停工具的显示属性。这是示例代码:

from bokeh.plotting import figure, output_file
from bokeh.models import CustomJS, HoverTool, CheckboxGroup, ColumnDataSource
from bokeh.layouts import gridplot
from bokeh.resources import CDN
from bokeh.embed import file_html

from collections import OrderedDict

TOOLTIPS = [ ("x", "$~x"), ("y", "$~y") ]

TOOLS = ['crosshair']

data = {'x':range(10),
        'y0':range(10),
        'y1':range(10)[::-1],
        'y2':[i**0.5 for i in range(10)]
        }

source = ColumnDataSource(data=data)

fig = figure(tools=TOOLS)

fig.tools[0].dimensions = 'height'

plots = []
plots.append( fig.line(x='x',y='y0', name="first", source=source) )
plots.append( fig.line(x='x',y='y1', name="second", source=source) )
plots.append( fig.line(x='x',y='y2', name="third", source=source) )

checkbox = CheckboxGroup(   
    labels=[plot.name for plot in plots],
    active=[0,1,2],
    width=200,
    callback = CustomJS(    args=dict(p0=plots[0],p1=plots[1],p2=plots[2]),
                            code = """
                                p0.visible = cb_obj.active.includes(0);
                                p1.visible = cb_obj.active.includes(1);
                                p2.visible = cb_obj.active.includes(2);
                                """
                        )
    )

for ID,plot in enumerate(plots):
    fig.add_tools(  
        HoverTool(  
            mode='vline',
            line_policy='interp',
            renderers=[plot],
            names=[plot.name],
            tooltips=OrderedDict( [('name',plot.name)]+TOOLTIPS ),
            callback=CustomJS(  args=dict(cb=checkbox),
                                code="""
                                    if(!cb.active.includes(%d)) {
                                    document.getElementsByClassName('bk-tooltip')[%d].style.display = 'none';
                                    }
                                    """ % (ID,ID)
                                )
            ) 
    )

grid = gridplot([[fig,checkbox]],toolbar_location='left')

outfile=open('hovtest.html','w')
outfile.write(file_html(grid,CDN,'hovtest'))
outfile.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

散景:Figure.line的hovertool自定义回调

如何实现 javascripts 回调以更改散景图标题

仅在释放“散景”滑块时如何执行回调

如何获得对象触发了回调的散景回调?

如何使用散景使用 HoverTool 创建多线图?

如何使用散景按钮小部件激活/停用 HoverTool?

如何在回调中引用悬停的散景四值

散景服务器-如何在回调函数中操作选择

在输入文本时使散景 TextInput 回调响应

散景多选小部件回调不起作用

散景滑块自定义JS回调

散景 python 回调以打印新的属性值

散景回调是否更新了列表而不是ColumnDataSource?

散景:用于鼠标移动或单击的CustomJS回调

散景:双击圆圈时的JS回调

散景:在单个Python回调中多次更新图形

散景:hovertool 的图像不显示

散景 Hovertool 堆叠条形图

Hovertool散景“无法读取属性”

如何使用Zurb Foundation显示打开,打开,关闭,关闭回调函数?

散景的依赖滑块,如何编写回调

散景-如何使HoverTool工具提示与点击点保持一致?

散景:如果触发另一个回调,则触发回调

带有 CustomJS 回调的散景滑块无法使用 callback_policy='mouseup' 选项

散景:CustomJS TextInput回调可调整x轴范围

散景小部件为单个事件调用CustomJS和Python回调吗?

散景图访问单选按钮在滑块的回调中处于活动状态

散景回调以更改源数据和悬停工具提示

散景小部件回调以选中所有复选框