What is Chart.js
I would recommend this library for the great documentation and ease of use compared to others.
A new (pre-)release is out v1.0.1 beta-4 and bring some major improvements. I had the chance to play with the version 0.2.0 a while back for monitor_it a simple remote system monitor. monitor_it required me to constantly update multiple charts, at the time you could only do this by creating a new chart with the updated data. Not a clean way to update a chart… But it has changed : the new version now have a “update” feature (amongst other wonders).
Let’s play with those new update features. I will only use a “Line” chart but these are transferable to other chart types.
Your browser need to handle HTML 5 canvas to view the example above!
You can see the source of this example in your browser or in the github repository.
Very few is needed for this example:
- Generate a chart with the value 0 for data and labels, add a bit of color
- Generate a random number for your next point
- Remove the oldest point from the chart (the one at the left):
- Add your newly generated random number (at the right):
- Repeat steps 2 to 4
addData() take an array for parameters, this is for multiple datasets (e.g. multiple lines on the chart). The values and datasets must be ordered the same way.
Moreover, this new version of Chart.js offer the possibility to get a point of the chart/dataset in a bubble. This feature is a bit buggy due to the animations but I trust it will be fixed soon enough.
If you are looking for a more professional/feature-rich JS chart library and don’t need the update feature, check out metrics-graphics from mozilla.