Change title based on condition (Repeating Panel)

I need to change the title of a panel based on a variable which the panel repeats over.

For example:

Repeating variable var can take values: 1, 2, 3, 4, 5.

When var = 1, title of panel should read: “blah blah Precision blah”

When var = 2, title = “blah blah Accuracy blah”

When var = 3, 4, or 5, then title = “blah blah Speed blah”

Is there a syntax to support this in Grafana titles?

you can configure a variable with a value and a label, and then you can reference the value or the label individually:

variable
1:Precision, 2:Accuracy, 3:Speed, 4:Speed, 5:Speed

Then you can reference ${variable:value} for 1,2,3 etc. or ${variable:label} for Precision etc.

1 Like

@sowdenraymond

And with value…

What am I doing wrong? (I’ve hit Apply each time)

The variable setup is very touchy, spacing has to be exact
env1 : host1, env2 : host2

1 : Precision, 2 : Accuracy, 3 : Speed, 4 : Speed, 5 : Speed

1 Like

@yosiasz Thanks! Is it still possible to access the labels within queries?

e.g.

In title: ${variableName} gives me the label, e.g., 2

Using the exact same syntax in the queries gives a different result: the values. e.g., ‘Precision’

My queries need to use 1, 2, 3, 4, 5. It would be impossible to swap the values and labels, since ‘Speed’ for example would need to map to 3 different values.

Edit: I’ve tried ${variableName:label} and ${variableName:value} in the queries. Always produces the value.

Try

${variableName:text}

Query should be

select name as __text, idnumner as __value

1 Like

Thanks again! Is there an equivalent for getting the value?

I can only get the label (2) in the title, when I need the value (Precision)

I’m using ${variableName:value}

(Also, ${varName:text} worked, but now it works without using this i.e. just $varName is giving the label in the queries now… very strange)

Time to read the docu

:fishing_pole_and_fish:

@yosiasz I’ve read that multiple times since @sowdenraymond 's response above. It doesn’t provide a single example where the variable is a key-value pair, so it’s not very helpful (that’s also why it didn’t occur to me to try the “text” option - it’s not at all clear that the “text” representation is just the label).

Is this even possible in grafana?

@sowdenraymond

Is this true?

2 Likes

Oops sorry meant to share this one

:fishing_pole_and_fish:

@sowdenraymond Yes!! Thank you for answering my question. It seemed to not be working in my case because of the way the variables were repeating on my dashboard (It was only repeating for keys which had the same values).

Btw: I believed you, until it didn’t work. Your first message:

does not work (also didn’t say anything about the spacing - thanks @yosiasz ).

And the documentation (which I went straight to after seeing that first message) is not sufficient to answer this question. It makes no mention of ‘value’ as a format, and the information for ‘text’ makes no reference to how it behaves when using key : value variables like this.

I’m interested to know how you even knew this was possible. Thank you! @sowdenraymond

@yosiasz I see this page mentions the correct spacing, but have not seen any mention of how to extract the key or value.

I went to update the documentation (first-time open source contributor here). After seeing Get started with writing documentation | Writers' Toolkit documentation I’m not sure I have time for this right now.

My update would have been something like the following, made to …/docs/…/variable-syntax/index.md

Text

Formats single- and multi-valued variables into their text representation. For a single variable it will just return the text representation. For multi-valued variables it will return the text representation combined with +. For key-value variables (i.e. ‘test1 : 3’) it will return the key.

servers = ["test1", "test2"]
String to interpolate: '${servers:text}'
Interpolation result: "test1 + test2"

Value (Key-value variables)

Returns the value of a key-value variable.

servers = ["test1 : 3"]
String to interpolate: '${servers:value}'
Interpolation result: "3"

Query parameters

Formats single- and multi-valued variables into their query parameter representation. Example: var-foo=value1&var-foo=value2

servers = ["test1", "test2"]
String to interpolate: '${servers:queryparam}'
Interpolation result: "var-servers=test1&var-servers=test2"
1 Like

Indeed! The value part is missing
Edit it and submit your change

1 Like

i saw a post by one of the other grafana champions:

1 Like

Ah nice. Thanks again :slight_smile:

It should be possible to have the key determined by the value of another variable. The current solution is limited to one key-value mapping if someone is repeating rows/panels over the variable (i.e. me lol, at least I only need one mapping right now :sweat_smile:)

If I have time later I will open a feature request (maybe it exists already…?)

Also @yosiasz I submitted the pull request for the documentation.

is this not working?

That is working. If I need more than one mapping (example below), it will not work because my whole dashboard repeats over the variable (which is limited to just one key-value mapping).

A solution is to reference keys based another variable (like is normally possible in programming e.g. dict[var], or list[var]).

Here’s an example of what I mean should be possible:

Panels/rows repeat over the following variable:

Name: Level
Values: 1, 2, 3, 4, 5

Variables which depend on Level (used within repeating elements):

Name: Difficulty
Values: 1 : Easy, 2 : Medium, 3 : HOOBOY!!!, 4 : MediumHard etc.

Name: Length
Values: 1 : 200, 2 : 250, 3 : 410, etc…

Syntax for referencing might look something like this:

${Difficulty:{$Level}} - “difficulty of level”
${Length:{$Level}} - “length of level”

use the power of a database

image