During a recent hardware hackathon, held by PCH International on DCU’s Innovation Campus, the team I was a part of created a fridge monitoring setup for pharmacies.

We built a “smart-shelf” – pressure-sensitive shelving prototyped in acrylic using a laser cutter and hooked up to an Intel Galileo – and combined it with an EpiSensor temperature monitor. These components fed their data up to a Node-RED instance running on IBM’s Bluemix platform, which in turn piped it through to a couple of MongoLab-hosted collections.

I was mainly focused on building the web side, using my hackathon-starter-handlebars Node.js project as the base (itself a port of hackathon-starter which uses Jade out of the box).

On this platform I built us a nice web dashboard to present the data. One of the aces up my sleeve was a pretty, real-time graph powered by Morris.js and jQuery AJAX calls to a Node.js API.

The emphasis was on speed and simplicity, and I feel this could totally be useful for other people out there who need to inject some dynamism into their graphing. I’ll show you how I did it.

The finished product - except imagine this graph actually updating...

The finished product – except imagine this graph actually updating…

Powering this example is a simple (and extremely limited) Node.js API powered by Express.

Imagine we have a MonogDB collection filled with entries like the following:

And can grab this data from MongoDB in a Node.js function like so:

The :id in the comments is a nice Express paradigm enabling us to write URLs REST-style, like example.com/users/1 – in this case we could process this in Node by writing the route /users/:id. Here the :id parameter refers to the device ID, and is checked by reading req.params.id (:id -> req.params.id). Gotta love them RESTful APIs!

The important part to look at begins at the json: function() property of the res.format object. The presence of this property tells Express to use the json function in response to an AJAX (JSON) request to the URL. For HTML requests to that URL we are giving back a 404 as this isn’t applicable to our purposes.

When we receive the data from the MongoDB call (results), we loop through each result (for our purposes a list of datetime and corresponding temperature value as exampled above), taking the date and time and placing them in a graphPoint object.

Once I have the necessary data for the result, I push it onto an array of graphPoints called graphData, which is returned to the AJAX request by the res.json() call once the looping is done and all the results have been processed. This gives us a nice array of JSON objects that Morris will be able to read with only a little bit of prompting on our parts.

It’s especially important to note that this kind of parsing could be done on the frontend side if you are not the creator of the API. You just need to make your API call and then construct your elements as you need them. You may not even need to do any parsing. Technically I don’t really need to here (as the original MongoDB records have the information in a readily accessible state that could be pointed to for Morris), but am doing so for demonstration purposes.

Let’s take a look at where this data is requested, and how the Morris graph is initialised.

As you can see here, a simple jQuery.getJSON() function is used to request the necessary data from the (completely unsecured, don’t-do-this-for-commercial-projects API). Once the data is received, we remove the loading gif we had in place of the graph and initialise a new Morris.js line graph.

Note that we tell Morris what the important data object is – results.graphData – and that what we named the properties of the graphPoint objects in graphData is important because we explicitly tell Morris how to map graphData’s properties to the axes. In this case we want to put timestamp on the x and temp on the y, giving us a nice temperature over time graph.

The really interesting part is that we initialise the line graph and assign it to a variable – mainGraph. Once the graph is rendered the first time, we setup a normal Javascript setInterval call to an updateLiveTempGraph() function, passing the mainGraph object and repeating it every twenty seconds. This interval could be whatever you want. In our case we were only receiving temperature data every twenty seconds.

This is the meat of the real-time updating, so what’s happening in that update call?

Simply put, we are requesting the new data from the API and telling the previously initialised Morris line graph (mainGraph) to set its data to the new payload.

Morris is so smart that it doesn’t need to be told a second time how to parse this data (where to look for the data and which axes to map to what) – we simply pass the new data object (the same object we told Morris to look at in the initialisation of the graph) and it does the rest, reading the timestamp and temp and updating our graph right before our eyes.

That’s really all there is to it. Nothing fancy, but if you need real-time (or as near to it as you could want) graphing, without complicated coding, give this a shot.

WordPress Backup