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

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

0 2 227
2 REPLIES 2

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.

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

Top Labels in this Space
Top Solution Authors