Profile: the time axis is scaled by moving time (pauses take no width), splits and laps redrawn on it
CI / check (push) Successful in 18s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-17 16:57:19 +01:00
1 parent 38368fc801
commit 066e831219
1 file changed
+43 -23
+43 -23
View File
@@ -30,6 +30,7 @@ const BEST_DISTANCES_KM = [1, 5, 10, 21.0975, 42.195, 100];
const BEST_POWER_SECS = [5, 60, 300, 1200, 3600];
const CLIMB_TOLERANCE_M = 10; // a dip smaller than this doesn't end a climb
const MILEPOST_SOURCE = "profile-mileposts";
const MOVING_MPS = 0.3; // slower counts as a pause on the time axis
export class TrackProfilePanel {
constructor(map) {
@@ -51,7 +52,7 @@ export class TrackProfilePanel {
if (!profile || profile.length === 0) return;
this._profile = profile.map((point) => ({ ...point, speed_kmh: typeof point.speed_mps === "number" ? point.speed_mps * 3.6 : null }));
this._maxDistance = this._profile.at(-1).distance_meters || 1;
addMovingTime(this._profile);
this._title.textContent = properties.name || "Track";
this._stats.textContent = statsText(properties);
@@ -69,7 +70,7 @@ export class TrackProfilePanel {
// Only offer the time axis when there's actually elapsed-time data —
// plenty of GPX files have no <time> at all.
this._hasTime = this._profile.some((p) => typeof p.elapsed_secs === "number");
this._xAxisMode = "distance";
this._setXAxis("distance");
this._activeSeries = SERIES.filter((s) => this._profile.some((p) => typeof p[s.key] === "number"));
this._chartsContainer.innerHTML = "";
@@ -112,6 +113,18 @@ export class TrackProfilePanel {
this._showMileposts();
}
// Distance, or moving time: pauses (auto-pause gaps, standing at a
// crossing) take no width, so the line doesn't jump or go flat there.
_setXAxis(mode) {
this._xAxisMode = mode;
this._xKey = mode === "time" ? "moving_secs" : "distance_meters";
this._maxX = this._profile.at(-1)[this._xKey] || 1;
}
_fx(point) {
return point[this._xKey] / this._maxX;
}
_segmentLabel(n) {
if (this._segmentMode === "laps") return `L${n}`;
const km = this._profile[this._bounds[n]].distance_meters / 1000;
@@ -127,8 +140,8 @@ export class TrackProfilePanel {
let html = "";
for (let n = 1; n <= count; n++) {
if (n % every && n !== count) continue;
const x1 = this._profile[this._bounds[n - 1]].distance_meters / this._maxDistance;
const x2 = this._profile[this._bounds[n]].distance_meters / this._maxDistance;
const x1 = this._fx(this._profile[this._bounds[n - 1]]);
const x2 = this._fx(this._profile[this._bounds[n]]);
html += `<span style="left:${(((x1 + x2) / 2) * 100).toFixed(2)}%">${n}</span>`;
}
strip.innerHTML = html;
@@ -174,14 +187,14 @@ export class TrackProfilePanel {
const segments = [["1000", "1 km"], ["5000", "5 km"], ["10000", "10 km"], ...(this._laps ? [["laps", `Laps (${this._laps.length})`]] : [])];
const group = (name, options, current) =>
`<span class="profile-panel__toggle" data-group="${name}">${options.map(([value, label]) => `<button type="button" class="profile-panel__xaxis-toggle-btn${value === current ? " active" : ""}" data-value="${value}">${label}</button>`).join("")}</span>`;
row.innerHTML = `<span class="profile-panel__controls-label">Splits</span>${group("segments", segments, this._segmentMode)}<span class="profile-panel__controls-spacer"></span>${this._hasTime ? group("axis", [["distance", "Distance"], ["time", "Time"]], this._xAxisMode) : ""}`;
row.innerHTML = `<span class="profile-panel__controls-label">Splits</span>${group("segments", segments, this._segmentMode)}<span class="profile-panel__controls-spacer"></span>${this._hasTime ? group("axis", [["distance", "Distance"], ["time", "Moving time"]], this._xAxisMode) : ""}`;
row.addEventListener("click", (event) => {
const btn = event.target.closest("button");
if (!btn || btn.classList.contains("active")) return;
btn.parentElement.querySelectorAll("button").forEach((b) => b.classList.toggle("active", b === btn));
if (btn.parentElement.dataset.group === "axis") {
this._xAxisMode = btn.dataset.value;
this._renderXAxisTicks();
this._setXAxis(btn.dataset.value);
this._renderSegments();
} else {
this._segmentMode = btn.dataset.value;
this._renderSegments();
@@ -204,11 +217,11 @@ export class TrackProfilePanel {
const max = Math.max(...values);
const range = max - min || 1;
const toX = (d) => (d / this._maxDistance) * VIEWBOX_WIDTH;
const toX = (p) => this._fx(p) * VIEWBOX_WIDTH;
const toY = (v) => series.height - 6 - ((v - min) / range) * (series.height - 12);
const points = this._profile
.map((p) => (typeof p[series.key] === "number" ? [toX(p.distance_meters), toY(p[series.key])] : null))
.map((p) => (typeof p[series.key] === "number" ? [toX(p), toY(p[series.key])] : null))
.filter(Boolean);
const linePath = points.map(([x, y], i) => `${i === 0 ? "M" : "L"}${x.toFixed(1)},${y.toFixed(1)}`).join(" ");
const areaPath = series.area && points.length > 0
@@ -217,8 +230,8 @@ export class TrackProfilePanel {
let bands = "";
for (let n = 1; n < this._bounds.length; n += 2) {
const x1 = toX(this._profile[this._bounds[n - 1]].distance_meters);
const x2 = toX(this._profile[this._bounds[n]].distance_meters);
const x1 = toX(this._profile[this._bounds[n - 1]]);
const x2 = toX(this._profile[this._bounds[n]]);
bands += `<rect x="${x1.toFixed(1)}" width="${(x2 - x1).toFixed(1)}" y="0" height="${series.height}" class="profile-chart__segment"></rect>`;
}
@@ -250,13 +263,13 @@ export class TrackProfilePanel {
const colors = trailColorsAlong(this._profile);
if (!colors) return null;
const HEIGHT = 10;
const toX = (d) => (d / this._maxDistance) * VIEWBOX_WIDTH;
const toX = (p) => this._fx(p) * VIEWBOX_WIDTH;
let rects = "";
let start = 0;
for (let i = 1; i < this._profile.length; i++) {
const run = colors[start];
if (i < this._profile.length - 1 && colors[i].join() === run.join()) continue;
const [x1, x2] = [toX(this._profile[start].distance_meters), toX(this._profile[i].distance_meters)];
const [x1, x2] = [toX(this._profile[start]), toX(this._profile[i])];
run.forEach((color, j) => {
rects += `<rect x="${x1.toFixed(1)}" width="${(x2 - x1).toFixed(1)}" y="${((j * HEIGHT) / run.length).toFixed(2)}" height="${(HEIGHT / run.length).toFixed(2)}" fill="${color}"></rect>`;
});
@@ -276,10 +289,8 @@ export class TrackProfilePanel {
const TICK_COUNT = 5;
this._xAxisStripEl.innerHTML = Array.from({ length: TICK_COUNT }, (_, i) => {
const fraction = i / (TICK_COUNT - 1);
const point = this._profile[nearestIndex(this._profile, fraction * this._maxDistance)];
const label = this._xAxisMode === "time" && typeof point.elapsed_secs === "number"
? formatDuration(point.elapsed_secs)
: `${(point.distance_meters / 1000).toFixed(1)} km`;
const value = fraction * this._maxX;
const label = this._xAxisMode === "time" ? formatDuration(value) : `${(value / 1000).toFixed(1)} km`;
return `<span class="profile-panel__xaxis-tick" style="left:${(fraction * 100).toFixed(2)}%">${escapeHtml(label)}</span>`;
}).join("");
}
@@ -288,9 +299,7 @@ export class TrackProfilePanel {
_onPointerMove = (event) => {
const rect = this._plots.getBoundingClientRect();
const fraction = Math.min(1, Math.max(0, (event.clientX - rect.left) / rect.width));
const targetDistance = fraction * this._maxDistance;
const index = nearestIndex(this._profile, targetDistance);
const index = nearestIndex(this._profile, this._xKey, fraction * this._maxX);
const point = this._profile[index];
this._updateCrosshair(point);
this._updateTooltip(point, event);
@@ -304,7 +313,7 @@ export class TrackProfilePanel {
};
_updateCrosshair(point) {
const x = (point.distance_meters / this._maxDistance) * VIEWBOX_WIDTH;
const x = this._fx(point) * VIEWBOX_WIDTH;
this._plots.querySelectorAll(".profile-chart").forEach((wrapper, i) => {
const series = this._activeSeries[i];
const value = point[series.key];
@@ -483,14 +492,25 @@ function formatPace(secsPerKm) {
return `${minutes}:${String(Math.round(secsPerKm % 60)).padStart(2, "0")}`;
}
function nearestIndex(profile, targetDistance) {
// Cumulative time spent moving (over MOVING_MPS), for the time axis.
function addMovingTime(profile) {
let moving = 0;
profile.forEach((p, i) => {
const prev = profile[i - 1];
const dt = prev && typeof p.elapsed_secs === "number" && typeof prev.elapsed_secs === "number" ? p.elapsed_secs - prev.elapsed_secs : 0;
if (dt > 0 && (p.distance_meters - prev.distance_meters) / dt >= MOVING_MPS) moving += dt;
p.moving_secs = moving;
});
}
function nearestIndex(profile, key, target) {
// Distances are monotonically non-decreasing (see TrackStats.build_profile),
// so a binary search would work too — linear is simpler and profiles here
// top out in the low thousands of points, not worth the complexity.
let closest = 0;
let closestDiff = Infinity;
profile.forEach((p, i) => {
const diff = Math.abs(p.distance_meters - targetDistance);
const diff = Math.abs(p[key] - target);
if (diff < closestDiff) {
closestDiff = diff;
closest = i;