Visualising ConAir data with Cubism.js
This post is part of a series of blog posts detailing how I built a system which records the temperature in our office. The parts are as follows:
- Measuring temperature with an Arduino describes how to use an Arduino and a thermistor to record the temperature.
- ConAir gives an overview of the motivation behind the project, and runs through the process of taking the readings from the arduino and publishes them to a TempoDB database on the internet.
- This post describes building a proxy webservice for the TempoDB database, and then visualising the temperature data using cubism.js
- iOS App describes building an iOS app which pulls the data from the webservice created in this app and uses ShinobiCharts to plot the result.
Hot on the tail of being able to record temperature readings from the Arduino in the office, we can get some charting on the go.
This post describes building a cubism.js front end to a TempoDB API proxy we implement in Sinatra. You can see the result running live on heroku at sl-conair, and there’s a screenshot below in case we’re working on the electronics and there is no data:
We have been using TempoDB to store the temperature data points - it has a great API for querying your dataset, including rollups, which are able to summarise your data at a resolution of your choice. Unfortunately, TempoDB doesn’t yet allow public access to datasets - the API requires authentication for both read and write. Therefore the first part of this stage will be to build a proxy for the TempoDB API. We then use Cubism.js to interface this proxied API.
TempoDB Proxy
TempoDB provide a selection of API clients - we used the python one to upload the data points as they are read off the Arduino. Here I’m going to use the Ruby one - just ‘cos.
The following is part of a really simple Sinatra application which will interface with the TempoDB API.
We receive requests for data on /data
with URL parameters start
, stop
and
step
. The two timestamps are in a format which can be parsed by ruby’s Time.parse
method, whilst the step is measured in milliseconds. This is to fit nicely with
Cubism.js.
We convert the step into a string of a suitable format for the TempoDB client, here, assuming that the step will always be an integer number of minute. We then send the request to TempoDB.
As mentioned before, if you ask for data at a coarser level than that at which it was recorded, TempoDB will “roll” it up for you. This is a fantastic feature, and although the logic to perform this kind of operation isn’t complex, this kind of operation can absorb days worth of optimisation time, combined with the fiddling associated with working with date objects. Here, we are telling TempoDB that the rollup function should be mean, although other sensible collection operators are implemented too (e.g. count, sum, max etc).
Discontinuous Data
If there is a discontinuity in your data, then TempoDB won’t perform any interpolation - there will be gaps in what it returns. This is fine because the data it returns consists of timestamp-value pairs.
This does however present an issue with Cubism.js, which doesn’t expect discontinuities in the data, and in fact only works with an array of values, ignoring timestamps.
Therefore we iterate through the array of datapoints returned by TempoDB and if there is a temporal discontinuity, we interpolate suitable values. In this instance we’ve chosen to linearly interpolate between the points either side of the discontinuity.
This particular endpoint will return a JSON array of hashes, each containing a value key. The array will be in time order, and the elements represent consecutive temperature readings, at the specified time interval, from start to stop.
Cubism.js
Cubism is a time-series visualisation tool built on top of the brilliant d3.js javascript library. d3.js works on the paradigm of data driven websites - where the content and the style changes as events occur within the data - whether it be a user interacting with it, or new data points arriving. There are some awesome d3.js demos on their website with fantastic visualisations - easily a way to waste an hour…
Cubism.js is a library developed by the people at square for displaying just this kind of data. We are going to use it to call our new API proxy.
Start with a div, within which we will place the chart:
Cubism has the concept of a context, which manages the data requests and the UI elements.
The serverDelay
specifies how long a delay we are prepared to wait before
querying the server for new data points, step
defines how many milliseconds
you wish to wait between datapoints and size
determines how many datapoints
(and therefore the width in pixels) you wish the chart to display.
Cubism manages the requests for the data points given the above settings, and a data source. It has built-in datasource types for Cube and Graphite, but we need to create our own:
We have defined 2 metrics - the second of them simply a time-shifted version of the first. This is the simple kind of metric calculations provided by cubism, and we’ll use this in the difference chart.
The temperature()
function returns a context metric, which describes
how cubism should request data given start, stop and step values, and then
returns the data with a node.js inspired callback method. It’s all pretty
straightforward - we just plug in our particular API URL and then make sure
we process the results to pass an array of numerical values to the callback function.
That’s all the data handling work done. Now we just need to sort the GUI.
This code first find the div we defined beforehand, adds an axis to the top of it, adds a horizon chart and a comparison chart and a rule which follows the cursor over the map.
The last block updates the value display to follow the rule.
Lots more info on setting this up is available on the cubism site, and there are plenty of demos to hack around with.
Et voila…
After all that work, you probably want to see it. You can check the current graph at the site I’ve popped up on heroku sl-conair. Be warned that this project is a work in progress - we’ve got loads we want to do with the electronics, the data collection and the frontend, so it’s quite likely that there will be gaps in the data.
Hope that was of interest. I still want to post about the electronics side of this project - hopefully I’ll get round to that soon
sx
Edit (11/02/2013)
Added an additional introduction to pull together the ConAir posts.