Legend change issue with Grafana/Flux

I am having an issue with changing the legend of my graph to something meaningful.

I have this flux query:

from(bucket: “xianenergyd”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: (r) => r._measurement == “xianenergy”)
|> filter(fn: (r) => r._field == “AC21_W” or r._field == “AC22_W” or r._field == “AC11_W” or r._field == “AC12_W”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> pivot(rowKey: [“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> map(fn: (r) => ({ r with _value: r.AC21_W + r.AC22_W - r.AC11_W - r.AC12_W}))
|> set(key: “_field”, value: “BATTERY”)
|> yield(name: “AC”)

But the resulting legend is:

BATTERY {AC11_W=“some value”,AC12_W=“some value”,AC21_W=“some value”,AC22=“some value”} Current: 415 W

How do I get rid of the info between the 2 brackets {} ?

I tried overrides but that does not work as the value changes and it comes back to showing the brackets.

Welcome @christianjoly

Try the Rename by Regex transformation. I created many examples here:

I checked the link. you sent but this is way beyond my level of expertise

Would you have a regex suggestion for my example ? Where do you place the regex ?

@christianjoly

Pretty sure that Gemini has this correct, but give it a try:

Thank you. I adapted it and that transform works.

I upgraded my Grafana to version 10 and now it no longer works.

I have the query below and tried to do a labels to fields transformation but I cannot see the _label in the list of transformation:

Thoughts ? Thank you

What version did you migrate from?

What do you mean by - it’s not working

Migrated from 7 to 10

By not working, you can see on the bottom graph, the legend is _value

I added a column _label and I should be able to a transformation labelstofields by selecting the _label column but I cannot see it in the list of transformation.

Basically, I want to display Solar (the _label field) instead of _value

1 Like

Can you remove line 9 from your query and then post the results here in Table format, as well as a screenshot from Grafana?

There it is. If I remove the map line to rename the series, it will change it to xianenergy which is the name of my dashboard.


Disregarding the incorrect labels for the moment, do these 4 values make sense, and if yes, what should each of these colors be described as in the legend? Do you expect 4 values or something different?

The values are good. My database is in Watts. I have 4 queries for this graph. I am showing the query for the yellow one. (The others are the same with different _fields. The Line7 is to convert W to Wh. And Line8 is to aggregate over a month. So this is working. What is not working is the Legend/Label

Can you answer this question?

Yellow: Solar
Green: Grid
Blue: Main
Orange: Battery

That is why I created a new Column in the table _label so that I could do a transformation with Labels to Fields - But that does not seem to work

I see from the screenshot that you have 4 queries. Do the 4 colored bars represent the output from these 4 queries?

Second, what other choices are in this dropdown box? (the one that shows ‘xianenergy’)

Yes. The colored bar represents the output of the 4 queries with the Yellow bar(Solar) being the query I showed above.

There is one choice for each query. As I removed the map _label on QueryB, it shows xianenergy. for the others, it shows _value, _label

OK, for the sake of being comprehensive, can you paste the 4 queries here? (please use the correct formatting for the text here in the forum).

QueryA:

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "MAIN1_W" or r._field == "MAIN2_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value: r.MAIN1_W + r.MAIN2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> map(fn: (r) => ({_value: r._value, _time: r._time, _label: "Grid"}))
  |> yield(name: "GRID")

Query B

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "SOLAR1_W" or r._field == "SOLAR2_W")  
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value: r.SOLAR1_W + r.SOLAR2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> yield(name: "SOLAR") 

QueryC

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "HOUSE1_W" or r._field == "HOUSE2_W" or r._field == "GARAGE1_W" or r._field == "GARAGE2_W" or r._field == "PI1_W" or r._field == "PI2_W" or r._field == "POOL1_W" or r._field == "POOL2_W" or r._field == "HVAC1_W" or r._field == "HVAC2_W" or r._field == "SHED1_W" or r._field == "SHED2_W" or r._field == "EV1_W" or r._field == "EV2_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value:  r.HOUSE1_W + r.HOUSE2_W + r.GARAGE1_W + r.GARAGE2_W + r.PI1_W + r.PI2_W  + r.POOL1_W + r.POOL2_W + r.HVAC1_W + r.HVAC2_W + r.SHED1_W + r.SHED2_W + r.EV1_W + r.EV2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> map(fn: (r) => ({_value: r._value, _time: r._time, _label: "Main"}))
  |> yield(name: "MAIN")

QueryD

 from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "AC21_W" or r._field == "AC22_W" or r._field == "AC11_W" or r._field == "AC12_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value: r.AC21_W + r.AC22_W - r.AC11_W - r.AC12_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> map(fn: (r) => ({_value: r._value, _time: r._time, _label: "Battery"}))
  |> yield(name: "BATTERY")

Can you make these changes and see if that helps? Note that in all cases, I redid your map function to explicitly create a new property (grid, main, etc.), like this:

|> map(fn: (r) => ({ r with newColumn: r._value * 2 }))

and got rid of the final map function in each query. Query C might still require some finessing.

Query A:

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "MAIN1_W" or r._field == "MAIN2_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with grid: r.MAIN1_W + r.MAIN2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> yield(name: "GRID")

Query B

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "SOLAR1_W" or r._field == "SOLAR2_W")  
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with solar: r.SOLAR1_W + r.SOLAR2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> yield(name: "SOLAR") 

QueryC

from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "HOUSE1_W" or r._field == "HOUSE2_W" or r._field == "GARAGE1_W" or r._field == "GARAGE2_W" or r._field == "PI1_W" or r._field == "PI2_W" or r._field == "POOL1_W" or r._field == "POOL2_W" or r._field == "HVAC1_W" or r._field == "HVAC2_W" or r._field == "SHED1_W" or r._field == "SHED2_W" or r._field == "EV1_W" or r._field == "EV2_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with main:  r.HOUSE1_W + r.HOUSE2_W + r.GARAGE1_W + r.GARAGE2_W + r.PI1_W + r.PI2_W  + r.POOL1_W + r.POOL2_W + r.HVAC1_W + r.HVAC2_W + r.SHED1_W + r.SHED2_W + r.EV1_W + r.EV2_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> map(fn: (r) => ({main: r._value, _time: r._time, _label: "Main"}))
  |> yield(name: "MAIN")

QueryD

 from(bucket: "xianenergyd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "xianenergy")
  |> filter(fn: (r) => r._field == "AC21_W" or r._field == "AC22_W" or r._field == "AC11_W" or r._field == "AC12_W")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with battery: r.AC21_W + r.AC22_W - r.AC11_W - r.AC12_W}))
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> aggregateWindow(every: 1mo, offset: 7h, fn: sum, timeSrc:"_start")
  |> yield(name: "BATTERY")
1 Like