Bookmark and share:
Bookmark with delicious tweet this site Post to Google Buzz! Post to Facebook Share on FriendFeed Stumble! this site

RGraph: HTML5 canvas graph library - API documentation


Working with RGraph objects is purposefully easy, to make them straight forward to integrate into your own scripts if you want to. For any particular graph type there are a few files required - the common libraries and the graph specific library. Eg:

<script src="RGraph.common.core.js"></script>
<script src=""></script>

RGraph.common.core.js is a function library that contains a large set of functions that support the graph classes. Some functions, and sets of functions, have their own files. For example, the zoom functions reside in RGraph.common.zoom.js, so if you don't need zoom, you don't need this file. Each of the graph libraries (RGraph.*.js) contains that particular graphs class. If you'd like to see a "bare bones" implementation, you can look at the basic example.

Canvas and context references

Each graph object maintains references to the context and canvas as properties. So to get hold of them all you need to do is this:

    window.onload = function ()
        // 1/2 First draw the chart
        var myBar = new RGraph.Bar('myCanvas', [1,5,8,4,6,3,1,5,7,8,4,6]);
        myBar.Set('chart.labels', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
        // 2/2 Now get hold of the references
        var context = myBar.context; // Get hold of a reference to the context
        var canvas  = myBar.canvas;  // Get hold of a reference to the canvas

Working with events

When working with events, you may come across the situation where you have a reference to the canvas, but also need to access the graph object. For this reason the constructor of each object adds a reference to the object to the canvas and you can access it like this:

    document.getElementById("myCanvas").onclick = function (e)
        var src = (document.all ? event.srcElement :;
        // The RGraph object constructors add __object__ to the canvas.
        var myBar = src.__object__;

Working with graph coordinates

For most graph types, the coordinates of elements (eg bars, lines etc) are to be found in the class variable obj.coords. This is usually a multi-dimensional array consisting of the coordinates of those shapes, or of points. As an example the bar chart maintains the X, Y, width and height of each bar (or sections of bars in a stacked bar chart). The coords array is usually a flat array, even when you have multiple sets of data.

By using the RGraph.getMouseXY() function and this array you can determine if a bar was clicked on, or if the mouse is near a line point etc.

var myCoords = myBar.coords;

Note: Previously the coords array values sometimes included the margin values, and sometimes didn't. As of 17th April 2010 the values have all been unified to not include the margin values, so the values are as reported.

Working with graph data

Another variable you may need is the data variable. For most graph types, this is where the data is stored. It is usually untouched, so it is as you supplied to the graph objects constructor. A notable exception to this is filled line charts. Here the original data is stored in the class variable original_data. The data supplied is modified to produce the stacking effect. If you need to modify a filled line charts data you will need to change this variable instead.

Not all graph types use the data variable. For some the name is different so that it makes a little more sense. The graph types and their associate data variables are listed below1.

Graph type Data variable(s)
Bi-polar obj.left, obj.right
Donut This is now a variant of the Pie chart
Gantt See the docs
Horizontal Bar
Horizontal Progress bar obj.max, obj.value
LED obj.text
Line obj.original_data3
Meter obj.min, obj.max, obj.value
Odometer obj.start, obj.end, obj.value
Pie obj.angles,
Radial Scatter chart
Rose obj.max,
Scatter obj.max, obj.data2
Traditional Radar, obj.max
Vertical Progress bar obj.max, obj.value
  1. In the table, obj refers to your graph object.
  2. For the Scatter chart, each data point is an array of X/Y coordinates, the color and the tooltip for that point.
  3. The Line chart obj.original_data is an aggregation of all the datasets given to the Line chart, so the first dataset is held in obj.original_data[0], the second in obj.original_data[1] etc.

Updating your graphs dynamically

A common request is to be able to update graphs dynamically. This is quite simple and consists of three steps:

  1. Get the new data from the server (with an AJAX request for example).
  2. Set the data in your graph object. See the above table for the appropriate property to use.
  3. Call the .Draw() method again.

If you don't need to get data from your server (ie it's all client-side) then you can omit the first step. Also, it may be sufficient to simply recreate the entire object from scratch. This means that you won't have to alter and RGraph internal properties - just recreate the graph object and configuration. There's a simple function you can use for AJAX purposes here.

Setting properties

To set RGraph properties, ideally you should use each objects setter (there's also a corresponding getter). These functions accomodate some backwards compatibility changes, so by not using them you run the risk of your graphs not working entirely as expected.

myObj.Set('chart.gutter', 25);

Scale information

For the Bar, Bipolar, HBar, Line and Scatter charts the scale that is used is stored in the scale class variable. Eg:

var myBar = new RGraph.Bar('cvs', [56,43,52,54,51,38,46,42,52]);

var myScale = myBar.scale

RGraph functions

This is a list of some of the functions available to you in the RGraph common libraries. It's not every single one that's available, but is a list of the common ones that you're likely to want to use. Arguments in square brackets are optional.

<script src="RGraph.common.core.js"></script>

    // Returns 9
    myArray = [3,2,5,4,9,7,8];
    max = RGraph.array_max(myArray);





Custom RGraph event related functions


These are functions which are less generic, and used to build the graphs. You may still wish to use them however.


If you have a question regarding the API, you can ask it on the mailing list