散景复选框与图中的右线不对应

bokeh checkbox doesn't correspond to the right line in the plot

我正在尝试在我的散景图中添加复选框,以便我可以在我的图中隐藏或显示不同的线条。我在 bokeh github 中找到了以下代码,这正是为此:

import numpy as np

from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
from bokeh.models import CheckboxGroup, CustomJS

output_file("line_on_off.html", title="line_on_off.py example")

p = figure()
props = dict(line_width=4, line_alpha=0.7)
x = np.linspace(0, 4 * np.pi, 100)
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend="Line 2", **props)

checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"],
                         active=[0, 1, 2], width=100)
checkbox.callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2,     checkbox=checkbox),
                             code="""
                                  l0.visible = 0 in checkbox.active;
                                  l1.visible = 1 in checkbox.active;
                                  l2.visible = 2 in checkbox.active;
                                  """)

layout = row(checkbox, p)
show(layout)

可以生成具有交互功能的"line_on_off.html"。但是,如果我取消选中一个框,无论是哪个框,l2 始终是隐藏的。如果我取消选中两个框,无论它们是哪个,l1 和 l2 始终是隐藏的。

我还尝试了其他代码,该代码将在散景服务器上生成相同的图,并且按意图工作。但我希望将其保存为具有交互功能的离线文件,而不是保留 运行 服务器。

知道为什么它在脱机​​文件中的行为不正确吗?

这是我运行进入的问题。它与 JavaScript 检查 in 的工作方式有关。它检查 0、1 或 2 是否在 checkbox.active 数组索引中。

我通过切换到 CoffeeScript 或使用 checkbox.active.indexOf('0')>-1) 作为测试解决了这个问题。两种方法都有效