I’m not sure if you found a resolution to this but I had the same issue. Posting this because it was quite a struggle to find similar posts and your issue seemed exactly the same as mine but without resolution. I’m not sure if my solution is the recommended solution but it did change the behaviour to what was expected by my users.
Using Influx 2.1.1 and Grafana 8.2.4. I was using 1 minute windows with 5 second refresh so it was likely easier to visualize the issue. My original query was:
from(bucket: "myBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "jvm_memory_used_bytes" and r["area"] == "heap")
|> aggregateWindow(every: 1m, fn: spread)
Bars would gradually increase in size to become the expected size then suddenly drop to being very thin then repeat the process:
I noticed that when the time rolled just past the minute Influx would produce a window with a very narrow time frame. As that window increased, so did the width of the bars. Grafana seemed to be rendering the bars as thin as the shortest time frame.
I was able to eliminate the short most recent window using experimental.addDuration for the stop argument to extend the window to one minute beyond the latest full minute, however, the results then also included a tiny window for the minute beyond. Adding “createEmpty: false” to the aggregation got rid of that window since there was no data.
The bars maintained consistent width at that point but the oldest bar in the graph would start to go down as the data began to exceed the lower bounds of the query period causing some additional confusion for my users. This was alleviated using experimental.subDuration on the start of the range. My final query ended up being:
import "experimental"
from(bucket: "myBucket")
|> range(start: experimental.subDuration(d: 1m, from: v.timeRangeStart), stop: experimental.addDuration(d: 1m, to: v.timeRangeStop))
|> filter(fn: (r) => r["_measurement"] == "jvm_memory_used_bytes" and r["area"] == "heap")
|> aggregateWindow(every: 1m, fn: spread, createEmpty: false)
It should be noted that the subDuration portion worked when used from Grafana, however, when I attempted to use it in the Influx Data Explorer it failed with “expected time but found duration (argument from)”. I’m not entirely sure why but I have seen some mention of changing v.timeRangeStart to be a timestamp here which seems to possibly have been changed around Oct 2021. I suspect this may work from the data explorer in more recent versions?