About
RGraph is a JavaScript charts library based on
HTML5 SVG and canvas. RGraph is mature (over 15 years
old) and has a wealth of features making it an ideal
choice to use for showing charts on your website.
Download
Get the latest version of RGraph (version 6.18, 1st June 2024) from
the download page. You can read the changelog here. There's also older versions available,
minified files and links to cdnjs.com hosted libraries.
License
RGraph can be used for free under the GPL or if
that doesn't suit your situation there's an
inexpensive (£129) commercial license available.Updating your charts dynamically
The example code shown below shows a Line chart
that automatically
updates itself every 50 milliseconds. An ideal
use for this could be showing a network's bandwidth usage or a
server's load value.
This particular example shows a filled Line chart
.
To get up-to-date data from your server you could simply have the page refresh itself, storing the data on the server, or use AJAX if you want the data stored client-side or, like this example, the storage location doesn't strictly matter.
Notes:-
Remember that browsers can slow down timers (ie
setTimeout
calls) for background pages (eg minimised browsers). Google Chrome does this. If you usesetInterval
instead ofsetTimeout
then it can cause "jumpiness" in updates and may also cause unexpected results. -
For long-running processes, you should not keep recreating the
object. Here, the
Line chart
is created once and stored on the window object (ie a global variable). - Be careful of the data types you use to pass the data to RGraph - you should use numbers to represent values, not strings.
<canvas id="cvs" width="600" height="250">[No canvas support]</canvas> <script> d1 = [0]; l = 0; // The letter 'L' - NOT a one obj = null; // Pre-pad the arrays with null values for (var i=0; i<600; ++i) { d1.push(null); } function getGraph(id, d1) { // After creating the chart, it's stored on the global window object if (!window.obj) { window.obj = new RGraph.Line({ id: id, data: d1, options: { marginRight: 75, backgroundColor: 'white', backgroundGridVlines: false, backgroundGridBorder: false, yaxis: false, yaxisPosition: 'right', yaxisScaleMax: 50, yaxisLabelsCount: 2, yaxisScaleUnitsPost: 'MB/s', xaxis: false, colors: ['#000'], textSize: 14, linewidth: 1, tickmarksStyle: null } }); } return window.obj; } // // The draw() function draws a single frame of the chart. It's // called repeatedly to get the scrolling effect. // function draw () { // Clear the canvas in preparation for for // drawing a new frame RGraph.clear(document.getElementById('cvs', 'white')); // Create the chart and draw it var graph = getGraph('cvs', d1); graph.draw(); // Generate a random value that's close to the // last value of the current data var index = d1.length - 1; var r1 = RGraph.random( RGraph.isNull(d1[index]) ? 26 : d1[index] - 2, RGraph.isNull(d1[index]) ? 24 : d1[index] + 2 ); // Bounds checking for the new value r1 = Math.max(r1, 0); r1 = Math.min(r1, 50); // Add the new value on to the end of the data array d1.push(r1); // Ensure the array is at most 600 values while (d1.length > 600) { d1 = RGraph.arrayShift(d1); } // Set the new data on the Line chart object window.obj.original_data[0] = d1; // Call this function again in 50ms setTimeout(draw, 50); } // Call the draw function to set things going draw(); </script>