[Tutorial] Custom colors based on labels

I’ve been searching all over for a way to accomplish a specific color customization, and haven’t found any help online. I have now figured out on my own how to accomplish my goal. But in my search I found many other folks asking for something like this, so I want to share my findings here.

I’ve tested this approach with the Gauge Panel, but it should work with any panel that has the “Color scheme” option.

So, here’s what this tutorial will help you accomplish:
You have a visualization with a number of results (fields) being displayed in it. Each result has some label attached to it that encodes a sort of “category”. You want to give each category its own color. Notably, you might have a really large number of results, but the number of distinct categories is actually pretty small.

I’ll illustrate this with an example: I have a data source that gives me information on some items I have in stock. I might have a large amount of items, and items get added and removed all the time. Each item has a label called “vendor” which tells me the vendor I got the item from - and there are only about 5 different vendors across all items. I want to color-code the items such that each vendor has a distinct color.

This is where we start: I’ve just created a new Gauge panel and put in my query. (I have a Prometheus datasource, but this guide should also work for other datasources)

Here’s what we’ll do:

First, you have to make sure that the category you want to use to color your results is what’s displayed in the legend. It’s okay if you want the legend to be something else - we will fix the legend later. But first, the category has to be the legend.
Since I’m using a Prometheus datasource, I can customize the legend directly in the Query window.

If you use something other than Prometheus, you’ll have to figure out how to do it for your specific data source. Unfortunately, customizing the Display Name in the visualization options does not work - you have to do it as part of your query!

The panel should now look something like this:


The labels aren’t very helpful but don’t worry, we will fix that at the very end! Now we get to coloring.

In the visualization options, go to the “Overrides” tab and add a new field override. Make it a “Fields with name” override and choose the first of your categories. I’ll choose one of my vendors.

Next, click “Add override property” and search for “color”. You should find the “Standard options > Color scheme” property. Add that.

A new drop-down appears. Change it to “Single color”. Now, a color picker appears next to the drop-down - click it to customize your color for that category.

Add more field overrides (by repeating these steps) until you’ve configured a color for every category. In the end, it should look something like this:

Now it’s time to fix the labels so they display something useful again. For this, we add one final field override. (The order is important here! The override which fixes your labels MUST be the last one.)

This one should be a “Fields with name matching regex” override. You can leave the default /.*/ regex - this matches everything, which is exactly what we want here.
Add an override property to this new override. This time, we want to override the “Standard options > Display name”.

We’ll now set the display name to whatever we want. You can access all other labels on your results by using the ${__field.labels.[labelname]} substitution - the documentation has more info on this: Configure standard options | Grafana documentation

My results happen to have a name label that I want to use, so I set this field to ${__field.labels.name}

Here’s what it looks like in the end:

I hope this proves helpful to some of you. I really wish there was an official “set colors based on labels” feature - or better yet, an official “field override based on labels” feature, so we wouldn’t need this ugly workaround of changing the display name and later changing it back.

Have a lovely week! :smiley:

PS: Unfortunately I had to remove most of the pictures in the guide due to the media attachment limit for new users. I hope the instructions are still clear enough even without the pictures.

1 Like