-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptAll2.js
292 lines (265 loc) · 8.87 KB
/
scriptAll2.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
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
document.getElementById('upload-csv1').addEventListener('change', function (event) {
handleFileUpload(event, 0);
});
document.getElementById('upload-csv2').addEventListener('change', function (event) {
handleFileUpload(event, 1);
});
const filesData = [null, null];
function handleFileUpload(event, index) {
const file = event.target.files[0];
if (file) {
Papa.parse(file, {
header: true,
dynamicTyping: true,
complete: function (results) {
filesData[index] = results.data;
if (filesData[0] && filesData[1]) {
conversionOfData();
plotAllGraphs();
}
},
error: function (error) {
console.error('Error parsing the CSV file:', error);
}
});
}
}
function conversionOfData() {
// calculate the conversion ratio for pixels to mm
file1rulerDeltaX = [0];
file1rulerDeltaY = [0];
file1XLabels = [0];
file1YLabels = [0];
filesData[0].forEach((row, index) => {
file1Labels.push(index);
file1rulerDeltaX.push(row['ruler 0 mm x'] - row['ruler 20 mm x']);
file1YLabels.push(index);
file1rulerDeltaY.push(row['ruler 0 mm y'] - row['ruler 20 mm y']);
});
file2rulerDeltaX = [0];
file2rulerDeltaY = [0];
file2XLabels = [0];
file2YLabels = [0];
filesData[1].forEach((row, index) => {
file2XLabels.push(index);
file2rulerDeltaX.push(row['ruler 0 mm x'] - row['ruler 20 mm x']);
file2YLabels.push(index);
file2rulerDeltaY.push(row['ruler 0 mm y'] - row['ruler 20 mm y']);
});
let file1TotalDeltaX = 0;
for (let i = 1; i < file1Labels.length; i++) {
file1TotalDeltaX = file1rulerDeltaX[i] + file1TotalDeltaX;
}
const file1DeltaXAvg = file1TotalDeltaX / file1Labels.length;
let file2TotalDeltaX = 0;
for (let i = 1; i < file2Labels.length; i++) {
file2TotalDeltaX = file2rulerDeltaX[i] + file2TotalDeltaX;
}
const file2DeltaXAvg = file2TotalDeltaX / file2Labels.length;
let file1TotalDeltaY = 0;
for (let i = 1; i < file1Labels.length; i++) {
file1TotalDeltaY = file1rulerDeltaY[i] + file1TotalDeltaY;
}
const file1DeltaYAvg = file1TotalDeltaY / file1Labels.length;
let file2TotalDeltaY = 0;
for (let i = 1; i < file2Labels.length; i++) {
file2TotalDeltaY = file2rulerDeltaY[i] + file2TotalDeltaY;
}
const file2DeltaYAvg = file2TotalDeltaX / file2Labels.length;
const file1Pixels20 = Math.sqrt(file1DeltaXAvg * file1DeltaXAvg + file1DeltaYAvg * file1DeltaYAvg);
const FILE1MMCONVERSION = 20 / file1Pixels20;
const file2Pixels20 = Math.sqrt(file2DeltaXAvg * file2DeltaXAvg + file2DeltaYAvg * file2DeltaYAvg);
const FILE2MMCONVERSION = 20 / file2Pixels20;
}
function plotAllGraphs() {
clearPreviousGraphs();
plotTimeVsApexX();
plotTimeVsApexY();
plotApexXvsApexY(0, 'Apex X vs Apex Y (Dataset 1)', 'apexXvsApexY1');
plotApexXvsApexY(1, 'Apex X vs Apex Y (Dataset 2)', 'apexXvsApexY2');
plotTimeVsDisplacement();
}
function clearPreviousGraphs() {
const chartArea = document.getElementById('chartArea');
chartArea.innerHTML = '';
}
function createChartContainer(title, canvasId) {
const chartArea = document.getElementById('chartArea');
const container = document.createElement('div');
container.className = 'chart-container';
const titleElement = document.createElement('h3');
titleElement.textContent = title;
const canvas = document.createElement('canvas');
canvas.id = canvasId;
container.appendChild(titleElement);
container.appendChild(canvas);
chartArea.appendChild(container);
}
function plotTimeVsApexX() {
createChartContainer('Time vs Apex X', 'timeVsApexX');
const FPH = 100;
const framesToHoursX = [0];
const apexXValues = [0];
filesData[0].forEach((row, index) => {
framesToHoursX.push(index * FPH);
apexXValues.push(row['apex x'] * FILE1MMCONVERSION); // Convert to mm
});
const ctx = document.getElementById('timeVsApexX').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: framesToHoursX,
datasets: [{
label: 'Apex X (mm)',
data: apexXValues,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time (Minutes)'
}
},
y: {
title: {
display: true,
text: 'Apex X (mm)'
}
}
}
}
});
}
function plotTimeVsApexY() {
createChartContainer('Time vs Apex Y', 'timeVsApexY');
const framesToHoursY = [0];
const apexYValues = [0];
filesData[0].forEach((row, index) => {
labels.push(index);
apexYValues.push(row['apex y'] * FILE1MMCONVERSION); // Convert to mm
});
const ctx = document.getElementById('timeVsApexY').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: framesToHoursY,
datasets: [{
label: 'Apex Y (mm)',
data: apexYValues,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time (frames)'
}
},
y: {
title: {
display: true,
text: 'Apex Y (mm)'
}
}
}
}
});
}
function plotApexXvsApexY(index, title, canvasId) {
createChartContainer(title, canvasId);
const apexXValues = [];
const apexYValues = [];
filesData[index].forEach(row => {
apexXValues.push(row['apex x'] * FILE1MMCONVERSION); // Convert to mm
apexYValues.push(row['apex y'] * FILE1MMCONVERSION); // Convert to mm
});
const ctx = document.getElementById(canvasId).getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: apexXValues,
datasets: [{
label: title,
data: apexYValues,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 1,
pointRadius: 0, // Hide points
fill: false
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Apex X (mm)'
},
type: 'linear',
position: 'bottom'
},
y: {
title: {
display: true,
text: 'Apex Y (mm)'
}
}
}
}
});
}
function plotTimeVsDisplacement() {
createChartContainer('Time vs Displacement', 'timeVsDisplacement');
const frames = [0];
const displacements = [0];
let cumulativeDisplacement = 0;
for (let i = 1; i < filesData[0].length; i++) {
const prev = filesData[0][i - 1];
const curr = filesData[0][i];
const deltaX = (curr['apex x'] - prev['apex x']) * FILE1MMCONVERSION;
const deltaY = (curr['apex y'] - prev['apex y']) * FILE1MMCONVERSION;
const displacement = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
cumulativeDisplacement += displacement;
labels.push(i);
displacements.push(cumulativeDisplacement);
}
const ctx = document.getElementById('timeVsDisplacement').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: frames,
datasets: [{
label: 'Displacement (mm)',
data: displacements,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time (hours)'
}
},
y: {
title: {
display: true,
text: 'Displacement (mm)'
}
}
}
}
});
}