|
|
|
@ -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 () {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 () {
|
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
|
|
|