Search code examples
bar-chartapache-superset

Can I have a bar chart that is both stacked and grouped?


I have a bar chart in Superset where I have two different columns configured under BREAKDOWN. Both columns divide the data into two parts, so I end up with four different breakdowns. Example: I have sales data for two different cities, and the sales data are broken down by product (2 different products).

I would like to compare sales between the cities, so I would like to group the one column (sales). However, for each city I want to add up total sales by adding both products, so I would like to stack those bars.

So far, I can only find a way to EITHER stack or group the bars, which gives me either four bars per month, or a single bar - neither of which is useful. Is there a way to have a chart that is both grouped and stacked at the same time depending on the column used for the breakdown?

This is what it looks like now:

Stacked columns

Grouped columns


Solution

  • Unfortunately right now it is impossible to do with the Timeseries charts. Even when using mixed Timeseries when both of the charts are Bar charts when they are stacked, they will stack together.

    There is one workaround that could help you with achieving this goal.

    You need to use Bar Chart

    enter image description here

    Here I just have some dummy data with sales_date, product, city and sales

    As a first dimension I set

    DATE_TRUNC('month', sales_date)::date
    

    as I want to have sales grouped by month. As a second dimension I've just set city

    When it comes to breakdown you can simply add product as a breakdown but I've set

    (product || ', ' || city)
    

    so it can be treaded as different labels and therefore it is more readable on chart.

    At the end since Bar chart doesn't understand that it need to be sorted by date. In a field sort by I've set

    (DATE_TRUNC('month', sales_date)::date)::text || city
    

    and and set to be sorted ascending After doing all that go to CUSTOMIZE options and set chart to have STACKED BARS

    This is a workaround by using only Superset's charts.