Color by expression
Coloring by expression sets colors using a user-defined expression. When coloring by expression, you can define both what colors to use and which values to use them with, enabling more control over how colors are used in the visualization.
For example, you might highlight values of particular interest, or differentiate between values within different value ranges. Coloring by expression can also be used to color a visualization by values not included within a visualization, such as coloring products and the sum of their monthly sales by the country of origin for the product.
When you select to color By expression, you can chose to either use the expression as a color code or to define how By measure color options are applied to the visualization using an expression.
The following visualizations support color by expression:
- Bar chart
- Combo chart
- Line chart
- Pie chart
- Scatter plot
Coloring by expression as a color code
By default, if you choose to color by expression, The expression is a color code is enabled. If you have this option selected, your expression must include a color code in a supported expression format to define the colors to use. Using this method provides you with manual control over visualization colors as well as the conditions for the colors being used in a visualization. With tables and pivot tables, you can use expressions to define the background color and the text color of columns.
Here are a few examples to show what you can do with expressions by color.
Example: Coloring by random color range
This example uses
Example: Coloring by single measure value
if(sum([Budget Amount]) > 1000000, 'cornflowerblue', magenta())
In this example, there is a condition. If
magenta() is the Qlik Sense color function that generates a magenta color.
Example: Coloring by single measure value using an aggregated expression
if(avg(Value) > avg(Total aggr(avg(Value), Name)), Blue(), Brown())
In this example, there is a condition. If the
Example: Coloring by multiple measure values
if(Sum(Sales) > 3000000, 'green', if(Sum(Sales) > 2000000, 'yellow', if(Sum(Sales) > 1000000, 'orange', red())))
In this example, there are multiple conditions. If Sum(Sales) is greater than 3,000,000, then corresponding measure values will be colored green. If Sum(Sales) is between 2,000,000 and 3,000,000, then the corresponding measure values will be colored yellow. If Sum(Sales) is between 1,000,000 and 2,000,000, the corresponding measure values will be colored orange. All other measure values will be colored red.
Example: Coloring by multiple dimensions
if([CompanyName]= 'A Corp', rgb(100, 149, 227), if([CompanyName]= 'B Corp', rgb(100, 149, 200), if([CompanyName]= 'C Corp', rgb(100, 149, 175), if([CompanyName]= 'D Corp', rgb(100, 149, 150), 'grey'))))
In this example, the expression is used to define a RGB color for each specific dimension value in the field CompanyName.
Example: Coloring table object font and background by measure value.
if(Sum([Sales]) < 10000, 'red', green())
if(Sum([Sales]) > 200000, 'gold', )
In this example, two expressions are used to color the background and text of the Sales column. Measure values in Sales that are lower than $10000 have a red background color, all other values have a green background. In addition, the values that are higher than $200000 have the text color '
Coloring by expression without a color code
You can color by expression without a color by disabling The expression is a color code when you enable coloring by expression. In this method of coloring, the expression is used to evaluate to a numeric value that is plotted against a By measure chart gradient, treating the expression like a measure when coloring by measure.
100*Sum([Sales Margin Amount])/Sum([Sales Amount])
In this example, charts would have By measure color gradients applied to them based on the profit margin percentile calculated in the expression.
Supported expression formats
The following formats are supported when you create your expressions.
This example generates the color blue. Many of the
The first value (125), sets the alpha value. The value 0 generates full transparency and the value 255 full opacity.
This example generates a red color with medium saturation and lightness.
Qlik Sense supports
Use the following links to find out more about
This example generates a blue color with the hex value of #6495ed and a
Qlik Sense color functions
The following color functions can be used in expressions when coloring by expression.
Creating an expression
You create expressions for colors in the properties panel.
Do the following:
In the properties panel, open Appearance > Colors and legend.
Click the Colors button to switch to Custom.
In the drop-down list, select the option By expression.
An expression text box is opened.
Enter your expression in the text box, or click to open the expression editor.
If the expression is valid, the visualization is updated.