AJAX functions
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 code shows.
See also:
There's also a dedicated csv
reader that fetches data via ajax
.
The CSV reader is documented here.
<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(orange:orange:white)'] } }).draw(); }); </script>
The available functions 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 otherajax
functions (except theRGraph.SVG.AJAX.post
function). It 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 acallback
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 thecallback
function. Thecallback
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. Thecallback
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). Thecallback
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 ascsv
data. Like the abovegetJSON
function thegetCSV
function can be used to help you fetch data from your server. Thecallback
function might look something like this:RGraph.SVG.AJAX.getCSV('/csv.html', function (csv) { // Your code goes here });