Associating persistent colors to field values

The best practices say that a designer should avoid using bar charts with multi-colored bars or having too many colors in any of your chart objects. But at times, we need to cater to the demands of the organization and take an uncalled for approach to designing. The following recipe explains how to associate distinct field values to different colors in the Qlik Sense script.

Getting ready

This recipe serves to be a good example to demonstrate the use of Pick function in the script. Use the following steps to get started:

  1. Create a new Qlik Sense application and name it Persistent Colors.
  2. Open the data load editor.
  3. Load the following script that contains information about the Actuals and Budget of four products. The script is available for download on the Packt Publishing website:
    ProductsTemp:
    LOAD * INLINE [
    Product, Actuals, Budget
    Footwear, 100000, 120000
    Tyres, 180000, 150000
    Mountain Bikes, 250000, 195000
    Road Bikes, 200000, 225000
    ];
    
    Products:
    LOAD *,
    pick(match("Product", 'Footwear', 'Tyres', 'Mountain Bikes', 'Road Bikes'), RGB(236,129,0),RGB(250,185,0), RGB(70,137,164), RGB(141,25,8)) as "Product color"
    
    RESIDENT ProductsTemp;
    
    Drop table ProductsTemp;

How to do it…

  1. Open the App overview and create a new sheet.
  2. Create a bar chart on the sheet.
  3. Use Product as dimension.
  4. Use Sum (Actuals) as measure. Label it as Actuals.
  5. Under Colors and Legend, switch off Auto Colors to activate custom colors.
  6. Along with the custom colors, a dropdown to define the colors is also activated. This is situated right below the colors switch. Under this dropdown, select By expression.
  7. Add the following expression under the color expression:
    =[Product color]
  8. Make sure that The expression is a color code is checked.
  9. The result would be as follows:
    How to do it…

How it works…

The Pick function used in the script links values in the Product field to distinct RGB values. Each product is displayed in a different color bar when the Product color field is used in the color expression of the chart.

There's more…

Persistent colors can also be obtained through the chart properties when we select the colors by dimension. However, using this approach we can't have custom colors but have to depend on the color scheme in Qlik Sense.

See also

  • Use of ColorMix1 function to establish a color gradient in charts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset