-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
107 lines (95 loc) · 3.05 KB
/
app.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
This is a website displaying graphs using a basic backend that makes API requests to a Arduino that's hooked with sensors detecting certain
particles in the air.
It was used for the Hackathon 'De Warmste Week' on 12 october 2019.
Website developed by: Imanuel Vancauteren
Team: All you need is the air that you breath
Current display is configured to show temperature, Water vapor & air pressure.
Json structure used:
[
25.4,63.4,105.4
]
*/
'use strict';
console.log('loaded')
let tempAvg = [];
let vochtAvg = [];
let drukAvg = [];
$(document).ready(function () {
let jsonURL = "https://jsonplaceholder.typicode.com/todos/1"; //put an api link here to get json
Plotly.plot('chart1',[{
y:[],
type:'line',
}]);
;
Plotly.plot('chart2',[{
y:[],
type:'line',
}]);
;
Plotly.plot('chart3',[{
y:[],
type:'line',
}]);
;
let cnt = 0;
setInterval(function(){
$.getJSON(jsonURL, async function(data){
console.log('loading new JSON data!')
//Rng has been used for demo purposes:
let rngT = Math.floor(Math.random() * 20) + 10;
let rngV = Math.floor(Math.random() * 100) + 1;
let rngD = Math.floor(Math.random() * 10000) + 1000;
console.log(rngT);
console.log(rngD);
console.log(rngV);
// Plotly.extendTraces('chart1',{ y:[[getValue(data[0],0)]]}, [0]);
//Plotly.extendTraces('chart2',{ y:[[getValue(data[0],1)]]}, [0]);
//Plotly.extendTraces('chart3',{ y:[[getValue(data[0],2)/1000]]}, [0]);
Plotly.extendTraces('chart1',{ y:[[rngT]]}, [0]);
Plotly.extendTraces('chart2',{ y:[[rngV]]}, [0]);
Plotly.extendTraces('chart3',{ y:[[rngD]]}, [0]);
cnt++;
//tempAvg.push(parseInt(getValue(data[0],0),10));
tempAvg.push(rngT);
vochtAvg.push(rngV);
drukAvg.push(rngD);
//vochtAvg.push(parseInt(getValue(data[0],1),10));
//drukAvg.push(parseInt(getValue(data[0],2),10));
$('#val1').text(average((tempAvg)));
$('#val2').text(average(vochtAvg));
$('#val3').text(average(drukAvg));
//var d = new Date();
//var t = d.toLocaleTimeString();
//var s = t.substring(0, t.length-3);
if(cnt > 20) {
Plotly.relayout('chart1',{
xaxis: {
range: [cnt-20,cnt]
}
});
Plotly.relayout('chart2',{
xaxis: {
range: [cnt-20,cnt]
}
});
Plotly.relayout('chart3',{
xaxis: {
range: [cnt-20,cnt]
}
});
}
})},1000);
})
function getValue(str, i) {
let m = str.substring(1, str.length-1);
let valArr = m.split(',');
return valArr[i];
}
function average(arr) {
let ctr = 0;
for(let x = 0; x < arr.length; x++) {
ctr+=arr[x];
}
return (ctr/arr.length).toFixed(2);
}