-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.js
87 lines (82 loc) · 1.84 KB
/
chart.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
/*
* Compile with uglify.js
* uglifyjs chart.js -mt -m sort -e | tr \" \' | sed "s/\\\n/\\\\\\\n/" | sed "s/^[^{]\+{\(.*\)})();$/\"<script>\1<\/script>\"/" | sed "s/\([^\"]\{74\}\)/\1\"\n\"/g"
* remove the surrounding function() and escape \n
*/
// Do not use UTC to show correct DST times
Highcharts.setOptions({
global: {
useUTC: false
}
});
var values = document.querySelector("#x").innerHTML.split("\n"),
humi = [],
temp = [];
for(i=0;i<values.length;i++) {
var v = values[i].split(" ");
if( v.length == 4 || v.length == 2) {
if( v.length == 4) {
var cur_time = parseInt(v[0]),
interval = parseInt(v[1]),
cur_humi = parseInt(v[2]),
cur_temp = parseInt(v[3]);
} else {
cur_time += interval;
cur_humi += parseInt(v[0]);
cur_temp += parseInt(v[1]);
}
humi.push([cur_time * 1000, cur_humi / 10.0]);
temp.push([cur_time * 1000, cur_temp / 10.0]);
}
}
var chart = new Highcharts.StockChart({
chart: {
renderTo: 'x'
},
title: {
text: 'Meteo'
},
rangeSelector: {
buttons: [{
type: 'day',
count: 1,
text: '1d'
}, {
type: 'week',
count: 1,
text: '1w'
}, {
type: 'month',
count: 1,
text: '1m'
}, {
type: 'ytd',
text: 'YTD'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'all',
text: 'All'
}]
},
legend: {
enabled: true
},
series: [{
name: 'Humidity',
data: humi,
gapSize: 10
}, {
name: 'Temperature',
data: temp,
gapSize: 10
}]
});
var source = new EventSource('/i');
source.onmessage = function(evt) {
var data = evt.data.split(" ");
chart.series[0].addPoint([parseInt(data[0]) * 1000,parseInt(data[1]) / 10.0]);
chart.series[1].addPoint([parseInt(data[0]) * 1000,parseInt(data[2]) / 10.0]);
};