Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

Employee
Employee

How to custom color a stacked bar chart

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

This post luckily appeared just as I was searching how to do this !!!

It works great apart from the Legend that shows which colour represent which Quarter.

  • When Colors is set to By Expression the Legend disappears
  • When Colors is set to Auto the Legend appears

Is it possible to use Customs Colors as you describe and show the Legend so the End Users know what the colors represent what ?

96 Views
Employee
Employee

Great timing! When you switch to Color by expression and check on "the expression is a color code" we lost the legend. I don't think we can have both things at the same time right now (3.1), custom colors and legend.

0 Likes
96 Views

That is sad - would you be able to advise R&D of this bug ?

Not sure my Users will be happy when I tell them.

0 Likes
96 Views
Employee
Employee

Well, is not exactly a bug, is working as designed although it would be a nice new feature.

0 Likes
96 Views

I hope that this kind of workaround will soon be made redundant by the possibility of a color expression for each measure.

96 Views

Notwithstanding the semantics of bugs vs. undocumented features would you be able to advise R&D ?

0 Likes
96 Views
julian_rodrigue
Valued Contributor

I agree with you mr. gwassenaar , I don't really know why a user should need to do al this work just for a color change. I can't get it.

0 Likes
96 Views
chooco_co
Valued Contributor

Any idea when i have 1 dimension and stacked bar by measure?

example I want to create stacked bar chart, i have year as dimension and i have sales and cost as my measures.

I want yellow color to represent sales and red color to represent cost.

0 Likes
96 Views
Employee
Employee

Marco, check the multi-expression stacked bar chart work-around as described in the blog post, that will make it.

0 Likes
96 Views
Employee
Employee

I think we will see that soon can't guarantee it or say when but that's my bet.

0 Likes
96 Views
Employee
Employee

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

0 Likes
96 Views

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.

96 Views
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
96 Views

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

0 Likes
96 Views
msteedle
Contributor

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
96 Views