-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtml+js-timeSeriesGetWithPagingAndChart.html
64 lines (57 loc) · 3.02 KB
/
html+js-timeSeriesGetWithPagingAndChart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://dygraphs.com/dygraph-combined.js"></script>
<meta charset="utf-8" />
<title>HTML5 and jQuery.js demonstration of the OptiRTC Public HTTP API.</title>
</head>
<body>
<p>HTML5 and jQuery.js demonstration of the OptiRTC Public HTTP API.</p>
<p>GET a time range of datapoints and draw a graph on this page and write the data to the browser's javascript console when all of the data from the original request has been received.</p>
<p>This example uses the Dygraphs project to render its UI.</p>
<p>In Google Chrome, open the Developer Tools and navigate to the Console tab to see mid-process output of this example.</p>
<div id="graphdiv"></div>
<script type="text/javascript">
//declare global variables
var dataValues = "Date, Temperature [deg F]\n";
var pages = 0;
var totalCount = 0;
function GetValues(url) {
$.ajax({
url: url,
dataType: "json",
}).done(function (data) {
pages = pages + 1;
//this example gets all of the datapoint objects of the request and puts them in a local array before returning the complete dataset
//alternate implementations might render these data points here as they come in to provide a more responsive and fluid page load
$.each(data.Items, function (i, item) {
dataValues = dataValues + item.timeValue + ',' + item.value[0].value + '\n';
});
totalCount = totalCount + data.Count;
//show the last page of data in the browser javascript console => remove in production systems
console.log(data);
//handle paging; next page of data provided by NextPageLink, which is typeof() == 'undefined' if the client just received the last page of data
//see http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript
if (typeof (data.NextPageLink) != 'undefined') {
GetValues(data.NextPageLink);
}
else {
//paging complete
console.log('\n');
console.log('Paging complete, total non-null datapoints received: ' + totalCount + ' in ' + pages + ' pages.');
g = new Dygraph(
document.getElementById("graphdiv"),
dataValues,
{}
);
}
});
};
$(document).ready(function () {
//load a document of OptiRTC data.
GetValues("https://public.optirtc.com/api/datapoint/?key={YOUR-KEY-HERE, NO BRACKETS}&dataStreamId={dataStreamId}&utcHistorical=5/2/2013&utcModern=5/8/2013");
});
</script>
</body>
</html>