I have a plot with checkboxes to toggle line visibility.
I would also like to turn off the hovering on the invisible lines. I am not sure how to proceed, I tried to update the “names” attribute of the HoverTool. The attribute is updated as expected as can be checked in the console log. After one check the hover is still on for all lines, after two checks the hover seems to die until the page is refreshed.
I thought updating names would be enough according to the docs bokeh.models.tools — Bokeh 0.12.4 documentation on “names”:
“A list of names to query for. If set, only renderers that have a matching value for their name
attribute will be used.”
Any ideas how I could proceed? Here is my code:
from bokeh.plotting import figure, output_file
from bokeh.models import CustomJS, HoverTool, CheckboxGroup
from bokeh.layouts import gridplot
from bokeh.resources import CDN
from bokeh.embed import file_html
fig = figure(plot_width=400,plot_height=400,toolbar_location=None)
plots =
plots.append( fig.line(x=range(10),y=range(10),name=‘first’) )
plots.append( fig.line(x=range(10),y=range(10)[::-1],name=‘second’) )
plots.append( fig.line(x=range(10),y=[i**0.5 for i in range(10)],name=‘third’) )
fig.add_tools(HoverTool(mode=‘vline’,line_policy=‘next’,names=))
labels = [‘first’,‘second’,‘third’]
N_plots = range(len(plots))
checkbox = CheckboxGroup(labels=names,active=,width=200)
checkbox_iterable =[(‘p’+str(i),plots[i]) for i in N_plots]+[(‘checkbox’,checkbox),(‘hover’,fig.tools[-1])]
checkbox_code = “”“var indexOf = .indexOf;”""+’’.join([‘p’+str(i)+’.visible = indexOf.call(checkbox.active, ‘+str(i)+’) >= 0;’ for i in N_plots])
checkbox_code += ‘hover.names = ;’+’’.join([‘if (p’+str(i)+’.visible) hover.names.push("’+labels[i]+’");’ for i in N_plots])+‘console.log(hover.names);’
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()
``
Thank you,
Sébastien