Using the SVG AJAX functions to load data from your server
Summary
The SVG libraries include a set of AJAX functions which contains functions that you can use to load data from your server.
The RGraph SVG libraries incorporate AJAX functions that make it easy for you to fetch data from your server and then use it to create your charts. You can use them by simply including the file RGraph.svg.common.ajax.js in your page and then use the functions like the following example shows.
See also:
There's also a dedicated CSV reader that fetches data via AJAX.
The CSV reader is documented here.
<script src="RGraph.svg.common.core.js"></script> <script src="RGraph.svg.common.ajax.js"></script> <script src="RGraph.svg.bar.js"></script> <script> RGraph.SVG.AJAX.getJSON('/getdata.html?json', function (json) { var bar = new RGraph.SVG.Bar({ id: 'chart-container', data: json.data, options: { xaxisLabels: json.labels, hmargin: 5, backgroundGridVlines: false, backgroundGridBorder: false, yaxis: false, yaxisMax: 100, gutterLeft: 50, shadow: true, shadowOpacity: 0.1, colors: ['Gradient(#8686C6:#8686C6:white)'] } }).draw(); }); </script>
The functions that are availablecan just fetch the desired resource or can go further, such as the getJSON() function which doesn't return the string that the server provides; it turns into a usable JavaScript object for you. Here are the functions:
-
RGraph.SVG.AJAX(url, callback)
This is the function that underpins the other AJAX functions (except the POST function). It simply fetchs the specified URL and returns the response (as a string). It doesn't parse it or do anything with it - it just gives it to you as-is. This function might look something like this:RGraph.SVG.AJAX('/get.html', function (text) { // Your code goes here });
-
RGraph.SVG.POST(url, data, callback)
Instead of a GET request (which happens normally when you request a page) you can make a POST request instead (which typically happens when you submit a form). In this cas you supply the URL, the data to give with the request and a callback function. This function might look something like this:RGraph.SVG.POST('/post.html', {forename: 'Jane', surname: 'Hayford'}, function (text) { // Your code goes here });
-
RGraph.SVG.AJAX.getNumber(url, callback)
This function fetches a page and parses the response, turning it into a number before passing it to the callback function. The callback function might look something like this:RGraph.SVG.getNumber('/number.html', function (num) { // Your code goes here });
-
RGraph.SVG.AJAX.getString(url, callback)
This function fetches a page and explicitly converts into a string. It starts off as a string anyway, so this function simply reinforces it by giving the response to the String() function. The callback function might look something like this:RGraph.SVG.getString('/string.html', function (str) { // Your code goes here });
-
RGraph.SVG.AJAX.getJSON(url, callback)
This function fetches a page and converts the result into a JSON object (by giving it to the eval() function). It then returns the resulting object, (you might not get anything returned if the JSON is not valid). The callback function might look something like this:RGraph.SVG.getJSON('/json.html', function (json) { // Your code goes here });
-
RGraph.SVG.AJAX.getCSV(url, callback [, field-seperator [, line-seperator]])
This function fetches a page and parse the result as CSV data. Like the above getJSON() function this function can be used to help you fetch data from your server. The callback function might look something like this:RGraph.SVG.getCSV('/csv.html', function (csv) { // Your code goes here });