Skip to content

Commit 0c1d441

Browse files
Smoothen recording graph data (#205)
1 parent 02c2599 commit 0c1d441

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
/**
5+
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
6+
*
7+
* SPDX-License-Identifier: MIT
8+
*/
9+
10+
import { smoothenXYZData } from '../script/smoothenXYZData';
11+
12+
describe('smoothenXYZData', () => {
13+
test('smoothen empty data', () => {
14+
const xyz = {
15+
x: [],
16+
y: [],
17+
z: [],
18+
};
19+
expect(smoothenXYZData(xyz)).toEqual(xyz);
20+
});
21+
test('smoothen xyz data', () => {
22+
const xyz = {
23+
x: [1, 1, 1, 1, 1],
24+
y: [4, 4, 12, 10, 10],
25+
z: [8, 8, 24, 20, 20],
26+
};
27+
const smoothXYZData = {
28+
x: [1, 1, 1, 1, 1],
29+
y: [4, 4, 6, 7, 7.75],
30+
z: [8, 8, 12, 14, 15.5],
31+
};
32+
expect(smoothenXYZData(xyz)).toEqual(smoothXYZData);
33+
});
34+
});

src/components/graphs/RecordingGraph.svelte

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
PointElement,
1818
} from 'chart.js';
1919
import RecordingInspector from '../3d-inspector/RecordingInspector.svelte';
20+
import { smoothenXYZData } from '../../script/smoothenXYZData';
2021
2122
export let data: { x: number[]; y: number[]; z: number[] };
2223
24+
const smoothData = smoothenXYZData(data);
2325
let verticalLineX = NaN;
2426
let hoverIndex = NaN;
2527
let modalPosition = { x: 0, y: 0 };
@@ -34,9 +36,9 @@
3436
return { x: 0, y: 0, z: 0 };
3537
}
3638
return {
37-
x: data.x[index],
38-
y: data.y[index],
39-
z: data.z[index],
39+
x: smoothData.x[index],
40+
y: smoothData.y[index],
41+
z: smoothData.z[index],
4042
};
4143
};
4244
@@ -86,10 +88,10 @@
8688
const x: { x: number; y: number }[] = [];
8789
const y: { x: number; y: number }[] = [];
8890
const z: { x: number; y: number }[] = [];
89-
for (let i = 1; i < data.x.length; i++) {
90-
x.push({ x: i, y: data.x[i - 1] });
91-
y.push({ x: i, y: data.y[i - 1] });
92-
z.push({ x: i, y: data.z[i - 1] });
91+
for (let i = 1; i < smoothData.x.length; i++) {
92+
x.push({ x: i, y: smoothData.x[i - 1] });
93+
y.push({ x: i, y: smoothData.y[i - 1] });
94+
z.push({ x: i, y: smoothData.z[i - 1] });
9395
}
9496
return {
9597
type: 'line',
@@ -133,7 +135,7 @@
133135
x: {
134136
type: 'linear',
135137
min: 0,
136-
max: data.x.length,
138+
max: smoothData.x.length,
137139
grid: {
138140
drawTicks: false,
139141
display: false,

src/script/smoothenXYZData.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
interface XYZData {
8+
x: number[];
9+
y: number[];
10+
z: number[];
11+
}
12+
13+
const smoothen = (d: number[]): number[] => {
14+
if (d.length === 0) {
15+
return d;
16+
}
17+
const newData: number[] = [];
18+
let prevValue = d[0];
19+
d.forEach(v => {
20+
const newValue = v * 0.25 + prevValue * 0.75;
21+
newData.push(newValue);
22+
prevValue = newValue;
23+
});
24+
return newData;
25+
};
26+
27+
// Smoothen data
28+
export function smoothenXYZData(d: XYZData): XYZData {
29+
return {
30+
x: smoothen(d.x),
31+
y: smoothen(d.y),
32+
z: smoothen(d.z),
33+
};
34+
}

0 commit comments

Comments
 (0)