H

How to Use Grafana for Data Visualization

Photo by Luke Chesser on Unsplash

Chances are, if your data visualization background comes from outside of IT, you have never heard anything about Grafana. Grafana has primarily been used for monitoring and logging dashboards, related to infrastructure and applications. The buzzword for that these days is Observability dashboards. There are other players in this area, like Splunk, Kibana, and many more. In this article, I’ll show you how to use Grafana in other more common scenarios such as, that chart for an article, or the dashboard for your health metrics.

Grafana’s history

Grafana was first released in 2014 by Torkel Ödegaard as a “spinoff” of a project at Orbitz. It targeted time-series databases such as InfluxDB, OpenTSDB, and Prometheus, but evolved to support relational databases such as Oracle, MySQL, PostgreSQL, and Microsoft SQL Server. As a visualization tool, Grafana is a popular component in monitoring stacks like Sensu, IcingaCheckmarkZabbixNetdata, and PRTG.

Before you continue, it is important to highlight that I’m in no way trying to sell that Grafana is the best tool ever for data visualization, neither I have any personal gain in presenting the tool here. As a data visualization enthusiast, I think it is important to know tools that can help us to understand our data.

Currently, Grafana defines itself as a multi-platform, open-source analytics and interactive visualization web application. On the Grafana site, you can find their mission:

We believe in breaking traditional data boundaries, making metric visualization tools that are more accessible and easy to use across the entire organization. And most importantly, open source.

The core values of Grafana are expressed as art. Authors are Todd Moreland (1 & 2), Phil Pascuzzo (3), and Caroline Cracco (4).

How to start with Grafana

The first thing to know about Grafana is that there are three ways to use it:

  • Using the Grafana Cloud will give you access to a free version of Grafana, with some limitations related to the number of dashboards and users that can create/update dashboards;
  • Installing on-premise will remove the limitations above, but you’ll need to find a way to publish on the web;
  • If you want it for your company, get the Enterprise version.
Grafana home page
Grafana home page

In this article, you will use Grafana Cloud. Please create your account (at the time of this writing, they are promoting 10 days free of the PRO version with new registrations) to follow along. After you have logged in, you should see the following screen:

Grafana cloud main page
Grafana cloud main page

Then, click on the Login button on the Grafana box, and you’ll see the screen below:

My Grafana Environment

This is the home page for your Grafana cloud. It can be customized with panels and visualizations, but you will address that later. Throughout the next sections, you will learn how to build a dashboard, step-by-step.

Creating a data source

Any dashboard needs data to show, and in Grafana you need to create a data source before you start. It is simple.

Data source menu on Grafana left toolbar

Click on the Configuration icon and then select Data Sources. You’ll see the screen below.

Pre-defined list of data sources

As you can see, there are some pre-defined data sources available. These data sources allow you to create some observability dashboards about your Grafana cloud instance. You can see the usage logs, traces, alerts, and more. But remember, your objective is to create other types of dashboards, so click on the Add Data Source button to configure your new data source.

In the list, you can see many of the available types of data sources. Grafana uses a plugin system, which allows you to add other types, as you see fit. For your dashboard, select the PostgreSQL data source.

Configuration form for PostgreSQL data source

The instructions to follow are simple, you just need your database connection information.

Database connection
host: postgresql-22468–0.cloudclusters.net:22468
database: grafana
user: dvs_user
password: dvsuser123
TLS/SSL mode: disable
version: 12

Just fill the fields with your database information or the information above and click on Save and Test. You should see something like this:

Successful message for data source creation

Now, click on Back, and create your first dashboard.

Create Dashboard option on Grafana toolbar

Anatomy of a Grafana dashboard

If you have reached this point of the article, it means you are ready to created dashboards in Grafana. First, it is important to understand a few concepts about dashboards in Grafana:

  • Dashboards are made of two other smaller units: Panels and Rows.
  • Dashboards may have links between them.
  • Dashboards may have variables that can act as filters on your visualizations.
  • Dashboards may be imported/exported as JSON.

Panels are our charts, or visualizations if you prefer. Rows are a way to organize your panels inside the dashboard. Rows can help you avoid a lot of duplicated effort. Your recently-created dashboard should look like the one below.

Dashboard with an empty panel

