我试图在我的散景图中添加复选框,以便我可以在我的情节中隐藏或显示不同的线条 . 我在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总是隐藏的那个 .
我还尝试了其他代码,它将在散景服务器上生成相同的图,并且作为意图 . 但我希望将其保存为具有交互功能的脱机文件,而不是保持运行服务器 .
任何想法为什么它在离线文件中行为不正确?
1 回答
这是我遇到的问题earlier . 它与JavaScript检查
in
的工作方式有关 . 它检查0,1或2是否在checkbox.active
数组索引中 .我通过切换到
CoffeeScript
或使用checkbox.active.indexOf('0')>-1)
作为测试来解决它 . 两种方法都有效