Skip to main content
Announcements
See why Qlik is a Leader in the 2024 Gartner® Magic Quadrant™ for Analytics & BI Platforms. Download Now
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
Anonymous
Not applicable

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 ?

5,651 Views
ArturoMuñoz
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
5,651 Views
Anonymous
Not applicable

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
5,651 Views
ArturoMuñoz
Employee
Employee

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

0 Likes
5,651 Views
Gysbert_Wassenaar

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

5,651 Views
Anonymous
Not applicable

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

0 Likes
5,651 Views
julian_rodriguez
Partner - Specialist
Partner - Specialist

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
4,873 Views
marcohadiyanto
Partner - Specialist
Partner - Specialist

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
4,873 Views
ArturoMuñoz
Employee
Employee

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

0 Likes
4,873 Views
ArturoMuñoz
Employee
Employee

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

0 Likes
4,873 Views