Question

How to set conditional colors for a data point based on a range?

  • 14 March 2020
  • 2 replies
  • 145 views

How about conditional colors for a data point based on a range? I’m working on an app ratings dashboard, so the range would be between 1 - 5 stars going from Red to Green.


image


2 replies

measure: your_1_to_5_measure {
type: average
sql: ${name_of_field} ;;
html:
{% if value < 2.5 %}
<p style="color: red; text-align:center">{{ rendered_value }}</p>
{% else %}
<p style="color: green; text-align:center">{{ rendered_value }}</p>
{% endif %};;
}

I’m assuming this measure is an average.


…you could also do something like this if you want a range of colors


measure: your_1_to_5_measure  {
type: average
sql: ${retail_price} ;;
html:
<p style="color:
{% if value < 2 %}red
{% elsif value < 3 %}pink
{% elsif value < 4 %}lightgreen
{% else %}green
{% endif %}
; text-align:center">{{ rendered_value }}</p>
;;
}

HTML ignores whitespace so you can put the logic right inside the style tag.

Userlevel 7
Badge +1

Moved to a new topic for searchability / solution tracking. I’ll be doing this more now! Carry on.

Reply