Files
gpx.studio/website/src/lib/components/ElevationProfile.svelte

137 lines
2.8 KiB
Svelte
Raw Normal View History

2024-04-20 23:17:11 +02:00
<script lang="ts">
import Chart from 'chart.js/auto';
import { selectedFiles } from '$lib/stores';
import { onMount } from 'svelte';
let canvas: HTMLCanvasElement;
let chart: Chart;
Chart.defaults.font.family =
'ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"'; // Tailwind CSS font
onMount(() => {
chart = new Chart(canvas, {
type: 'line',
data: {
datasets: []
},
options: {
animation: false,
parsing: false,
maintainAspectRatio: false,
scales: {
x: {
type: 'linear',
title: {
display: true,
2024-04-21 12:18:54 +02:00
text: 'Distance (km)',
padding: 0
2024-04-20 23:17:11 +02:00
}
},
y: {
type: 'linear',
title: {
display: true,
2024-04-21 12:18:54 +02:00
text: 'Elevation (m)',
padding: 0
2024-04-20 23:17:11 +02:00
}
2024-04-21 12:13:58 +02:00
},
y1: {
type: 'linear',
position: 'right',
title: {
display: true,
2024-04-21 12:18:54 +02:00
text: 'Speed (km/h)',
padding: 0
2024-04-21 12:13:58 +02:00
},
grid: {
display: false
}
},
y2: {
type: 'linear',
position: 'right',
title: {
display: true,
2024-04-21 12:18:54 +02:00
text: 'Slope (%)',
padding: 0
2024-04-21 12:13:58 +02:00
},
grid: {
display: false
}
2024-04-20 23:17:11 +02:00
}
},
datasets: {
line: {
2024-04-21 12:13:58 +02:00
pointRadius: 0,
tension: 0.4
2024-04-20 23:17:11 +02:00
}
},
interaction: {
2024-04-21 12:13:58 +02:00
mode: 'nearest',
axis: 'x',
2024-04-20 23:17:11 +02:00
intersect: false
},
plugins: {
legend: {
2024-04-21 12:13:58 +02:00
display: false
2024-04-20 23:17:11 +02:00
},
decimation: {
enabled: true
}
2024-04-21 12:13:58 +02:00
},
stacked: false
2024-04-20 23:17:11 +02:00
}
});
});
$: {
if ($selectedFiles.size == 1) {
$selectedFiles.forEach((file) => {
const trackPointsAndStatistics = file.getTrackPointsAndStatistics();
chart.data.datasets[0] = {
label: 'Elevation',
data: trackPointsAndStatistics.points.map((point, index) => {
return {
x: trackPointsAndStatistics.statistics.distance[index],
y: point.ele ? point.ele : 0
};
}),
2024-04-21 12:13:58 +02:00
normalized: true,
fill: true
};
chart.data.datasets[1] = {
label: 'Speed',
data: trackPointsAndStatistics.points.map((point, index) => {
return {
x: trackPointsAndStatistics.statistics.distance[index],
y: trackPointsAndStatistics.statistics.speed[index]
};
}),
normalized: true,
yAxisID: 'y1'
};
chart.data.datasets[2] = {
label: 'Slope',
data: trackPointsAndStatistics.points.map((point, index) => {
return {
x: trackPointsAndStatistics.statistics.distance[index],
y: trackPointsAndStatistics.statistics.slope[index]
};
}),
normalized: true,
yAxisID: 'y2'
2024-04-20 23:17:11 +02:00
};
chart.options.scales.x['min'] = 0;
chart.options.scales.x['max'] = file.statistics.distance.total;
});
chart.update();
}
}
</script>
2024-04-21 12:13:58 +02:00
<div class="h-full grow min-w-0 py-4">
2024-04-20 23:17:11 +02:00
<canvas bind:this={canvas}> </canvas>
</div>