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 the time_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.

    1. Add the $symbol variable of type Text box to the Grafana dashboard.

    2. In Grafana, create a new panel and add this query:

      1. SELECT time,
      2. price
      3. FROM stocks_real_time
      4. WHERE symbol = '$symbol'
      5. AND time >= $__timeFrom()::timestamptz AND time < $__timeTo()::timestamptz
      6. ORDER BY time;
    3. Enter AMD in the symbol 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.

      1. time | price |
      2. --------------------+--------+
      3. 2022-02-08 06:38:21 | 123 |
      4. 2022-02-08 06:38:28 | 123 |
      5. 2022-02-08 06:39:18 | 123 |
      6. 2022-02-08 06:39:56 | 123 |
      7. | |

      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.

    4. Select Time series as your visualization type.

    1. Grafana returns a graph that looks similar to this:

      Screenshot of the time-series graph produced by Grafana. The graph represents the price of AMD in the past 6 hours.

    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.

    1. Add the $bucket_interval variable of type Interval to the Grafana dashboard.

    2. With a bucket_interval of 30 minutes and a date range of Last 30 days, Grafana returns this graph:

    1. 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.
    1. 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, replace time_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.

      1. AVG(price) AS price
      2. FROM stocks_real_time
      3. WHERE symbol = '$symbol'
      4. AND time >= $__timeFrom()::timestamptz AND time < $__timeTo()::timestamptz
      5. GROUP BY time_bucket_gapfill('$bucket_interval', time)
      6. ORDER BY time;
    2. In the options panel, set Connect null values to Threshold. Give Threshold a value of 24h.

      Screenshot of the 'Connect null values' in the time-series options panel. The selected value is 'Threshold' and it has a value of less than 24 hours.

    1. 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.

    1. Change the $symbol variable to the Query type.

      A screenshot of the 'symbol' variable settings. The variable type option has 'Query' selected.

    1. In the query options, add the following query. In the selection options, select Multi-Value.

      1. SELECT DISTINCT(symbol) FROM company ORDER BY symbol ASC;
    2. Under Preview of values, you see a handful of company symbols ordered alphabetically.

    1. In the dashboard panel, change the query to allow for multi-value answers:

    1. A screenshot of the multi-value time-series graph produced by Grafana. This graph displays the stock price for 'AAPL' in green and 'AMD' in yellow for the past 30 days.