Bokeh交互传奇隐藏多个符号。

时间:2022-01-07 00:36:01

I've enabled the hide legend option for my dataset. When I click it, only one bar goes off and others stay. I'm not quite sure what is causing the issue.

我已经为我的数据集启用了hide legend选项。当我点击它的时候,只有一个酒吧离开了,而其他的就留下了。我不太清楚是什么导致了这个问题。

Here's the bar plot before and after:

这是之前和之后的酒吧情节:

Bokeh交互传奇隐藏多个符号。.

Here's what my data looks like:

我的数据如下:

Bokeh交互传奇隐藏多个符号。

Here's the code:

这是代码:

p = Bar(output,'Programs',values="Averages", group="University",plot_width=600,plot_height=400, title="Comparison")
p.legend.click_policy="hide"
output_file("bar.html")
show(p)

1 个解决方案

#1


0  

It is not currently (Bokeh 0.12.6) possible to hide all the bars via legend.click_policy="hide", as stated in the documentation:

目前(Bokeh 0.12.6)不可能通过图例隐藏所有的酒吧。click_policy=“隐藏”,如文档中所述:

Interactive legend features currently work on “per-glyph” legends. Legends that are created by specifying a column to automatically group do no yet work with the features described below

互动的传奇人物目前致力于“每雕”的传说。通过指定一个列来自动分组的传说还没有使用下面描述的特性。

It is, however, possible to hide the bars by adding CheckboxGroup() with CustomJS that hides the bars when the checkboxes are clicked. Below you can see an MCVE, which is also available online as a Jupyter Notebook:

但是,可以通过在单击复选框时添加CheckboxGroup()来隐藏这些条,并将其隐藏起来。在下面你可以看到一个MCVE,它也可以作为一个Jupyter笔记本在线提供:

import numpy as np
from bkcharts import Bar, show
from bokeh.layouts import column
from bokeh.models import CheckboxGroup, CustomJS

data = {'University': ['ENGT'] * 3 + ['UBC'] * 3,
        'Averages': [76.5, 79.9, 72.2, 71, 72, 69],
        'Programs': ['CHML', 'CIVL', 'CPEN', 'CHML', 'CIVL', 'CPEN']}

group = "University"
bars = Bar(data=data, label='Programs', values="Averages", group=group,
           plot_width=600, plot_height=400, title="Comparison")

checkboxes = CheckboxGroup(labels=np.unique(data[group]).tolist(),
                           # Make all checkboxes checked by default
                           active=list(range(np.unique(data[group]).size)))
checkboxes.callback = CustomJS(args=dict(bars=bars), code="""

var group = '%s';

function change_bars_visibility(checkbox_name, visible) {
    for (j = 0; j < bars.renderers.length; j++) {
        // Go through rendered objects

        if (bars.renderers[j].attributes.hasOwnProperty('data_source') && 
            bars.renderers[j].data_source.data[group][0] === checkbox_name) {

            // Change the visibility of this rendered object if it belongs to
            // the group determined by the checkbox that was clicked
            bars.renderers[j].visible = visible;
        }
    }
}

for (i = 0; i < cb_obj.labels.length; i++) {
    // Go through checkbox labels
    var checkbox_name = cb_obj.labels[i];

    if (cb_obj.active.indexOf(i) >= 0) {
        // alert(checkbox_name + " is activated");
        change_bars_visibility(checkbox_name, true);
    }
    else {
        // alert(checkbox_name + " is disabled");
        change_bars_visibility(checkbox_name, false);
    }
}
""" % group)

show(column(bars, checkboxes))

#1


0  

It is not currently (Bokeh 0.12.6) possible to hide all the bars via legend.click_policy="hide", as stated in the documentation:

目前(Bokeh 0.12.6)不可能通过图例隐藏所有的酒吧。click_policy=“隐藏”,如文档中所述:

Interactive legend features currently work on “per-glyph” legends. Legends that are created by specifying a column to automatically group do no yet work with the features described below

互动的传奇人物目前致力于“每雕”的传说。通过指定一个列来自动分组的传说还没有使用下面描述的特性。

It is, however, possible to hide the bars by adding CheckboxGroup() with CustomJS that hides the bars when the checkboxes are clicked. Below you can see an MCVE, which is also available online as a Jupyter Notebook:

但是,可以通过在单击复选框时添加CheckboxGroup()来隐藏这些条,并将其隐藏起来。在下面你可以看到一个MCVE,它也可以作为一个Jupyter笔记本在线提供:

import numpy as np
from bkcharts import Bar, show
from bokeh.layouts import column
from bokeh.models import CheckboxGroup, CustomJS

data = {'University': ['ENGT'] * 3 + ['UBC'] * 3,
        'Averages': [76.5, 79.9, 72.2, 71, 72, 69],
        'Programs': ['CHML', 'CIVL', 'CPEN', 'CHML', 'CIVL', 'CPEN']}

group = "University"
bars = Bar(data=data, label='Programs', values="Averages", group=group,
           plot_width=600, plot_height=400, title="Comparison")

checkboxes = CheckboxGroup(labels=np.unique(data[group]).tolist(),
                           # Make all checkboxes checked by default
                           active=list(range(np.unique(data[group]).size)))
checkboxes.callback = CustomJS(args=dict(bars=bars), code="""

var group = '%s';

function change_bars_visibility(checkbox_name, visible) {
    for (j = 0; j < bars.renderers.length; j++) {
        // Go through rendered objects

        if (bars.renderers[j].attributes.hasOwnProperty('data_source') && 
            bars.renderers[j].data_source.data[group][0] === checkbox_name) {

            // Change the visibility of this rendered object if it belongs to
            // the group determined by the checkbox that was clicked
            bars.renderers[j].visible = visible;
        }
    }
}

for (i = 0; i < cb_obj.labels.length; i++) {
    // Go through checkbox labels
    var checkbox_name = cb_obj.labels[i];

    if (cb_obj.active.indexOf(i) >= 0) {
        // alert(checkbox_name + " is activated");
        change_bars_visibility(checkbox_name, true);
    }
    else {
        // alert(checkbox_name + " is disabled");
        change_bars_visibility(checkbox_name, false);
    }
}
""" % group)

show(column(bars, checkboxes))