MENU
.net Powerful JavaScript charts

AJAX functions

The AJAX functions in RGraph will help you easily get data from your server (from AJAX services for example). The SVG libraries have their own set of AJAX functions that are documented here.

RGraph has some simplified ajax helper functions that make it much easier to initiate ajax calls without having to be concerned about what the format that the response is in (string, number, object etc).

Previously, 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).

With these functions though they automatically do the data format conversion for you. The AJAX functions that are available to you are:

Name: string RGraph.AJAX(string url, function callback)
Description: 
Fetches the URL and returns the response to you as a string. No processing is performed on the response.
Name: number RGraph.AJAX.getNumber(string url, function callback)
Description: 
Fetches the URL and returns the response to you as a number. This assumes that the output of the AJAX request is a single number.
Name: string RGraph.AJAX.getString(string url, function callback)
Description: 
Fetches the URL and returns the response to you as a string.
Name: array RGraph.AJAX.getCSV(string url, function callback[, string separator = ","])
Description: 
Fetches the URL and returns the response to you as an array. The response is split on the optional separator, which defaults to a comma. It doesn't support multi-line files - it only fetches a single line of CSV data. If you want multi-line support, you'll need to use the CSV import utility.
Name: object RGraph.AJAX.getJSON(string url, function callback)
Description: 
Fetches the URL and returns the response to you as an object. This is the most versatile of all of the AJAX methods, allowing you to retrieve multiple pieces of data in one call. It uses the JavaScript eval() function to convert the reponse to an object.
Name: string RGraph.AJAX.post(string url, object data, function callback)
Description: 
This function allows you to make HTTP POST requests and send data back to the server. The data argument should be a JavaScript object of key/value pairs that will be sent to the server. The data is encoded for you.

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

<script>
    RGraph.AJAX.getJSON('/getdata.html?json', function (json)
    {
        new RGraph.Bar({
            id: 'cvs',
            data: json.data,
            options: {
                marginLeft:     50,
                marginInner:    10,
                tickmarksStyle: 'endcircle',
                linewidth:      2,
                xaxisLabels:    json.labels
            }
        }).draw();
    });
</script>

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