-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonar_plotter.html
528 lines (399 loc) · 15.9 KB
/
sonar_plotter.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<html>
<head>
<style type="text/css">
#container {
width:500px;
height:600px;
}
#controlpanel {
position: absolute;
left: 600px;
top:40px;
}
</style>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/jquery.flot.js"></script>
<script type="text/javascript" src="js/jquery.flot.time.js"></script>
<script type="text/javascript" src="js/jquery.flot.navigate.js"></script>
<script type="text/javascript" src="js/jquery.flot.fillbetween.js"></script>
<script type="text/javascript" src="js/FastBlur.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
var plot;
var seriesPairs = [];
var iii=0;
function initPlotter(){
// Test data
// Noise source should have a width parameter, so we represent it as a pair of series with different distance in each point. We'll fill this series between in order to achieve a monolithic form view.
// helper array to find which series are paired and selected
var pair = ['l+data','r+data','data', false];
seriesPairs.push(pair);
var data = [];
var rdata = [];
var t = new Date();
for (var i = 0; i <= 100; i++) {
var y = (t.getTime() + i * 3000);
var x = 120+Math.random()*5;
rdata.push([x+i*i/200+i/2, y]);
data.push([x+20+i*i/200, y]);
}
// set initial top/bottom of plot
var to = data[100][1]+500;
var bo = data[0][1]-500;
// set initial options
var options = {
grid: {
clickable: true,
backgroundColor: '#26292B',
},
hooks: { draw: [function(plot, canvascontext) {
for (var i=0;i<seriesPairs.length;i++) {
// Add a hook to fill the space between a given series
fillseries(plot, canvascontext, seriesPairs[i][0],seriesPairs[i][1], seriesPairs[i][3]); //
}
}],
//bindEvents: [ClickPlot]
},
series: {
color: '#269246',
lines: {
//clickable: true,
show: true,
lineWidth: 0,
fill: false
},
points: { show: false }
},
zoom: {
interactive: true
},
xaxis: {
min: 0,
max: 360,
//tickSize: 60,
position: "top",
zoomRange: [100, 360],// need to change to computable values to prevent stretch at zoom end
panRange: false
},
yaxis: {
max: to, // transfer initial top/bottom values
min: bo,
mode: "time",
zoomRange: [60000,360000],// need to change to computable values to prevent stretch
panRange: false,
//format labels
tickFormatter: function (v, axis) {
var dat = new Date(v);
//if (dat.getSeconds() % 20 == 0) {
var h = dat.getHours() < 10 ? "0" + dat.getHours() : dat.getHours();
var m = dat.getMinutes() < 10 ? "0" + dat.getMinutes() : dat.getMinutes();
var s = dat.getSeconds() < 10 ? "0" + dat.getSeconds() : dat.getSeconds();
return h + ":" + m + ":" + s;
//} else {
// return "";
//}
}
}
};
// Make/draw plot
plot = $("#container").plot([
{id: 'l+data', data: data},{id: 'r+data', data:rdata}
], options).data("plot");
}
function fillseries(plot, canvascontext, idl, idr, selected){
var ctx = canvascontext;
var s, series, series1,
i = 0,
a = true;
// Find series by names
while (a) {
s = plot.getData()[i];
if (s == undefined) {
//console.log('End of plot data');
break;
} else if (s.id == idl) {
series = s;
//console.log(s.id, ' found');
}
else if (s.id == idr) {
series1 = s;
//console.log(s.id, ' found');
break;
}
i++;
}
if (series != null && series1 != null) {
var points = series.datapoints.points,
points1 = series1.datapoints.points;
var xAxis = plot.getAxes().xaxis,
yAxis = plot.getAxes().yaxis;
var x,y;
// Start drawing
ctx.beginPath();
// First series
for (var i=0; i <= points.length; i++) {
if (i%2 == 1)
{
x = points[i-1];
y = points[i];
ctx.lineTo(xAxis.p2c(x)+plot.offset().left-8,yAxis.p2c(y)+plot.offset().top-8);
}
}
// Second series
for (var i=points1.length; i >= 0; i--) {
if (i%2 == 1)
{
x = points1[i-1];
y = points1[i];
ctx.lineTo(xAxis.p2c(x)+plot.offset().left-8,yAxis.p2c(y)+plot.offset().top-8);
}
}
// Close path by moving to the initial point
ctx.lineTo(xAxis.p2c(points[0]+plot.offset().left-8),yAxis.p2c(points[1])+plot.offset().top-8);
//ctx.closePath();
//ctx.fillStyle = "rgba(124,229,82, 0.5)";
var grd = ctx.createLinearGradient(0, 50, 0, 620);
//grd.addColorStop(0, '#8BEE6F');
grd.addColorStop(0, 'rgba(124,229,82, 0.1)');
//grd.addColorStop(1, "#438537");
grd.addColorStop(0.5, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.505, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.515, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.525, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.535, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.545, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.555, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.565, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.575, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.585, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.595, "rgba(124,229,82, 0.9)");
grd.addColorStop(0.663, "rgba(124,229,82, 0.9)");
grd.addColorStop(1, "rgba(124,229,82, 0.1)");
//ctx.stroke();
ctx.fillStyle = grd;
// Add stroke to selected series
if (selected) {
$('#selectedSeries').html(idl);
ctx.lineWidth = 10;
ctx.strokeStyle = "rgba(99,229,82, 0.2)";
ctx.fill();
ctx.stroke();
}
else {
ctx.fill();
}
// Close offset areas of previous fill which appears while graph zooming by white rectangles
ctx.beginPath();
ctx.rect(0,0,500,20);
ctx.rect(0,594,500,6);
ctx.fillStyle = 'white';
ctx.fill();
// Close frame
ctx.beginPath();
ctx.rect(56,20,435,3);
ctx.rect(56,591,435,3);
ctx.fillStyle = '#424242';
ctx.fill();
}
else console.log('fillseries: not found');
}
// // Click hook
// function ClickPlot(plot, eventHolder) {
// var xAxis = plot.getAxes().xaxis;
// var yAxis = plot.getAxes().yaxis;
// eventHolder.mousedown(function (e) {
// alert("You pressed the mouse at " + e.pageX + " " + e.pageY);
// });
// }
//Plot click event
$("#container").bind("plotclick", function (event, pos, item) {
// Remove any selections
removeSelections();
// If we pointed series
if (item){
var series = item.series.id;
// Find pair of current series
for (var i = 0; i < seriesPairs.length; i++){
if (seriesPairs[i][0] == series || seriesPairs[i][1] == series) {
// Set current series selected status in seriesPairs array.
// Before plot will draw it'll iterate over seriesPair.
seriesPairs[i][3] = true;
// Redraw with selections
var pData = plot.getData(),
pOptions = plot.getOptions();
plot = $.plot($("#container"), pData, pOptions);
break;
}
}
}
else {
// Redraw, selections have been already removed earlier
var pData = plot.getData(),
pOptions = plot.getOptions();
plot = $.plot($("#container"), pData, pOptions);
}
});
function removeSelections(){
$('#selectedSeries').html('');
for (var i = 0; i < seriesPairs.length; i++){
seriesPairs[i][3] = false;
}
}
var plotupdater = setInterval(function() {
var n = new Date(); // current time
var y = n.getTime() + 100 * 3000; // time of new detections falling from the top
// random test x data
var x = 120+Math.random()*20;
var plData = plot.getData();
for (var i=0;i<plData.length;i++) {
if (plData[i].id == 'l+data') {
// Update points
plData[i].data.push([x, y]); // left series
plData[i+1].data.push([x+20, y]); // right series
plData[i].data.splice(0, 1); // left series
plData[i+1].data.splice(0, 1); // right series
}
}
// In order to move plot we need to increase it's borders in options
plot.getOptions().yaxes[0].max += 3000;
plot.getOptions().yaxes[0].min += 3000;
plot.setupGrid();
// get current axes
var axes = plot.getAxes();
// get current options without zoom modifications
var pOptions = plot.getOptions();
// set zoom modifications picked from the state of current axes
pOptions.yaxis.max = axes.yaxis.max;
pOptions.yaxis.min = axes.yaxis.min;
//console.log(pOptions.yaxis.max);
pOptions.xaxis.min = axes.xaxis.min;
pOptions.xaxis.max = axes.xaxis.max;
// draw new cadre, save plot object
plot = $.plot($("#container"), plData, pOptions);
//$.extend(true, {}, options, { yaxis: { min: axes.yaxis.min, max: axes.yaxis.max } })
}, 3000);
// Prepare api functions
function clear() {
data = [0,0];
var pOptions = plot.getOptions();
plot = $.plot('#container', data, pOptions);
}
function addHeading(time, bearing) {
//
}
function addDetection(time, seriesName, bearing, strength){
// We need to add two series to the graph in order to show the width
var lseriesName = 'l+' + seriesName, // Left series id
rseriesName = 'r+' + seriesName; // Right series id
var k = 1; // Width koeff
// Split bearing into 2 points
var bearingl = bearing - k*strength,
bearingr = bearing + k*strength;
// Get plot data
var plData = plot.getData();
var notfound = true,
timeDelta;
// Search wether lseriesName exists in current plot data
for (var i=0;i<plData.length;i++) {
if (plData[i].id == lseriesName) {
// Update points
plData[i].data.push([bearingl, time]); // left series
plData[i+1].data.push([bearingr, time]); // right series
// !!! Need to add 1st points removal from all series which are outside the display
// get current axes
var axes = plot.getAxes();
//console.log('axes: ',axes);
// In order to move plot we need to increase it's borders in options
//var l = plData[i].data.length;
timeDelta = time - axes.yaxis.datamax; // time between last point in data and given time
// Update plot options to move it
plot.getOptions().yaxes[0].max += timeDelta;
plot.getOptions().yaxes[0].min += timeDelta;
plot.setupGrid();
// get current options without zoom modifications
var plOptions = plot.getOptions();
// set zoom modifications picked from the state of current axes
plOptions.yaxis.max = axes.yaxis.max;
plOptions.yaxis.min = axes.yaxis.min;
//console.log(pOptions.yaxis.max);
plOptions.xaxis.min = axes.xaxis.min;
plOptions.xaxis.max = axes.xaxis.max;
// Save and update plot
plot = $.plot('#container', plData, plOptions);
notfound = false;
break;
}
}
if (notfound) {
//console.log('not found');
// Prepare array for data
var allseries = [];
// Update seriesPairs
seriesPairs.push([lseriesName, rseriesName, seriesName, false]);
// Create new data series - lseriesNameData, rseriesNameData
var lseriesNameData = [], rseriesNameData = [];
lseriesNameData.push([bearingl, time]);
rseriesNameData.push([bearingr, time]);
var i;
// Save old data
for (i=0;i<plData.length;i++) {
allseries.push(plData[i]);
}
// Add new data
allseries.push({id: lseriesName, data: lseriesNameData});
allseries.push({id: rseriesName, data: rseriesNameData});
// Update plot
// get current axes
var axes = plot.getAxes();
// In order to move plot we need to increase it's borders in options
if (plData[i-1] != null) {
//var l = plData[i-1].data.length;
//timeDelta = time - plData[i-1].data[l-1][1]; // time between last point in data and given time
timeDelta = time - axes.yaxis.datamax; // time between last point in data and given time
} else {
timeDelta = 3000; // If no data before set timeDelta to 3s
}
// Update plot options to move it
plot.getOptions().yaxes[0].max += timeDelta;
plot.getOptions().yaxes[0].min += timeDelta;
plot.setupGrid();
// get current options without zoom modifications
var plOptions = plot.getOptions();
// set zoom modifications picked from the state of current axes
plOptions.yaxis.max = axes.yaxis.max;
plOptions.yaxis.min = axes.yaxis.min;
//console.log(pOptions.yaxis.max);
plOptions.xaxis.min = axes.xaxis.min;
plOptions.xaxis.max = axes.xaxis.max;
// Save and update plot
plot = $.plot('#container', allseries, plOptions);
}
}
// listener?
// Control panel events
$( document ).ready(function() {
initPlotter();
$('#clearButton').on('click', function() {
$('#selectedSeries').html('');
clearInterval(plotupdater);
clear();
});
$('#nextButton').on('click', function() {
var n = new Date(); // current time
var y = n.getTime() + 100 * 3000; // time of new detections falling from the top
// random test x data
var x = 120+Math.random()*20;
iii = iii + 1;
if (iii > 3) {addDetection(y, 'fghfg', x, 10);} else {addDetection(y, 'gsgs', x, 10);}
});
});
</script>
<div id="controlpanel">
<div id="clearButton" style="display:block;width:200px;height:30px;border-radius:5px;background:lightgrey;color:white;font-family:sans-serif;font-size:22px;padding:30 0 30 100;cursor:pointer">Clear data</div>
<!--<div id="nextButton" style="margin-top:100px;display:block;width:200px;height:30px;border-radius:5px;background:lightgrey;color:white;font-family:sans-serif;font-size:22px;padding:30 0 30 100;cursor:pointer">Next</div>-->
<div id="selectedSeries" style="margin-top:200px;display:block;width:200px;height:30px;border-radius:5px;background:lightgrey;color:white;font-family:sans-serif;font-size:22px;padding:30 0 30 100;"></div>
</div>
</body>
</html>