-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
104 lines (96 loc) · 4.03 KB
/
script2.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
document.getElementById('upload-csv').addEventListener('change', function (event) {
const file = event.target.files[0];
if (file) {
Papa.parse(file, {
header: true,
dynamicTyping: true,
complete: function (results) {
const data = results.data;
const frameRate = 48 / 3600; // 48 frames per hour = 48/3600 frames per second
const labels = [];
const displacements = [];
const rates = [];
for (let i = 1; i < data.length; i++) {
const frame1 = data[i - 1];
const frame2 = data[i];
// Calculate displacement using Pythagorean theorem
const deltaX = frame2['apex x'] - frame1['apex x'];
const deltaY = frame2['apex y'] - frame1['apex y'];
const displacement = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
// Calculate time in seconds since the start
const time = frame2.frame_number * frameRate;
// Calculate rate of movement
const rate = displacement * frameRate; // displacement per second
labels.push(time.toFixed(2));
displacements.push(displacement);
rates.push(rate);
}
// Create displacement chart
const ctxDisplacement = document.getElementById('displacementChart').getContext('2d');
new Chart(ctxDisplacement, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Displacement',
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 (seconds)'
}
},
y: {
title: {
display: true,
text: 'Displacement'
}
}
}
}
});
// Create rate of movement chart
const ctxRate = document.getElementById('rateChart').getContext('2d');
new Chart(ctxRate, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Rate of Movement',
data: rates,
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time (seconds)'
}
},
y: {
title: {
display: true,
text: 'Rate of Movement'
}
}
}
}
});
},
error: function (error) {
console.error('Error parsing the CSV file:', error);
}
});
}
});