Set time range for panel dynamically (autofocus, autozoom)


I am new here and I try to learn Grafana - and for this problem I need help.

We want to plot laboratory tests in Grafana. For example:

We do a lot of electrical tests and log the voltage as time series data. To choose the desired test I built a filter to select the “test_id” (see database example below). The data selection works good, but what we need is an autofocus at the panel (graph). Maybe, if we choose “test_id” 1, the focus should be between 2019-02-01 and 2019-02-02 (min and max timestamp from the filtered data). Do you have any solution for that?

Id | timestamp | value | unit | test_id
0 | 2019-02-01 | 21| Voltage | 1
1 | 2019-02-02 | 22 | Voltage | 1
2 | 2020-02-02 | 20 | Voltage | 2
3 | 2020-02-03 | 23 | Voltage| 2
4 | 2021-02-03 | 29 | Voltage | 3
5 | 2021-02-04 | 30 | Voltage| 3

Thanks! Best Regards. Patrick

Hi @patrick89

I don’t think there is a direct way to do this. Not currently, at least, but perhaps in the future.

I will try to think if there is a workaround way: in the meantime.


thanks for information! Maybe a workaround with the json? Using “TimeFrom” and “TimeTo” with a variable as input?

Whenever I try, it does not work…


Hi Patrick,

Please note you have not used any timestamp rather you have used a DATESTAMP.

As such Grafana sees DATE 00:00:00

You will need a combination of time and test id.

However, if you create a dynamic drop-down list variable for each test then selecting the test id will only show and focus on that test ID but the granularity will be by day. This is fine if you do one test per day but if you do several tests per day then you will need to log time or datetime. If you include date and time then using a testid dynamic drop down list will automatically selct and order the tests for that tesid in the cirrect order.


thanks for reply. Yes, the timestamp is a combination of a date and time - the example above is not really detailed, sorry.

In particular I am looking for a solution to do an autofocus for the graph. Maybe by giving the graph two absolute dates (or timestamps) in a variable to set the limits for the x-axis (min and max time) automatically, not manually.

Best Regards

Ok so the problem is that you have to load the entire dataset and then based on this dataset you wish to subquery or filter the dataset by test and then find the min and max DateTime stamps from the subquery or filter the DATETIME for the from and to for the x-axis.

It seems feasible but not all data sources allow subqueries

I would say it is not a query problem, but a visualization problem. It is absolute feasible to take the right data and filter for the startdate and the enddate - I have already done.

But I cannot find a solution to set the limits on the x-axis (panel, graph) automatically (with the startdate and enddate I got from the query).


Ok understood.

Depending on what data source you use they allow scaling to be performed. I think the max resolution or data point are tied to the panel as shown below as well as the interval all are settable.

I did play around with this and its works but to make it variable I think will be a mission unless you develop it yourself. But this is what I think is the source of your problem as it auto-scales the x-axis!

Thanks for information!

As @mattabrams said, there’s no direct way to do this currently. It’s not the first time it’s been requested on this forum though, so maybe worth making it a Github feature request, if one doesn’t already exist?

For now, the only workaround I can think of is introducing some custom JavaScript (e.g. in a Text panel), which (a) queries the data and works out what the time boundaries should be, and (b) sets the dashboard time boundaries as needed. The latter is not too difficult (happy to share some pointers). But the former, if feasible, is likely to be reasonably “ugly”.