-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.js
70 lines (57 loc) · 1.72 KB
/
graph.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
"use strict";
const { TimeSeries, SmoothieChart } = window;
const accelerometer = createGraph("accelerometer");
const gyroscope = createGraph("gyroscope");
const magnetometer = createGraph("magnetometer");
function listener(data) {
updateGraph(accelerometer, data[0], data[1], data[2]);
updateGraph(gyroscope, data[3], data[4], data[5]);
updateGraph(magnetometer, data[6], data[7], data[8]);
}
function start(movuino) {
movuino.on("data", listener);
document.querySelector("#graphwrapper").hidden = false;
}
function stop(movuino) {
movuino.removeListener("data", listener);
document.querySelector("#graphwrapper").hidden = true;
}
function createGraph(sensor) {
const smoothie = new SmoothieChart({
grid: {
fillStyle: "transparent",
strokeStyle: "transparent",
borderVisible: false
},
millisPerPixel: 3,
maxValue: 1,
minValue: -1,
responsive: true
});
const canvas = document.querySelector("." + sensor);
smoothie.streamTo(canvas);
const xline = new TimeSeries();
const yline = new TimeSeries();
const zline = new TimeSeries();
smoothie.addTimeSeries(xline, {
lineWidth: 2.2,
strokeStyle: "rgba(255,0,0,1)"
});
smoothie.addTimeSeries(yline, {
lineWidth: 2.2,
strokeStyle: "rgba(0,0,255,1)"
});
smoothie.addTimeSeries(zline, {
lineWidth: 2.2,
strokeStyle: "rgba(0,255,0,1)"
});
return smoothie;
}
function updateGraph(graph, x, y, z) {
const [xTimeSeries, yTimeSeries, zTimeSeries] = graph.seriesSet;
xTimeSeries.timeSeries.append(new Date().getTime(), x);
yTimeSeries.timeSeries.append(new Date().getTime(), y);
zTimeSeries.timeSeries.append(new Date().getTime(), z);
}
module.exports.start = start;
module.exports.stop = stop;