How to autozoom graph to data?

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 …