简体   繁体   English

如何根据Bokeh中先前的多选项选择将参考曲线添加到绘图中

[英]How to add a reference curve to a plot depending on a previous multiselect choice in Bokeh

I am new in Bokeh and I am trying to implement a checkbox widget that would add a single reference curve to a plot depending on the user's previous multiselect choice. 我是Bokeh的新手,我正在尝试实现一个复选框小部件,它会根据用户之前的多选项选择向绘图添加单个参考曲线。 For example, if the user selects A in the multiselect widget, than the curve A would appear when the checkbox widget is used. 例如,如果用户在多选小部件中选择A,则使用复选框小部件时将显示曲线A. If the user selects B in the multiselect widget, then the curve B would appear when the checkbox widget is used. 如果用户在多选小部件中选择B,则在使用复选框小部件时将显示曲线B. There would only be a single check box called ''Add reference curve''. 只有一个名为“添加参考曲线”的复选框。 Right now the checkbox widget doesn't quite work most likely because I am having a hard time with the CustomJS. 现在,复选框小部件最不可能工作,因为我很难使用CustomJS。 My code is below. 我的代码如下。 Any ideas ? 有任何想法吗 ?

from bokeh.models import CustomJS, ColumnDataSource, MultiSelect, CheckboxGroup, Column

from bokeh.plotting import figure, show
import pandas as pd

data = dict(letter = ['A','A','B','C','B','B','A','C','C','B'],
x = [1, 2, 1, 2, 3, 2, 2, 3, 2, 3],
y = ['10','20','10','30','10','40','10','30','10','40'])
data = pd.DataFrame(data)

data_source = ColumnDataSource(data)
source = ColumnDataSource(dict(x = [], y = []))

plot = figure()
plot.circle('x', 'y', line_width = 2, source = source)

callback = CustomJS(args = {'source': source, 'data_source': data_source},
code = """
var data = data_source.data;
var s_data = source.data;
var letter = data['letter'];
var select_vals = cb_obj.value;
var x_data = data['x'];
var y_data = data['y'];
var x = s_data['x'];
x.length = 0;
var y = s_data['y'];
y.length = 0;
for (var i = 0; i < x_data.length; i++) {
    if (select_vals.indexOf(letter[i]) >= 0) {
        x.push(x_data[i]);
        y.push(y_data[i]);
        }
}
source.change.emit();
""")

multiselect = MultiSelect(title = 'Choose', value = [], options = ['A', 'B', 'C'])
multiselect.js_on_change('value', callback)


# Toggle reference curves on/off

## Dummy data for plotting lines testing
x = list(range(10))
A = [ a**1.5 for a in x]
B = [ a**1.6 for a in x]
C = [ a**1.7 for a in x]

curve_A = plot.line(x, A, color='red')
curve_B = plot.line(x, B, color='red')
curve_C = plot.line(x, C, color='red')

curve_A.visible = False
curve_B.visible = False
curve_C.visible = False

curve_checkbox = CheckboxGroup(labels=["Add reference curve",], active=[])
curve_checkbox.callback = CustomJS(args=dict(A=A, B=B, C=C, source=source, curve_checkbox=curve_checkbox),
                                     code="""
                                     curve_A.visible = 0 in checkbox.active;
                                     """)

layout = Column(multiselect, curve_checkbox, plot)
show(layout)

Try the following code: 请尝试以下代码:

curve_checkbox.callback = CustomJS(args=dict(curve_A=curve_A, 
                                             curve_B=curve_B, 
                                             curve_C=curve_C, 
                                             source=source, 
                                             checkbox=multiselect),
                                     code="""
    let checked = cb_obj.active.length > 0
    curve_A.visible = checkbox.value.indexOf("A") >= 0 && checked;
    curve_B.visible = checkbox.value.indexOf("B") >= 0 && checked;
    curve_C.visible = checkbox.value.indexOf("C") >= 0 && checked;
                                     """)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM