'Grafana influxdb2 not showing correct legend label
Using Grafana 8 & influx db2 I want to combine two measurements & add the values
measurement 1:
from(bucket: "myBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "SCP" )
|> filter(fn: (r) => r["device"] == "device1")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
|> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC P" }))
This shows a time series in grafana with legend 'SC P'
measurement 2
from(bucket: "myBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "SCO" )
|> filter(fn: (r) => r["device"] == "device1")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
|> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC O" }))
This shows a time series in grafana with legend 'SC O'
Combined measurement
from(bucket: "myBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "SCO" or r["_measurement"] == "SCP")
|> filter(fn: (r) => r["device"] == "device1")
|> filter(fn: (r) => r["_field"] == "value")
|> group()
|> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
|> map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SC T" }))
This shows a time series in grafana with legend '_value', whereas I want it to be "SC T" The value itself is correctly calculated as the sum of both (after I added group() in the query)
My question : How can I get it to show the correct legend ( "SC T" i.s.o. "_value" ) ?
Remark 1 : As far as I can tell, the 3 queries return exactly the same structure of data Remark 2 : I can't use the override, as I have multiple of these queries in the same panel, all showing '_value' in the legend.
Solution 1:[1]
Managed to find the answer. All I had to do was add this line at the end
|> map(fn: (r) => ({ time : r["_time"], "SC T" : r._value}))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kermit754 |

