RGraph: HTML5 canvas graph library - API documentation


Overview

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 only two files required - the common library and the graph specific library. Eg:

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

RGraph.common.js is a function library that contains a large set of functions that support the graph classes. 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:

<script>
    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']);
        myBar.Draw();
    
        // 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
    }
</script>

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 them like this:

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

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. An 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 slightly different so that it makes a little more sense. An example is the Progress bar, which stores the indicated value in the chart.value property, and the maximum value in chart.max.

RGraph functions

This is a list of some of the functions available to you in RGraph.common.js. 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.js"></script>

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

Arrays

Strings

Miscellaneous

Other

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

Questions

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