Good morning,
I have been trying to monitor the naps of my newborn and plot them as a State Timeline. Please see screenshot below:
My intention is to have on the y axis the last 7 days (according to the time range selected in the dashboard). On the x axis I am trying to have a fixed axis that starts from 00:00 and ends at 23:59.
With such a configuration I would be able to compare days and highlight patterns.
I am almost there, what is missing is the fixed x-axis. As you can see, the axis is rolling with the advancing of the day. How can I keep it fixed.
Please see below my current Flux query to take thata from InfluxDB:
import "date"
import "strings"
import "timezone"
option location = timezone.location(name: "Europe/Rome")
base =
from(bucket: "baby-care")
|> range(start: -7d)
|> filter(fn: (r) =>
r._measurement == "baby_events" and
r.event_type == "Nap"
)
|> pivot(
rowKey: ["_time", "parent", "user_id"],
columnKey: ["_field"],
valueColumn: "_value"
)
|> map(fn: (r) => ({
r with
start_time: time(v: r.start),
stop_time: time(v: r.stop),
}))
|> map(fn: (r) => ({
r with
start_day: date.truncate(t: r.start_time, unit: 1d),
stop_day: date.truncate(t: r.stop_time, unit: 1d),
}))
|> keep(columns: ["start_time","stop_time","start_day","stop_day"])
// Segment that belongs to the start day (up to either the stop or end of that day)
segStartDay =
base
|> map(fn: (r) => ({
r with
day_label: strings.substring(v: string(v: r.start_day), start: 0, end: 10), // YYYY-MM-DD
seg_start: r.start_time,
seg_end: if r.stop_time < date.add(d: 1d, to: r.start_day)
then r.stop_time
else date.add(d: 1d, to: r.start_day),
}))
|> filter(fn: (r) => r.seg_end > r.seg_start)
|> keep(columns: ["day_label","seg_start","seg_end"])
// Segment that belongs to the stop day (from its midnight to the stop time), only if different day
segStopDay =
base
|> filter(fn: (r) => r.stop_day != r.start_day)
|> map(fn: (r) => ({
r with
day_label: strings.substring(v: string(v: r.stop_day), start: 0, end: 10), // YYYY-MM-DD
seg_start: r.stop_day,
seg_end: r.stop_time,
}))
|> filter(fn: (r) => r.seg_end > r.seg_start)
|> keep(columns: ["day_label","seg_start","seg_end"])
// Combine and rebase each segment’s time-of-day onto TODAY’s midnight
segments =
union(tables: [segStartDay, segStopDay])
|> rename(columns: {seg_start: "_time", seg_end: "timeEnd"})
|> map(fn: (r) => ({ r with _value: "Nap" }))
|> keep(columns: ["day_label","_time","timeEnd","_value"])
// Today’s local midnight for rebasing
today0 = date.truncate(t: now(), unit: 1d)
segments
|> map(fn: (r) => ({
r with
_time: time(v: int(v: today0) +
(int(v: r._time) - int(v: date.truncate(t: r._time, unit: 1d)))),
timeEnd: time(v: int(v: today0) +
(int(v: r.timeEnd) - int(v: date.truncate(t: r.timeEnd, unit: 1d)))),
}))
|> group(columns: ["day_label"])
|> sort(columns: ["_time"])
Also I have set the following query options:
Any suggestion would be very much appreciated.
My current Version is: Grafana v11.6.0
Thanks a lot!