当使用分类范围时,与当前范围中的类别不对应的glyph值将被忽略。考虑到这一点,要做的最简单的事情就是预先“绘制”两个数据集,然后更改回调中的范围:
# plot dataframes
names = df_fruit['names'].tolist()
plot = figure(plot_width=500, plot_height=400, title="Data Counts", x_range=names)
fruit = plot.vbar(source=source_fruit, x="names", top="values", bottom=0, width=0.75, color="red", fill_alpha=0.7, name='fruits')
veg = plot.vbar(source=source_vegetables, x="names", top="values", bottom=0, width=0.75, color="red", fill_alpha=0.7, name='veg')
# callback for input controls
callback = CustomJS(args=dict(fruit=fruit, veg=veg, plot=plot), code="""
if (ui_view.value=="Fruit") {
plot.x_range.factors = fruit.data_source.data.names
} else {
plot.x_range.factors = veg.data_source.data.names
}
""")
ui_view = Select(title="View", callback=callback, value="Fruit", options=["Fruit", "Vegetables"])
callback.args['ui_view'] = ui_view
# layout
layout = column(ui_view, plot)
show(layout)