Skip to main content
ArturoMuñoz
Employee
Employee

Stacked bar charts are perfect to represent the contribution of particular elements to the total, the classic example is Sales by Year and by Quarter.

 

1.png

 

Just by observing the chart a few seconds we can conclude that Actual Amount was higher in 2007 than 2006, and it seems clear that Q2 rise in 2007 contributed significantly to the 2007 total increase in Actual Amount.

 

To create a simple stacked bar chart like the one in our example we've needed 2 dimensions (Year and Quarter) and one measure. Alternatively, we could recreate the chart using one single dimension "Year" and 4 expressions one per each one of the quarters. The procedure to customize the colors will depend of what type of chart you have, bi-dimensional chart or multi-expression chart, let’s start with bi-dimensional chart coloring.

 

Bi-dimensional stacked bar chart

 

This is the simplest case, you just need to target each one of the segments (Quarters in our example) by name, you could just use an if statement to target them, something like:

 

if(Quarter='Q1',red(),if(Quarter='Q2',blue(), if(Quarter='Q3', green(), yellow())))














 

Alternatively, you could use conditional functions for a more elegant approach:

 

pick(match(Quarter,'Q1','Q2','Q3','Q4'),red(),blue(),green(),yellow())














 

 

2.png

 

 

Multi-expression stacked bar chart

 

In this scenario our data table contains one column per quarter as in the image below but we still want to represent them in one stacked chart using custom colors.

 

3.png

 

We could get a stacked bar chart using one single dimension "Year", and 4 expressions Q1,Q2, Q3 and Q4. The problem comes when trying to color the segments, at this point we can’t target specific segments anymore because each one of our segments is made of an expression.

 

To solve this situation, we need to work-around our chart to make it again bi-dimensional. In order to do that we’ll add a new table to our data model. Our table will contain the name of the segments for our chart, I called it [segment names] in my example.

 

4.png

 

Once the data has been loaded then it’s time to create our chart. The dimensions will be “Year” and the recently created “measure”. To complete our chart, we’ll need to add a simple if statement (or the more elegant pick&match combo) in our measure expression, similar to this one:

 

if(measure='Q1', sum(Q1),

if(measure='Q2', sum(Q2),

if(measure='Q3', sum(Q3),

if(measure='Q4', sum(Q4)))))

 

 

Now that our chart is standard bi-dimensional bar chart, all we need to do is to apply custom colors as described earlier in this post. So again we could use the good old if statement or pick&match to end up having customized segment colors in our stacked bar chart.

 

6.PNG

 

I'm attaching an example app so you can check how it's done.

 

I want to give credits to all the contributors to How to use custom colours in a stacked bar chart‌, please check that community thread if you have questions or just to learn more about custom colors in stacked bar charts.

 

AMZ

15 Comments
ArturoMuñoz
Employee
Employee

R&D is well aware of it, they designed it to be like that.

0 Likes
1,656 Views
Gysbert_Wassenaar

They may have made a design decision and not completely thought through the consequences. It is possible to make a mistake in a design. To me that's a design bug. It may work as designed, but imho it doesn't work as it should. It's a flaw in the design. In other words a bug. Bill and I would like to make sure that R&D is aware of the issues Bill and I have with this particular design decision.

1,656 Views
ArturoMuñoz
Employee
Employee

I really wasn't trying to start a discussion on semantics. I don't think it's a bug, at least not in the way we internally refer to bugs. You still think it's a bug, that's ok. Your points are fair, believe me I'm with you, I daily work with Sense, I'd love to have a simpler way to do it. Bill and you insisted in getting R&D aware of it, and as a matter of fact they are aware, I had a conversation last week about it right before my reply here.

0 Likes
1,625 Views
Gysbert_Wassenaar

Great, that's good to hear. Thanks for the clarification.

0 Likes
1,625 Views
msteedle
Luminary Alumni
Luminary Alumni

One trick I like to employ for better UI performance, centralizing logic, and re-use across multiple visualizations, is storing the colors in the data model. For instance, pushing this to the script, you could just refer to this field instead of writing a formula in the chart.

if(Quarter='Q1',red(),if(Quarter='Q2',blue(), if(Quarter='Q3', green(), yellow()))) as [color.Quarter]


Taking it a step further, you could encode the color, which has an underlying numeric value, as the numeric portion of a dual value, and store both the quarter values and colors in a single field, ex. Dual('Q1', red())...

0 Likes
1,625 Views