Using the SVG AJAX functions to load data from your server
Summary: The SVG libraries include a set of AJAX functions which incorporate 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, marginInner: 5, backgroundGridVlines: false, backgroundGridBorder: false, yaxis: false, yaxisScaleMax: 100, marginLeft: 50, shadow: true, shadowOpacity: 0.1, colors: ['Gradient(#8686C6:#8686C6:white)'] } }).draw(); }); </script>
The functions that are available can 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 returns 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 theRGraph.SVG.AJAX.post()
function). It simply fetches 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.AJAX.post(url, data, callback)
Instead of aGET
request (which happens normally when you request a page) you can make aPOST
request instead (which typically happens when you submit a form). In this case you supply the URL, the data to submit with the request and a callback function. This function might look something like this:RGraph.SVG.AJAX.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.AJAX.getNumber('/number.html', function (num) { // Your code goes here });
-
RGraph.SVG.AJAX.getString(url, callback)
This function fetches a page and explicitly converts the response into a string. The response starts off as a string anyway, so this function simply reinforces it by giving the response to theString()
function. The callback function might look something like this:RGraph.SVG.AJAX.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 aJSON
object (by giving it to theeval()
function). It then returns the resulting object, (you might not get anything returned if theJSON
is not valid). The callback function might look something like this:RGraph.SVG.AJAX.getJSON('/json.html', function (json) { // Your code goes here });
-
RGraph.SVG.AJAX.getCSV(url, callback [, field-separator [, line-separator]])
This function fetches a page and parses the result as CSV data. Like the abovegetJSON()
function thegetCSV()
function can be used to help you fetch data from your server. The callback function might look something like this:RGraph.SVG.AJAX.getCSV('/csv.html', function (csv) { // Your code goes here });