Hello,
The code below has buttons to add/remove lines (with legend). Tried on 0.12.6dev3 and 0.12.5
Unsure if a bug, but in an attempt to attach js_on_change on renderer visibility with clickable legend,
the callback is called for the first line but not for the next added lines (or very rarely).
also, this is probably a bug, sometimes, after adding a line, we get :
TypeError: undefined is not an object (evaluating ‘M.draw_legend’)
That is a bit troubling. Actually, in a larger framework, this happens a lot and we haven’t found a way to cure it.
Any guidance/workarounds would be helpful,
Thx
import random
from bokeh.io import curdoc
from bokeh.layouts import row,column
from bokeh.plotting import figure
from bokeh.models import Legend,CustomJS
from bokeh.models.widgets import Button
n=30
x = list(range(30))
def v(): return [random.randint(0,100) for r in range(n)]
def get_nglyph(p): return sum([int(r.ref[‘type’]==‘GlyphRenderer’) for r in p.renderers])
ba = Button(label=‘add line’)
bd = Button(label=‘remove line’)
p = figure(plot_width=800, plot_height=250)
r = p.line(x=x,y=v(),legend=‘0’,name=‘0’)
r.js_on_change(‘change:visible’,CustomJS(code=""“console.log(‘change:visible’,cb_obj);”""))
leg = p.legend[0]; leg.click_policy=“hide”
def ba_cb():
r=p.line(x=x,y=v(),legend=str(len(p.legend[0].items)),name=str(len(p.legend[0].items)))
r.js_on_change(‘change:visible’,CustomJS(code=""“console.log(‘change:visible’,cb_obj);”""))
ba.on_click(ba_cb)
def bd_cb():
n_glyph = get_nglyph(p)
if n_glyph==0: return
leg_item = p.legend[0].items.pop()
p.renderers.remove(leg_item.renderers[0])
bd.on_click(bd_cb)
curdoc().add_root(column(row(ba,bd),p))