A description of the new AJAX functions

  Read comments...

Summary
New AJAX functions added in April 2013 such as RGraph.AJAX.getCSV() make it much easier to fetch data from your server when working with AJAX

RGraph now has new AJAX helper functions which were added in April 2013 that make it significantly easier to make AJAX requests without having to be overly concerned about what type the response is (string, number, object etc). Prior to now, because the response to an AJAX request is a string, you had to convert it into a usable form (such as a number or an array). Now though there are helper functions that will automatically do the conversion for you. The available AJAX functions now consist of:

The first two functions are self explanatory. The getCSV() function fetches a comma seperated sequence of numbers and automatically converts it into an array for you so that you can use it directly as an RGraph dataset. It does not support multi line files - it only fetchs a single line of CSV data. If you want multi-line support you'll need to use the dedicated CSV file reader. The getJSON() method fetches the URL that you give it and treats the response as JSON (ie it eval()s the response). Using JSON means that you can return multiple datasets and/or labels or other information in your response. The POST function allows you to make POST requests - sending data back to the server. The data argument should be a JavaScript object of key/value pairs that is to be sent to the server as POST data. The data is encoded for you.

There are straight-forward examples of the AJAX functions in the basic examples in the RGraph archive

window.onload = (function ()
{
    RGraph.AJAX.getJSON('/getdata.html?json', function (json)
    {
        var bar = new RGraph.Bar({
            id: 'cvs',
            data: json.data,
            options: {
                gutterLeft: 50,
                hmargin: 10,
                tickmarks: 'endcircle',
                linewidth: 2,
                labels: json.labels
            }
        }).draw();
    });
});

* As of October 2013 there's now a dedicated CSV reader (that also fetches the CSV via AJAX) * part of RGraph.

Share RGraph
X

Comments