Visualizing Your Time Series Data from InfluxDB with Rickshaw

Recently, we took a look at visualizing our time series data from InfluxDB using the graphing library plotly.js (see post here), which offers over 20 different charting types, and packages everything so neatly that it is simple and easy for users to reproduce graphs of their own style and choosing. Today we’ll take a look into pulling our data from InfluxDB and displaying it with the Rickshaw library, which, like plotly.js, is similarly built on d3.js. The Rickshaw toolkit offers users the ability to create custom interactive time series graphs, giving users access to a broad array of styles and a score of different elements—renderers, legends, hovers, and range selectors—with which to construct their graphs.

Before we get started of course, you’ll need to ensure you have an instance of InfluxDB up and running on your local machine. Here’s a great site to getting the TICK Stack all set up and doing things on your machine—the aptly named “Getting Started” guide on the InfluxData site supplies documentation that will walk you through installing and running all four packages of the stack. You can also try experimenting in sandbox mode.

We Need Data!

Now in order to visualize our data, you’ll first need to get some. For this scenario, I’ll be querying InfluxDB via the Node-influx client library for stats that Telegraf is already collecting about my machine. You can do the same for your own machine or if the mood strikes, you could hook up and start monitoring any number of applications of your own. Telegraf has a number of plugins that can handle the translation of your data into line protocol (the text-based format for writing points to InfluxDB) and send your data right into the database.

For this example, I’ll be using Node/Express to set up my server file and query InfluxDB for data on average cpu usage. If you have the full TICK Stack installed and running, you should be able to do the same. After you install and require in the appropriate dependencies, you’ll connect to your local instance of InfluxDB and the specific database you’d like to query. Here’s what my server file looks like:

You’ll notice the last section of code has an endpoint which queries the database for the mean cpu user usage.

Visualizing Your Data

You’ll need to set up your html, css, and script file structure, so you have somewhere to display your data in the browser. You’ll also need to either install Rickshaw and d3 as npm packages, or add the minified versions to your document head. Check out Rickshaw’s GitHub ReadMe for more information. For some of the more ornate graphs, Rickshaw requires jQuery and jQueryUI, but we’ll keep it simple for today. Take a look at this index.html file:

As you can see, you’ll need to define separate containers for your chart, y-axis, and legend, along with an optional toggle capability to render the graph in different formats. The separation of all these components allows users to create custom formatting and unique visualizations, but it can also be a bit tricky to set everything up as newbie to the graphing game. I, for instance, tried to reproduce a version of the following example straight away, and realized all too quickly, it would probably be better to start at the beginning first with Rickshaw’s tutorials.

Gotta walk before you run!

I digress, however. Let’s move onto the script file where we can add all the functionality to paint our InfluxDB time series data in a graph. Copy and paste (or type it out for muscle memory development!) the following:

Now aside from the fact that my ‘loadData’ function is far too long and not at all single-responsibility, there is definitely a lot going on here. Let’s dig in, shall we?

We first make a fetch call to grab the data out of InfluxDB and hopefully if our call is successful, we can then parse and mutate (unpack) our data to fit the format that Rickshaw requires. Rickshaw has several color schemes one can choose from, so after setting our color palette to ‘colorwheel’, we can then instantiate a new Rickshaw graph and insert the formatted data accordingly.

What follows is the creation of the x and y axes, a legend to denote what data we are looking at, and the toggler functionality to switch the graph format between a line graph or a stacked graph (Rickshaw has quite a few more formats as well). These optional add-ons are in my opinion where things get tricky. Particularly in regards to the x and y axes, as this is a time series graph, it makes most sense to me for those to be present by default, and not something that has to be added on as an afterthought (and then styled and positioned on top of that!). For a list of the possible extensions, I would head over to Rickshaw’s GitHub page and spend some time experimenting.

The Final Product

If you restart your server at this point and navigate to port 3000, you should hopefully see a graph of particular beauty and form. If it doesn’t look as awesome as the one below, don’t forget to add a bit of CSS flair to liven it up and make things pop!

The Graphing Possibilities!

Thanks for reading and feel free to check out the source code on GitHub or ping me an email at [email protected] if you have any questions. Happy graphing!