Gradient colouring of +- data, centered around zero


Using Grafana v9.2.5, reposting this since my first post got auto-deleted by the spam filter :angry:

I’m trying to graph values deviating from zero using the gradient colouring in Grafana.

I can’t get this to work how I want. I have values ranging from ± x (say, 2% either side of zero) and I want to colour them based on their distance away from zero.

Essentially I want a gradient of [red, yellow, green, yellow, red], mapped to [-2%, -1%, 0%, +1%, +2%].
The in-built gradients don’t seem to support this, they are only one-way. If I try mapping my values to the red-yellow-green gradient, it maps negatives as green, around 0 as yellow, and positives as red:

I can get the colour mapping working correctly if I also include a helper column in my SQL that grabs the absolute value, and use the “Color by field” setting in grafana to colour the non-ABS data with this scale. That way I can colour it according to the ABS(), giving green around 0 and red as it deviates away from 0 in either direction.

The problem with this is that in order for the colour scale to work this way, I need to set graph min as 0, and max as +2. This prevents me from viewing the entire negative side of the plot.

If I map the ABS(val) to the green-yellow-red gradient and change the minimum to -2, then I can see the numbers correctly but the gradient is incorrect:

This is because green-yellow-red is mapped to the full range of [-2, +2] and I’m only colouring according to the ABS which is between 0 and +2.

The only way I can get this to work is by using the symlog scaling mode, setting the linear region to my full data range, and setting the min/max to [0 to 2]. Somehow in this mode, it will still show both sides of the graph regardless of my min/max settings, and then the gradient will map correctly:

With these settings there are new problems:

  • The axis bugs out - it only shows the center-point and no other points or gridlines, even if I force them on.
  • I can’t control the maximum values in the plot. If I want to show +10 to -10, I have no way of doing that. Even if I change the max to +10, the gradient will successfully map to this new range but the above value of 1.5% will still be the same size, it doesn’t scale back. It seems stuck on some sort of auto-scaling.
  • This seems wrong to be using symlog scaling as a hack to get the gradient colouring to work.

Is there any way to do this correctly with linear scaling mode, gridlines, and adjustable graph axes min/max?

Thanks :slight_smile:

@bobblybook What visualization are you using to produce the graph that you have shown? Please share as many of the settings as possible.

Hi, sure.

Graph is a Bar Chart.

Bar chart:
Orientation: Horizontal (doesn’t matter, vertical works the same way)
Show values: Always
Stacking: Off
Color by field: abs(val) (my sql helper column)
Gradient mode: None

Visibility: Off

Text size:
Value: Auto

Placement: Left
Width: Auto
Show grid lines: On
Color: Text
Scale: Symlog
Log base: 10
Linear threshold: 1
Centered zero: Off (doesn’t make any diff)
Soft min: -0.02
Soft max: 0.02

Standard options:
Unit: Percent (0.0-1.0)
Min: 0
Max: 0.02
Decimals: 1
Color scheme: Green-Yellow-Red (by value)

Sample data table: