Commit 8d94678e authored by olau@iola.dk's avatar olau@iola.dk

Extended the AJAX example with a setTimeout example of polling

git-svn-id: https://flot.googlecode.com/svn/trunk@175 1e0a6537-2640-0410-bfb7-f154510ff394
parent c98d5145
...@@ -21,33 +21,47 @@ ...@@ -21,33 +21,47 @@
extracts it from a database and serializes it to JSON.</p> extracts it from a database and serializes it to JSON.</p>
<p> <p>
<input type="button" value="First dataset"> - <input class="fetchSeries" type="button" value="First dataset"> -
<a href="data-eu-gdp-growth.json">data</a> - <a href="data-eu-gdp-growth.json">data</a> -
<span></span> <span></span>
</p> </p>
<p> <p>
<input type="button" value="Second dataset"> - <input class="fetchSeries" type="button" value="Second dataset"> -
<a href="data-japan-gdp-growth.json">data</a> - <a href="data-japan-gdp-growth.json">data</a> -
<span></span> <span></span>
</p> </p>
<p> <p>
<input type="button" value="Third dataset"> - <input class="fetchSeries" type="button" value="Third dataset"> -
<a href="data-usa-gdp-growth.json">data</a> - <a href="data-usa-gdp-growth.json">data</a> -
<span></span> <span></span>
</p> </p>
<p>If you combine AJAX with setTimeout, you can poll the server
for new data.</p>
<p>
<input class="dataUpdate" type="button" value="Poll for data">
</p>
<script id="source" language="javascript" type="text/javascript"> <script id="source" language="javascript" type="text/javascript">
$(function () { $(function () {
var options = { lines: { show: true }, points: { show: true } }; var options = {
lines: { show: true },
points: { show: true },
xaxis: { tickDecimals: 0, tickSize: 1 }
};
var data = []; var data = [];
var placeholder = $("#placeholder");
$.plot(placeholder, data, options);
$.plot($("#placeholder"), data, options);
// fetch one series, adding to what we got
var alreadyFetched = {}; var alreadyFetched = {};
$("input[type=button]").click(function () { $("input.fetchSeries").click(function () {
var button = $(this); var button = $(this);
// find the URL in the link right next to us // find the URL in the link right next to us
...@@ -68,7 +82,7 @@ $(function () { ...@@ -68,7 +82,7 @@ $(function () {
} }
// and plot all we got // and plot all we got
$.plot($("#placeholder"), data, options); $.plot(placeholder, data, options);
} }
$.ajax({ $.ajax({
...@@ -78,6 +92,50 @@ $(function () { ...@@ -78,6 +92,50 @@ $(function () {
success: onDataReceived success: onDataReceived
}); });
}); });
// initiate a recurring data update
$("input.dataUpdate").click(function () {
// reset data
data = [];
alreadyFetched = {};
$.plot(placeholder, data, options);
var iteration = 0;
function fetchData() {
++iteration;
function onDataReceived(series) {
// we get all the data in one go, if we only got partial
// data, we could merge it with what we already got
data = [ series ];
$.plot($("#placeholder"), data, options);
}
$.ajax({
// usually, we'll just call the same URL, a script
// connected to a database, but in this case we only
// have static example files so we need to modify the
// URL
url: "data-eu-gdp-growth-" + iteration + ".json",
method: 'GET',
dataType: 'json',
success: onDataReceived
});
if (iteration < 5)
setTimeout(fetchData, 1000);
else {
data = [];
alreadyFetched = {};
}
}
setTimeout(fetchData, 1000);
});
}); });
</script> </script>
......
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9]]
}
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2]]
}
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5]]
}
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1]]
}
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
}
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<li><a href="basic.html">Basic example</a></li> <li><a href="basic.html">Basic example</a></li>
<li><a href="graph-types.html">Different graph types</a></li> <li><a href="graph-types.html">Different graph types</a></li>
<li><a href="setting-options.html">Setting various options</a> and <a href="annotating.html">annotating a chart</a></li> <li><a href="setting-options.html">Setting various options</a> and <a href="annotating.html">annotating a chart</a></li>
<li><a href="ajax.html">Using AJAX with JSON data</a></li> <li><a href="ajax.html">Updating graphs with AJAX</a></li>
</ul> </ul>
<p>Being interactive:</p> <p>Being interactive:</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment