-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (124 loc) · 4.25 KB
/
index.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
// Load signals
async function loadJson() {
fetch("./signals.json").then(response =>
response.json().then(data => ({
data: data,
status: response.status
})
).then(res => {
// Load
let ys = res.data;
let sampling_rate = 500;
// Time vector
var x = [];
for (i=0; i<1000; i++){
x.push(i/sampling_rate);
};
// Graph divs
var graphDivs = [];
for (i=0; i<48; i++){
// Create div for each simulation
let graphDiv = document.createElement("div");
graphDiv.id = "graphDiv" + i;
// Append the div to a centered div
centerDiv.appendChild(graphDiv);
// Create plots
Plotly.newPlot(graphDiv, [
// Signal
{
mode: 'lines',
x: x,
y: ys["sig_" + i],
xaxis: 'x',
yaxis: 'y',
name: 'Signal',
line: {
color: 'black'
}
},{
// Burst
mode: 'lines',
x: [],
y: [],
xaxis: 'x',
yaxis: 'y',
name: 'Burst',
line: {
color: 'red'
}
}], {
// Layout
dragmode: 'select',
selectdirection: "h",
xaxis: {zeroline: false},
showlegend: true,
width: 1000,
height: 150,
margin: {
l: 25,
r: 25,
b: 25,
t: 25,
pad: 5
}}
);
graphDivs.push(graphDiv);
};
console.log(graphDivs);
console.log(graphDivs.length);
// Selection callback
let gi = 1;
for (gi=0; gi<48; gi++){
let gDiv = graphDivs[gi]
gDiv.on('plotly_selected', function(eventData) {
try {
// Get lower and upper bounds of selected bursts
let xselect = [eventData.range.x[0], eventData.range.x[1]];
let xburst = [];
let yburst = [];
for (xi = 0; xi < x.length; xi++) {
if (gDiv.data[0]['x'][xi] >= xselect[0] && gDiv.data[0]['x'][xi] <= xselect[1]) {
xburst.push(gDiv.data[0]['x'][xi]);
yburst.push(gDiv.data[0]['y'][xi]);
}
};
// Update red burst signal
gDiv.data[1]['x'] = xburst;
gDiv.data[1]['y'] = yburst;
// Replot updated bursts
var replot = () => {
// Bug in plotly that sometimes leads to infinite
// recursion. Timing out to fix.
setTimeout(() => {
Plotly.redraw(gDiv, [1]);
}, 200);
}
replot();
} catch (error) {// Get lower and upper bounds of selected bursts
let xselect = [];
let xburst = [];
let yburst = [];
for (xi = 0; xi < x.length; xi++) {
if (gDiv.data[0]['x'][xi] >= xselect[0] && gDiv.data[0]['x'][xi] <= xselect[1]) {
xburst.push(gDiv.data[0]['x'][xi]);
yburst.push(gDiv.data[0]['y'][xi]);
}
};
// Update red burst signal
gDiv.data[1]['x'] = xburst;
gDiv.data[1]['y'] = yburst;
// Replot updated bursts
var replot = () => {
// Bug in plotly that sometimes leads to infinite
// recursion. Timing out to fix.
setTimeout(() => {
Plotly.redraw(gDiv, [1]);
}, 200);
}
replot();
}
});
};
}));
};
loadJson();