A basic example of AJAX and JSON data

This is a basic example of using JSON and AJAX. AJAX is used to request the data and it is returned in JSON format. The JSON data is then evaluated and a chart created using it.

[No canvas support]

This goes in the documents header:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
Put this where you want the chart to show up:
<canvas id="cvs" width="600" height="250">
    [No canvas support]
This is the code that generates the chart:
    * Ths window.onload function initiates the AJAX request. The AJAX page is: http://www.rgraph.net/getdata.html?json
    * If you view this in your browser you'll see that all it does is output a JSON object (a JavaScript object).
    window.onload = function ()
        RGraph.AJAX.getJSON('/getdata.html?json', drawGraph);

    * This is the AJAX callback function. It splits up the response, converts it to numbers and then creates the chart.
    function drawGraph (json)
        // Set the JSON on the window object so that the button below can show it to the user.
        window.__json__ = json;

        // Now draw the chart
        var line = new RGraph.Line({
            id: 'cvs',
            data: json.data,
            options: {
                textAccessible: true,
                hmargin: 10,
                linewidth: 2,
                ymax: 100,
                labels: json.labels,
                gutterLeft: 35

Share on Facebook Share on Twitter