CustomJSTransform on Legend Label

What are you trying to do?

I have a line glyph/renderer and I want the line’s legend label to follow a CustomJSTransform, in my simple MRE it should read “My Custom Label”.

What have you tried that did NOT work as expected?

I looked at legend instantiation here → Annotations — Bokeh 3.6.0 Documentation

and it looks like I can mimic what assigning ‘legend_field’ to renderer instantiation does by assigning a transform to the label argument when instantiating an explicit LegendItem. However, this does produce the desired behavior, as my CustomJSTransform assigned to the transform is ignored and it just does the usual legend_field thing instead.

MRE:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJSTransform, Legend, LegendItem
from bokeh.transform import transform

src = ColumnDataSource({'x':[1,2,3],'y':[2,3,1]})

f = figure()

r = f.line(x='x',y='y',source=src)

ctr = CustomJSTransform(func='''
                        return 'My Custom Label'
                        ''')

legend = Legend(items=[
    LegendItem(label="my label", renderers=[r]),
    #???
    LegendItem(label=transform(field_name='x',transform=ctr),renderers=[r],index=0)
])
f.add_layout(legend)
show(f)

Thanks…

I’m guessing this is some missing plumbing. LegendItem.label probably used NullStringSpec because it needed to distinguish between values and fields (column names) and NullStringSpec can do that. I think it was only later on that support for transforms was added in other places that happened to use dataspecs (e.g. glyph coordinates) and no one thought to update legend items to also make use of the new capabilities at that time.

I think it might be fairly simple to add (no guarantees I am right about that) but it would need GitHub Issue to prompt new development, regardless.

1 Like

Thanks → [FEATURE] CustomJSTransform on Legend Label · Issue #14087 · bokeh/bokeh · GitHub

1 Like