MENU
.net Powerful JavaScript charts
About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 15 years old) and has a wealth of features making it an ideal choice to use for showing charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.19, 28th September 2024) from the download page. You can read the changelog here. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

Download »

 

License
RGraph can be used for free under the GPL or if that doesn't suit your situation there's an inexpensive (£129) commercial license available.

More »

The AJAX functions

Written by Richard Heyes, RGraph author, on 24th July 2018

Introduction

Since 2005 ajax has been a popular choice for web developers. It can be used to good effect to get data from your server - be it initially when the page has just loaded or after the page has loaded (eg to facilitate periodic updates).

You might want to do this to make it easier to integrate with legacy or pre-existing server-based scripting. Or it could be an easier way to handle migration if that's what you're doing.

An example use-case would be when you have a chart - perhaps a Line chart that you want to update with new data.

What you could do is simply refresh the page. This however might have other consequences that you'd rather not have to deal with.

Also if your page is weighty then bandwidth or page load speed might be a concern that you'd prefer not to have.

What AJAX functions are available?

There are a few functions that are available, with both canvas and svg having their own set that is available to you.

The canvas AJAX functions

With canvas charts, the functions are located in the RGraph.common.core.js file and since this file is necessarily included you don't have to include anything extra to what you normally include in your page.

The functions that are available to you are:

In each of these functions, the url parameter is the URL that you wish to call and the callback parameter is a function that is called when the ajax call completes. It's passed the output of the request as an argument.

The extra data argument to the RGraph.AJAX.post function is an object that should consist of the post data that you want to send to the server (in the form of key/value pairs).

The SVG AJAX functions

The svg ajax functions on the other hand are located in their own file in order to try and keep the file sizes down. The necessary file is called RGraph.svg.common.ajax.js.

The functions that are available to you are:

Like the canvas ajax functions, in each of these functions, the url parameter is the URL that you wish to call and the callback parameter is a function that is called when the ajax call completes. It's passed the output of the request as an argument.

Like the canvas post function, the extra data argument to the RGraph.SVG.AJAX.post function is an object that should consist of the post data that you want to send to the server (in the form of key/value pairs).

The example code from the chart above

The following URL is used by several of the demos to produce charts

And the code to make the charts is as follows - making use of the getString function. Remember that when using this function you'll probably need to convert any numbers from strings (which is what they are initially) to numbers.

<script>
    // This initiates the AJAX call to fetch the sample.csv file and runs
    // the function when it has completed.
    RGraph.SVG.AJAX.getString('/sample.csv', function (data)
    {
        // Convert the data from the file into a usable format. Notice that
        // the last bit (the forEach() loop) converts all of the numbers from strings
        // into real numbers. It's important to do this.
        data = data.split(/\r?\n/);
        data = data[0].split(/,/).slice(1);
        data.forEach(function (v, k, arr)
        {
            arr[k] = parseInt(v);
        });

        // Create and display the chart
        new RGraph.SVG.Line({
            id: 'cc',
            data: data,
            options: {
                title: 'A chart demonstrating the AJAX functions',
                titleY: '-10',
                xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
                yaxis: false,
                backgroundGridVlines: false,
                backgroundGridBorder: false,
                textSize: 20,
                marginTop: 50,
                linewidth: 5
            }
        }).draw();
    });
</script>

Further reading

Read the documentation for the svg and canvas ajax functions. There's a set for both the canvas and svg chart types.