How to autozoom graph to data?

Hello everyone, pretty new to Grafana with PostgresSQL and so far I love it! I will be presetting to our company the capabilities of Grafana next week and proposing to switching onto this platform - but I need help with one little thing:

I want to create an environment where I can show processes from industry plant. The dashboard should look something like this at the end (our current application):

So far I got to the point where I can select process numbers based on a query variable.

The problem I am facing right now is that the data shown in the graph doesn’t automatically zoom in to data. This can be a problem because processes can be years old. And the user shouldn’t scroll and “look” for the process through all the years.

Please tell me that there is a way how to do this?
I have been searching on the internet and couldn’t find any answers (I am limited to 2 links so I will put the rest into comments):

  1. https://community.grafana.com/t/set-time-range-for-panel-dynamically-autofocus-autozoom/48386
  2. https://community.grafana.com/t/auto-zoom-on-available-data/58549

Other links about this issue:
3) Time range controls | Grafana Labs
4) [Dashboard time range]: Automatically find time range for data in graphs ¡ Issue #6288 ¡ grafana/grafana ¡ GitHub

I hope I will be proven wrong, but I had a similar question a few weeks back and the answer was that it cannot be done.
You can run the make a panel to user a relative time, which means that it will always fix to the last minutes, hours that you specify:

But it is always a range that ends in the current time. So if your process completes 5pm and you are running the report at 6pm, there will a 1 hour empty space in your graph. Similarly it is always a fixed period to the past. I wanted a graph which zooms in the data on my solar panels, which always start and stop depending when the sun rises and sets in the day. And I could not do it.

I hope you find a solution, it there will be a solution for that in the future. For me it seems what we need is not so much a setting on the graph, but some option to programmatically influence the time range on the dashboard. But in my understanding, this is not possible at the moment. Only if we could add a script to the dashboard page source…

2 Likes

Ah … I can’t believe such a simple yet so powerful feature isn’t added! :frowning:

I hope someone can come up with a solution!

Isn’t it somehow possible for example to connect Y axis to variables?

Actually, have a look at this: GitHub - WilliamVenner/grafana-timepicker-buttons: 📊 Datasource-configured buttons panel plugin which set the time range of your Grafana dashboard

Just spend a few minutes scrolling through it, but it looks like a bespoke panel type where you can have buttons on your panel, and that will change the time range. So your button can determine the start and end of your data range and set the time range to that. Let me know if you can get this working.

I have stumbled across this plugin myself but idk how I could make it do what I want:

I don’t thing adding those buttons will automatically adjust the time range … user still manually has to type in the dates doesn’t he?

Or can I somehow make the buttons default value set up by query?

Yes, you are probably correct. But at least it auto zooms the time range. Maybe there is another plugin somewhere which can press the button for you :slight_smile:

Hmm maybe I could use the code for autozooming and just feed it a value programmatically.

Issue with this is that I can use this only on desktop version and not in cloud am I correct?

Just a bump on this topic. I’m also looking at solar data, but my information comes from Influx (2.6) and the time range is selected by the query (It presents yesterday’s data before 10AM and today’s after 10) and always displays from 6AM - 8PM of whichever day. The query is also set up for overlaying multiple days of data time-shifted to the “current” day.

I’m looking for an unattended option that will just zoom to results from the query; is that not possible?

Editing my thoughts from above slightly-- Looking into RelativeTime and Time Shift I understand that I can do now/d to get to midnight, but I would like a start time of 6AM. If I enter now/d + 6h the + 6h is truncated off.

It doesn’t seem like the selectors are sufficiently robust to do what I am doing in Flux (including if-then statements).

So an auto refresh feature that also does 6 to 8 of today()?

Does it shift left with the clock?

The query does everything I need it to do; the easy solution is to just have the panel display all query results rather than trying to manage zoom from a separate setting.

date = if date.hour(t: now()) >= 10 then today() else experimental.subDuration(d: 24h, from: today())
startoffset = 6h
duration = 14h
start = experimental.addDuration(d: startoffset, to:date)
stop = experimental.addDuration(d: duration, to: start)
startYesterday = experimental.subDuration(d: 24h, from: start)
stopYesterday = experimental.subDuration(d: 24h, from: stop)

(I run two queries, one for “today” and one for “yesterday”, and I timeShift the yesterday data into today so things overlay.)

When the refresh runs, the panel doesn’t shift, as it is already padded out to 8PM today by the yesterday information. [It works as expected when I run it in Influx.]

The problem is that I can’t figure out how to make Grafana “trust” that the time information all needs to be displayed (and nothing else).

1 Like

Hi, i know that topic is near 2 years old but i searched grafana (& google) forum and could not find a idea how to “autozoom graph to data” so i finally done that by myself :slight_smile: …i hope i didnt developed a wheel again but anyway i will paste how i did it and maybe someone find that useful

i have simple graph (# of client through time) …takes data from MSSQL based on mien own start date and end date picker

now when You change any dates (in start date or end date picker) it will show graph only for selected period and zoom graph to data

important here is that You dont need use default dashboard data picker to zoom graph and all is automatically counted /zoomed

  1. I defined 4 dashboard variable startD, endD, aaa, bbb. I know aaa & bbb are not good names for variable but are here only to be good visible in below explanation …also You can hide them from dashboard as they are counted in real time based on startD & endD selection and dont need to be presented on life dashboard

-startD & endD variable are query from the same TABLE as graph it self …just distinct/ grouped by Date …one Ascending order second in descending order (only cause i still not found how to in query select visible value …mean that i want see just 2nd from list …no i can t save as dashboard default value as list is dynamically created when dashboard is opened …but this another story to grafna developers)

SELECT FORMAT(Date, ‘yyyy-MM-dd’)
FROM <TABLE_NAME>
GROUP BY Date
ORDER BY Date

  • aaa (Relative time) query variable counting in days different between end and start date (for example 60 days) …and add “d” to end so after all $aaa is “60d”

SELECT CONCAT(DATEDIFF(day, CAST(${startD:singlequote} as smalldatetime), CAST(${endD:singlequote} as smalldatetime)) + 1,‘d’)

  • bbb (Shift time) variable counting in days different between TODAY/NOW date and end date (for example 3 days) …and add “d” to end so after all $bbb is “3d”

SELECT CONCAT(DATEDIFF(day, CAST(${endD:singlequote} as smalldatetime), GETDATE()),‘d’)

  1. mine graph is created by query …

SELECT
$__timeEpoch(Date),
‘# of clients’ as Client,
COUNT(ClientName) as Value
FROM <TABLE_NAME>
WHERE
Date >= CAST(${startD:singlequote} as date) AND Date <= CAST(${endD:singlequote} as date)
GROUP BY Date
ORDER BY Date

  1. for above query i must use “query options” (1)" just to avoid that graph utilize main dashboard date picker …is lest say override :slight_smile:
  • just set “Relative time”(2) to dashboard variable $aaa
  • just set “Time Shift”(3) to dashboard variable $bbb
  • can set also “Hide time info” (4) so You will have no CLOCK icon & text “Last 47 days timeshift -2d” on top of graph

and that’s it …now any time i select need by me range for graph it will automatically zoom it to show it from start date to end date and nothing more …