Before you continue, save your dashboard, so you can give it a name. Click on the Save button and fill out the name of the dashboard, as in the image below.

Save Dashboard dialog

Adding your first Panel

Now, click on Add Panel and get started.

When you select Add Panel, a new Panel will be created and you will enter the Panel Edit page, which will look like the image below:

Edit Dashboard page

Looking at this screen, you should be able to identify three areas of interest:

  • The drawing area (1)
  • The query area (2)
  • The configuration area (3)

The drawing area is where the results will appear as you make changes in the two other areas. In the drawing area, you will also find the time filter and the button to refresh results. The query area is where you select the data source to work, write your query, and apply transformations when needed. The configuration area allows you to select the type of chart and all the layout configurations.

Creating your chart

Note that when you start a new panel, it defaults to a sample line chart. Line charts are the standard chart type in Grafana. If you want to change the chart type, you need to go to the configuration area and change that on the Visualization option.

Selecting your chart type

For this tutorial, you will select the Bar Gauge chart. Observe that as you select it, the other options in the configuration area change.

Bar gauge chart type selected. Observe the different options displayed.

Your next step is to change the query. The first change is on the data source. Right now, it should be showing something like ‘ — Grafana — .’ Change that to your recently created data source, called ‘PostgreSQL.’ This will change the text box right below.

Query panel

Click on the ‘Edit SQL’ button and this will convert into a text box.

Query editor

Now, you can write your own query. It is important to understand that you can write two types of queries: Time Series or Table.

Time Series queries need to have a field called time, which is a timestamp. If you click on Show Help, you can learn more about how to use a Time Series query. Here is a simple query to demonstrate:

Your query area would look like this:

SELECT answer, count(distinct respondent) as value
FROM survey_data
WHERE question = ‘Identifique sua faixa etária:’
GROUP BY answer

Your query area would look like this:

New query

The query provoked an immediate change in the drawing area, as you can see below.

Query result

Now, to finalize, you need to make a few changes in the configuration area.

Change the chart to show one element for each answer

Change the chart by selecting the Display option.

Configuration panel

There, you will change from Calculate to All values. The Calculate option was designed to display a single value, which is the result of a computation with all rows returned by the query.

Changing the option to display multiple lines

This change will provoke an alteration in the drawing area.

The first version of the bar chart

Change the label of each element

As you can observe in the drawing area, you have now multiple bars, which is a good indication that you are on the right track. But, all the bars have the same label as ‘value.’ To fix that, you need to go to the Field Panel.

The Field tab on the Configuration panel

The standard options will already be opened, so you can make the following fixes:

  • set the Min field to 0;
  • set the Display name field to $__cell_0. This is a Grafana variable that indicates that you are going to get the value from the first column of the query, in this case, the column answer.
Adding a variable to show the value of the first column

Again, looking at the drawing area, you can see what has changed. Woohoo! Your chart is almost perfect!

Bar chart now displaying the correct values

Setting the chart title

It is easy to title the chart, just click on the Settings and fill the Panel Title field.

Changing the title of the chart

Change the title to Age Range.

Chart title changed

Note the change in the drawing area below.

The second version of the chart

Changing the style of the chart

Now, you can spend a little more time in the Display option on the Panel tab, making a few adjustments to improve the layout.

Removing the unfiled area

The main change here is to remove that gray area reserved for the bars. To simplify your visualization, remove that point of focus. To do that you just need to click on Show unfilled area. The result will be as follows:

The final version of the chart

Now, to finalize, click on Apply (at the right upper corner) and … voilá! Here is your first chart on your dashboard!

The first version of the dashboard

Now, you can edit the chart, add other charts, and move the charts around for better positioning. In my next article, I will cover chart configuration and variables.

Conclusion

In this article, you have learned how to create your first dashboard in Grafana and now you can add it to your data visualization toolbox. You have also explored the potential of Grafana beyond Observability dashboards. This link will lead you to the complete version of this dashboard for you to view. If you are interested in creating your own Grafana account and experimenting for yourself, this link will lead you to my Gitlab repository with the dashboard to be imported.

Product Designer at Dell Technologies

Walter Ritzel Paixão Côrtes is a product designer and data visualization practitioner who works solving users problems through a data-driven approach.