9

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.

Here's the bar plot before and after:

before and after.

Here's what my data looks like:

Here's

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)
tuomastik
  • 4,559
  • 5
  • 36
  • 48
Pranav Menon
  • 181
  • 3
  • 10

1 Answers1

3

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

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:

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))
tuomastik
  • 4,559
  • 5
  • 36
  • 48
  • Has there been any updates to this now that Bokeh is in version 1.2.0? – skrhee Jul 16 '19 at 23:50
  • 2
    I'm afraid that today in [Bokeh 1.3.0](https://bokeh.pydata.org/en/latest/docs/user_guide/interaction/legends.html) it is still the same. I tried implementing an interactive legend in an hbar plot. I wanted to mute some bars. When clicking in an element in the legend, the whole legend goes semitransparent, but the bars remain unchanged. – Arturo Moncada-Torres Jul 26 '19 at 23:37