A Color Trick

A Customer showed me this stacked bar chart and asked how to “make the Goals a different color”. What he really wanted was to differentiate the Goal stacks from the Actual stacks — but still be able to associate the Channels (Consumer, Online,…).

Channel values are expected to change over time, so any hard-coding of Channel to color would require maintainence. Here’s the Background Color expression I suggested:

if(Type=’Goal’
,argb(96,255,255,255) bitand color(FieldIndex(‘Channel’,Channel))
,color(FieldIndex(‘Channel’,Channel))

If that makes perfect sense to you, read no further. Otherwise let me break the expression down.

What we are doing in the expression is setting the Alpha value to “96” when “Type=Goal”. “Type=Actual” will retain the Alpha default of “255”.

Color codes in QV are made up of four numbers — Alpha, Red, Green, Blue — values which we can set in a color dialog or a color function. Alpha indicates the amount of transparency, ranging from 0 (fully transparent) to 255 (fully opaque). Colors functions without an explicit Alpha value like “RGB(0,128,0)” default to Alpha=255.

The function “Color(n)” returns the color code for the “Nth” position in the chart color palette.

FieldIndex(‘Field‘, Value) returns the position of  Value in Field (by load order).  This will generate a number for each distinct Channel value, regardless of how many Channel values are loaded. We are effectively assigning persistent colors because FieldIndex() is not affected by selections.

1. We use color(FieldIndex(‘Channel’,Channel)) to select color n from the palette.

2. If  Type=Goal  we use the boolean bitand operator to set the Alpha value for the selected color to “96” without modifying the RGB values. Adjust this 96 value for an effect of your liking.

-Rob

Share

19 thoughts on “A Color Trick”

  1. We implemented this and it seems that the FieldIndex causes significant slow-downs in rendering the charts. Is there a way to get the same effect without the use of FieldIndex?

  2. Can this be used in combination with Combo chart? I have tried it with a combo chart, 1 line and 1 bar, but the different colors for each stacked bar was removed and replaced with one color.

  3. I think the your Combo Chart issue is due to the rules for stacking related to number of dimensions and expressions. While on the Style property pane, press the Help button and read up on the rules for what can be stacked and what can not.

    1. I faced a similar situation. But I have 3 dimensions in my case: Company, Metric and Year. The sum(value) of the metric is my expression. I have chosen year as my primary and Company as secondary dimension and the metric is stacked. I get only 2 colors when i use an expression like this:
      if(Company=SubField($(vCompany),’,’,1),argb(75,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)) , If(Company=SubField($(vCompany),’,’,2),argb(100,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),If(Company=SubField($(vCompany),’,’,3),argb(125,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),
      If(Company=SubField($(vCompany),’,’,4),argb(150,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),If(Company=SubField($(vCompany),’,’,5),argb(175,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),If(Company=SubField($(vCompany),’,’,6),argb(200,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),If(Company=SubField($(vCompany),’,’,7),argb(255,255,255,255) bitand color(FieldIndex(‘Metric’,Metric)),color(FieldIndex(‘Metric’,Metric)))))))))

  4. Thanks for the useful trick. One thing I noticed though is that the colour of the legend will follow the alpha-ed value instead of the “solid” one. Is there any workaround (other than masquerading a text object as the legend..)?

  5. My experience is that the legend will be the first color form displayed. In the blog example, if “Actual” were sorted before “Goal” the legend would appear solid.

    As you noted, there is always the trick of using another text object as the legend. I like to use a straight table for this.

    1. The same color expression will work in Qlik Sense in a “color by expression”. Note that if you copy/paste from this blog post you may need to correct the single quotes.

      1. See, I’m sure you’re right, I just can’t get it! Is there a place for “Background Color” in a QlikSense bar chart? I have looked everywhere and cannot find it. Thank you for taking the time to answer me!

        1. The QS bar chart color is in Appearance -> Colors and Legend -> deselect Colors Auto -> By Expression

          1. When I change that color section to “by expression” it gets rid of my stacked colors. I can use your argb format to get the select company to appear more/less opaque than the others, but I lose the stacks and the bars are just a single color. I didn’t know if there was another place where I could alter the background color instead of the primary color. Right now my stacks are colored based on the UI panel in my custom theme. Which is great! But I can’t figure out this opacity for selected company thing. I’m sorry to have so many questions!

          2. Probably best at this point if you could post your specifics on QlikCommunity where which allows for screenshots and attachments.

          3. Okay. I’ve done that a few times, but this particular question seems tricky because it gets views but not replies. Your post was amazing and I’m glad I found your site. Thank you!

Leave a Reply to Priya Cancel reply

Your email address will not be published. Required fields are marked *