<!DOCTYPE html>
<html>
<head>
<title>A basic example of a CSV/AJAX</title>
<meta name="robots" content="noindex,nofollow" />
<meta name="googlebot" content="NOODP">
<link rel="stylesheet" href="demos.css" type="text/css" media="screen" />
<!-- Include the RGraph libraries -->
<script src="../libraries/RGraph.common.core.js" ></script>
<script src="../libraries/RGraph.line.js" ></script>
</head>
<body>
<h1>A basic example of AJAX and CSV data</h1>
<p>
This is a basic example of using CSV and AJAX. AJAX is used to request the data and it is returned in CSV format. The JSON
data is then evaluated and a chart created using it.
</p>
<canvas id="cvs" width="600" height="250">[No canvas support]</canvas>
<script>
/**
* Ths window.onload function initiates the AJAX request. The AJAX page is: http://www.rgraph.net/getdata.html
* If you view this in your browser you'll see that all it does is output a sequence of numbers.
*/
window.onload = function ()
{
RGraph.AJAX.getCSV('/getdata.html', drawGraph);
};
/**
* This is the AJAX callback function. The AJAX getCSV() converts the strings to numbers for you.
*/
function drawGraph (csv)
{
// Now draw the chart
var line = new RGraph.Line({
id: 'cvs',
data: csv,
options: {
textAccessible: true,
hmargin: 0,
linewidth: 2,
ymax: 100,
numxticks: 9,
gutterLeft: 35,
labels: ['Gary','Rachel','Neil','Martha','Kevin','Craig','John','Peter','Luis','Bert']
}
}).draw()
}
</script>
<p>
<b>Note:</b>
In October 2013 a new CSV reader was added to RGraph. It makes reading CSV files much easier. You can read about
<a href="http://www.rgraph.net/docs/csv-file-reader.html">the new CSV reader</a> here.
</p>
<p></p>
This goes in the documents header:
<pre class="code">
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
</pre>
Put this where you want the chart to show up:
<pre class="code">
<canvas id="cvs" width="600" height="250">
[No canvas support]
</canvas>
</pre>
This is the code that generates the chart:
<pre class="code">
<script>
<span>/**
* Ths window.onload function initiates the AJAX request. The AJAX page is: http://www.rgraph.net/getdata.html
* If you view this in your browser you'll see that all it does is output a sequence of numbers.
*/</span>
window.onload = function ()
{
RGraph.AJAX.getCSV('/getdata.html', drawGraph);
};
<span>/**
* This is the AJAX callback function. The AJAX getCSV() converts the strings to numbers for you.
*/</span>
function drawGraph (csv)
{
<span>// Now draw the chart</span>
var line = new RGraph.Line({
id: 'cvs',
data: csv,
options: {
textAccessible: true,
hmargin: 0,
linewidth: 2,
ymax: 100,
numxticks: 9,
gutterLeft: 35,
labels: ['Gary','Rachel','Neil','Martha','Kevin','Craig','John','Peter','Luis','Bert']
}
}).draw()
}
</script>
</pre>
<p>
<a href="https://www.facebook.com/sharer/sharer.php?u=http://www.rgraph.net" target="_blank" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=http://www.rgraph.net', null, 'top=50,left=50,width=600,height=368'); return false"><img src="../images/facebook-large.png" width="200" height="43" alt="Share on Facebook" border="0" title="Visit the RGraph Facebook page" /></a>
<a href="https://twitter.com/_rgraph" target="_blank" onclick="window.open('https://twitter.com/_rgraph', null, 'top=50,left=50,width=700,height=400'); return false"><img src="../images/twitter-large.png" width="200" height="43" alt="Share on Twitter" border="0" title="Mention RGraph on Twitter" /></a>
</p>
</body>
</html>