Build a time-series graph in Grafana
Because the time-series graph is the most common graph in Grafana, it’s also the default panel type.
With a time-series graph, you can answer questions like:
- What is the hourly stock price of AMD today?
- How many users visited a website page each day in the past week?
- What was the temperature yesterday?
To plot a time-series graph, Grafana requires you to provide a time column and a value column. To plot multiple time-series graphs in a single panel, you need to provide multiple value columns.
This is an example of valid time-series data:
This tutorial shows you how to:
Prerequisites
Before you begin, make sure you have:
- Installed version 8.5 or higher
- Installed TimescaleDB
- Imported the stock trade data from the
If you are new to Grafana, see the Grafana tutorials to get familiar with creating your first dashboard and visualizations. Also see .
The examples in this section use these variables and Grafana functions:
- : a variable used to filter results by stock symbols.
$__timeFrom()::timestamptz
&$__timeTo()::timestamptz
: Grafana variables. You change the values of these variables by using the dashboard’s date chooser when viewing your graph.$bucket_interval
: the interval size to pass to thetime_bucket
function when aggregating data.
Check out this video for a step-by-step walk-through on creating time-series graphs in Grafana:
A very common use case of the time-series graph is displaying stock data. The graph makes it easy to see if the value of a stock is going up or down. Also, no extra calculations are needed to make the graph.
Add the
$symbol
variable of typeText box
to the Grafana dashboard.In Grafana, create a new panel and add this query:
SELECT time,
price
FROM stocks_real_time
WHERE symbol = '$symbol'
AND time >= $__timeFrom()::timestamptz AND time < $__timeTo()::timestamptz
ORDER BY time;
Enter
AMD
in thesymbol
variable. Adjust the time range of your dashboard if desired. This plot uses a raw query, so if you set a large time range, it might take a long time for Grafana to get all the rows and display the data.-
time | price |
--------------------+--------+
2022-02-08 06:38:21 | 123 |
2022-02-08 06:38:28 | 123 |
2022-02-08 06:39:18 | 123 |
2022-02-08 06:39:56 | 123 |
… | … |
Check that your data meets Grafana’s requirements for graphing time series. The data must have a column named
time
, containing timestamps. Other columns can be named as you like. For the time-series visualization, the timestamps must be in ascending order. Otherwise, you get an error. Select
Time series
as your visualization type.
Grafana returns a graph that looks similar to this:
Create a time-series graph from pre-aggregated data using time_bucket()
In the previous example, you queried for all transactions of AMD stock in a 6 -hour period. This returned approximately 3 800 data points. If you query for all transactions of AMD stock in a 3-month period, you get approximately 1,500,000 data points.
Grafana, like many charting tools, doesn’t perform well when plotting millions of points. Also, by default, Grafana refreshes dashboards every 30 seconds. This further strains CPU, memory, and network bandwidth. In extreme cases, Grafana freezes.
To solve this problem, you can pre-aggregate your data using TimescaleDB’s hyperfunction.
Add the
$bucket_interval
variable of typeInterval
to the Grafana dashboard.With a
bucket_interval
of 30 minutes and a date range ofLast 30 days
, Grafana returns this graph:
Because the stock market is only open from 9:30 AM to 4:00 PM on weekdays, there are large gaps in the dataset where there is no data. Grafana automatically connects the last non-null value to the nearest other non-null value. This creates the long, straight, almost-horizontal lines you see in the graph.
To circumvent this issue, you can use Grafana’s
Connect null values
settings. But first, you need rows containing null values wherever you have no data. By default,time_bucket
doesn’t return a row if there is no data. In your query, replacetime_bucket
with . If you don’t specify a gapfilling function,time_bucket_gapfill
returns a row with a null value wherever there is no data.-
AVG(price) AS price
FROM stocks_real_time
WHERE symbol = '$symbol'
AND time >= $__timeFrom()::timestamptz AND time < $__timeTo()::timestamptz
GROUP BY time_bucket_gapfill('$bucket_interval', time)
ORDER BY time;
In the options panel, set
Connect null values
toThreshold
. GiveThreshold
a value of24h
.
Grafana returns a graph similar to this one:
Create multiple time-series graphs in a single panel
If you want to compare the price of two stocks over time, you could make two separate panels with 2 separate symbol variables. A better alternative is to combine the two time-series graphs into a single panel. To do this, change the $symbol
variable to a multi-value answer and make a slight change to your query.
Change the
$symbol
variable to theQuery
type.
In the query options, add the following query. In the selection options, select
Multi-Value
.SELECT DISTINCT(symbol) FROM company ORDER BY symbol ASC;
Under
Preview of values
, you see a handful of company symbols ordered alphabetically.
In the dashboard panel, change the query to allow for multi-value answers: