bokeh icon indicating copy to clipboard operation
bokeh copied to clipboard

[FEATURE] Dynamically positioned stacked bar chart segments

Open hahnd opened this issue 5 years ago • 5 comments

One feature in bokeh allows a user to click on the legend to toggle visibility of categorical data. When hiding a category in a stacked bar chart, the bar segments disappear as expected, however it would be nice to have the remaining bar segments change their position, slide over, so there are no gaps left in the plot.

The attached file is a sample that will plot a horizontal stacked bar chart. If you turn off the year 2016, you can see they disappear, but now I want the 2017 segments to slide to the left until they are against the 2015 segments. Here is a link to better illustrate the request if it is not clear: https://www.highcharts.com/demo/column-stacked

I posted this question on the forum and was told it was not possible at this time and suggested to create a new feature request. (https://discourse.bokeh.org/t/re-position-bar-segments-for-stacked-bar-plot/4887/2)

Sample Code

hahnd avatar Mar 12 '20 18:03 hahnd

I think this would be a great feature to have but it will require some investigation and thought. Currently the Stack transform is only configured with column names, it does not have any way to know which column corresponds to a glyph in order to check visibility. Some offhand ideas:

  • could add optional additional proeprty to specify a corresponding list of glyphs. Ugly bit here is having to coordinate two lists of things that could then get out of sync
  • could extend the current property type to also accept a list of (name, renderer) tuples, instead of a list of column names

bryevdv avatar Mar 12 '20 18:03 bryevdv

as requested, an image & minimal example of the current (faulty) state (sorry for my confusion):

from bokeh.io import output_file, show
from bokeh.plotting import figure

output_file("stacked.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 4, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,
             legend_label=years,
             )

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
p.legend.click_policy="hide"
show(p)

image

Dr-Nuke avatar Mar 29 '20 22:03 Dr-Nuke

I agree. This feature would be very useful. Without it, stacked bar charts have very limited intractability. If anyone has a workaround please let me know. If I figure one out I will update here.

Issue:

image image

EvanDungate avatar Jan 12 '22 18:01 EvanDungate

This is exactly the issue I am facing. I see this feature request is closed, but was a solution every implemented? I was unsuccessful in trying to build a JS callback to achieve this.

charleslstone avatar Jul 26 '24 21:07 charleslstone

I see this feature request is closed

I am not sure what you are referring to @charleslstone this issue is clearly still open

Screenshot 2024-07-26 at 15 18 24

There is, however, no news to report that I am aware of.

Unfortunately, the way stacking is implemented across the Python/JavaScript runtime boundary actually makes this ask not trivial at all to implement. Currently, stacked glyphs are in a "chained together" relationship, with each glyph knowing about the glyphs "before" and "after" in the stack. Removing a link in the middle is actually not a simple operation.

Realistically, I think to support something like this we will need to implement an alternative way to stack glyphs that pulls the stack configuration outside the glyphs entirely into one place so that there can be additional metadata to specify things like "hide this layer of the stack" or so that the stack order can be as simple as a list that can be updated. But I can't speculate when that work might happen or by who.

bryevdv avatar Jul 26 '24 22:07 bryevdv