0

Is it possible to change the names of the series displayed on the plot dynamically (depending on the current selection of the user)? This is my attempt, but the legend doesn't change.

df['y'] = df.iloc[:, 2].values
df['y2'] = df.iloc[:, 3].values

TITLE = 'test'
output_file(TITLE + '.html')

p = figure(x_axis_type='datetime', title=TITLE)

source = ColumnDataSource(df)

line1 = p.line(x='Date', y='y', source=source, color="blue", legend_label='Line 1')
line2 = p.line(x='Date', y='y2', source=source, color="red", legend_label='Line 2')

columns_without_y = [col for col in df.columns[1:] if col not in ['y', 'y2']]

ys1 = Select(title='Select Column 1:', value=df.columns[1], options=columns_without_y)
ys2 = Select(title='Select Column 2:', value=df.columns[2], options=columns_without_y)

callback = CustomJS(args=dict(source=source, sel1=ys1, sel2=ys2, line1=line1, line2=line2), code="""
    const data = source.data;
    const column1 = sel1.value;
    const column2 = sel2.value;
    data['y'] = data[column1];
    data['y2'] = data[column2];
                    
    source.change.emit();
                    
    line1.glyph.legend_label = column1;
    line2.glyph.legend_label = column2;
""")


ys1.js_on_change('value', callback)
ys2.js_on_change('value', callback)

controls = column(ys1, ys2)
layout = row(controls, p)

show(layout)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.