import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs"; import { trailColorsAlong } from "/js/hiking_trails.js"; // Small-multiples profile chart: one row per metric the track actually has // (elevation always, speed/cadence/heart rate/power only when present — plenty of // GPX files have none of those), sharing one x-axis (distance) and one // crosshair. Elevation/speed/cadence/heart rate live on wildly different // scales, so this is deliberately several single-series charts stacked // rather than one chart with several y-axes (see dataviz skill: no // dual-axis charts). // // SVG viewBox is a fixed 1000-unit-wide coordinate space (not real pixels) // stretched to the container via CSS — mouse interaction converts // clientX through the container's current bounding rect on every move, so // this stays correct across container width without redrawing, but a // browser window resize while the panel is open won't reflow the drawn // paths until it's reopened. Acceptable for a detail panel that's normally // open briefly; add a resize listener if that turns out to matter. const VIEWBOX_WIDTH = 1000; const SERIES = [ { key: "elevation_meters", label: "Elevation", unit: "m", color: "#2a78d6", height: 110, area: true }, { key: "speed_kmh", label: "Speed", unit: "km/h", color: "#1baf7a", height: 70, area: false }, { key: "cadence_rpm", label: "Cadence", unit: "rpm", color: "#eda100", height: 70, area: false }, { key: "heart_rate_bpm", label: "Heart rate", unit: "bpm", color: "#e34948", height: 70, area: false }, { key: "power_w", label: "Power", unit: "W", color: "#7b4fd6", height: 70, area: false }, { key: "temperature_c", label: "Temperature", unit: "°C", color: "#d9730d", height: 70, area: false }, ]; 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) { this._map = map; this._profile = null; this._marker = new Marker({ color: "#7209b7" }); } mount() { this._panel = document.getElementById("profile-panel"); this._title = this._panel.querySelector(".profile-panel__title"); this._stats = this._panel.querySelector(".profile-panel__stats"); this._chartsContainer = this._panel.querySelector(".profile-panel__charts"); this._panel.querySelector(".profile-panel__close").addEventListener("click", () => this.hide()); // Docked under the map (the Design canvas' Desktop): the map gives up // the profile's height instead of being covered by it. new ResizeObserver(() => { const height = this._panel.hidden ? 0 : this._panel.offsetHeight; document.documentElement.style.setProperty("--profile-h", `${height}px`); }).observe(this._panel); } show(properties) { const profile = properties.profile; if (!profile || profile.length === 0) return; this.shown = properties; // whose profile this is (route.js hides its own) this._profile = profile.map((point) => ({ ...point, speed_kmh: typeof point.speed_mps === "number" ? point.speed_mps * 3.6 : null })); addMovingTime(this._profile); this._title.textContent = properties.name || "Track"; this._stats.textContent = statsText(properties); const temperatures = this._profile.map((p) => p.temperature_c).filter((t) => typeof t === "number"); if (temperatures.length) { const avg = temperatures.reduce((a, b) => a + b, 0) / temperatures.length; this._stats.textContent += ` · ${Math.round(avg)} °C (${Math.round(Math.min(...temperatures))}–${Math.round(Math.max(...temperatures))})`; } this._activityType = properties.activity_type || properties.vehicle_type || (properties.vehicle && (properties.vehicle.type || "car")) || ""; this._ride = /cycl|bik|ride/i.test(this._activityType); this._laps = Array.isArray(properties.laps) && properties.laps.length > 1 ? properties.laps : null; this._segmentMode = /driv|motor|car/i.test(this._activityType) ? "10000" : this._ride ? "5000" : "1000"; // Only offer the time axis when there's actually elapsed-time data — // plenty of GPX files have no