-
Notifications
You must be signed in to change notification settings - Fork 1
/
realtimeScript.js
executable file
·158 lines (153 loc) · 5.27 KB
/
realtimeScript.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var arrayOfObjects;
var options;
var ws = new WebSocket(location.origin.replace("http", "ws"));
var iAm = "realtime";
console.log("attempt for client connection made");
console.log(location.pathname.split('/').pop());
var tick = 0.0;
var firstMessage = true;
ws.onmessage = function(message) {
console.log("got this message:" + message.data);
if (message.data.indexOf("refresh") >= 0) {
[what, tick, value] = message.data.split(" ");
arrayOfObjects[0].data.push([parseFloat(tick), parseInt(value)]);
if (arrayOfObjects[0].data.length > 120 || firstMessage) {
arrayOfObjects[0].data.shift();
firstMessage = false;
}
$.plot($("#placeholder"),arrayOfObjects,options);
};
if (message.data === 'exit') {
window.close();
};
if (message.data === 'connected') {
ws.send('iAm'+iAm);};
};
window.onload = function(){
var removedObjects = [];
options = { //set plot options
series: {
points: {show: true},
lines: {show: true}
},
xaxis: {
show: true,
},
yaxes: [{position: "left"}],
legend: {
show: true,
container: $("#legend")
},
grid: {
clickable: true,
hoverable: true
}
};
arrayOfObjects = [];
keys = [];
for (let aKey in collectedData) {
keys.push(aKey);
}
sorted_keys = keys.sort();
for (let index in sorted_keys) {
let series = sorted_keys[index];
arrayOfObjects.push({data:collectedData[series], label: series, yaxis: 1});
}
// plot measurements
$.plot($("#placeholder"),arrayOfObjects,options);
$("#placeholder").bind("plothover", function(event, pos, item) {
if (item) {
$("#rawData")[0].innerHTML = item.datapoint[1];
}});
$("#placeholder").bind("plotclick", function(event, pos, item) {
if (item == null) return;
return; //disable for now
removedObjects.push(arrayOfObjects.splice(item.seriesIndex,1)[0]);
$.plot($("#placeholder"),arrayOfObjects,options);
let insertionPoint = document.querySelector("#removedLines");
let box = document.createElement("input");
box.setAttribute("type", "checkbox");
box.setAttribute("value", item.series.label);
box.setAttribute("name", item.series.label);
box.innerHTML = item.series.label;
box.onclick = function(event) {
for (let seriesIndex = 0; seriesIndex < removedObjects.length; seriesIndex++) {
if (this.getAttribute("name") === removedObjects[seriesIndex].label) {
arrayOfObjects.push(removedObjects[seriesIndex]);
removedObjects.splice(seriesIndex,1);
$.plot($("#placeholder"),arrayOfObjects,options);
this.nextSibling.remove(); // remove label
this.remove(); // remove checkbox
if (removedObjects.length == 0) {
insertionPoint.removeAttribute("style");
}
break;
}
}
};
let label = document.createElement("label");
label.innerHTML = item.series.label;
insertionPoint.appendChild(box);
insertionPoint.appendChild(label);
insertionPoint.setAttribute("style", "display:block;");
});
// Create list of series
let insertionPoint = document.querySelector("#ownAxis");
let checkedBoxes = 0;
for (let seriesIndex = 0; seriesIndex < arrayOfObjects.length; seriesIndex++) {
let series = arrayOfObjects[seriesIndex];
let box = document.createElement("input");
box.setAttribute("type", "checkbox");
box.setAttribute("value", series.label);
box.setAttribute("name", series.label);
box.innerHTML = series.label;
box.onclick = function(event) {
let found = false;
for (let seriesIndex = 0; seriesIndex < arrayOfObjects.length; seriesIndex++) {
if (this.getAttribute("name") === arrayOfObjects[seriesIndex].label) {
if (arrayOfObjects[seriesIndex].yaxis === 1) {
checkedBoxes++;
if (options.yaxes.length < checkedBoxes + 1) {
options.yaxes.push({position: "right"});
}
arrayOfObjects[seriesIndex].yaxis = checkedBoxes + 1;
} else {
arrayOfObjects[seriesIndex].yaxis = 1;
checkedBoxes--;
}
$.plot($("#placeholder"),arrayOfObjects,options);
found = true;
break;
}
}
if (!found) {
for (let seriesIndex = 0; seriesIndex < removedObjects.length; seriesIndex++) {
if (this.getAttribute("name") === removedObjects[seriesIndex].label) {
if (removedObjects[seriesIndex].yaxis === 1) {
checkedBoxes++;
if (options.yaxes.length < checkedBoxes + 1) {
options.yaxes.push({position: "right"});
}
removedObjects[seriesIndex].yaxis = checkedBoxes + 1;
} else {
removedObjects[seriesIndex].yaxis = 1;
checkedBoxes--;
}
found = true;
break;
}
}
}
console.log("processing " + this.getAttribute("name") + ", checkedBoxes: " + checkedBoxes);
if (!found) {
console.log("logic error - element reference not found");
}
};
if (insertionPoint) {
let label = document.createElement("label");
label.innerHTML = series.label;
insertionPoint.appendChild(box);
insertionPoint.appendChild(label);
}
}
};