Is there a way to update legend patch labels using a CustomJS callback?

Hi, see my post on stack overflow below.

I am very, very stuck on this and would appreciate any help!

Hi gmerritt123! Welcome to the Bokeh Discourse.

I was able to get this working with some changes to your callback function. You were pretty close!

(Side note, this just happened to be quick because of a recent similar question, in which the person was trying to do something a little more complicated but still involving updates to Legend text.)

Here’s my changed version of your callback. The real difference is that I added a legend arg that points straight to the LegendItem object p.legend.items[0].

b.js_on_click(CustomJS(args=dict(b=b, source=source, patches=patches, cmap1=cmap1, cmap2=cmap2, legend=p.legend.items[0]), code="""
            if (b.label == 'RdBu') {
              patches.glyph.fill_color = {field: 's2',transform: cmap2};
              legend.label.field = 'label_2';
            else if (b.label == 'Spectral')
              patches.glyph.fill_color = {field: 's1', transform: cmap1};
              legend.label.field = 'label_1';

As far as why passing in the parent figure object p and then referring to p.legend.items[0] in the callback doesn’t work, I don’t have an answer there, and would love to know also (@Bryan?). But this solution should at least get you moving again!

Thank you so much! :smiley:

1 Like