// Lap chart: each driver's place after every lap of a heat, from the passings already stored. // Place after lap n = order in which the drivers who did n laps crossed the line for the n-th time. // Pure logic + an SVG string (screen and print); the PDF draws the same data in server/pdf_render.js. export const LAP_CHART_COLORS = ["#e63946", "#1d7fd6", "#2a9d3f", "#f08c00", "#8e44ad", "#00a6a6", "#d6336c", "#6c7a89", "#b5651d", "#5c940d", "#3b5bdb", "#c2255c"]; export const LAP_CHART_MAX_DRIVERS = 24; export const LAP_CHART_MAX_LAPS = 400; // entries: [{ name, timestamps: [ms, ...] }] in official finish order (one per counted lap). // Returns { laps, series: [{ name, color, positions: [place or null per lap] }] } or null when // there is nothing to draw (fewer than 2 drivers with laps, or fewer than 2 laps). export function buildLapChart(entries = []) { const drivers = entries .slice(0, LAP_CHART_MAX_DRIVERS) .map((entry) => ({ name: String(entry?.name || "-"), times: (entry?.timestamps || []).map(Number).filter(Number.isFinite).sort((a, b) => a - b).slice(0, LAP_CHART_MAX_LAPS) })); const laps = Math.max(0, ...drivers.map((driver) => driver.times.length)); if (laps < 2 || drivers.filter((driver) => driver.times.length).length < 2) return null; const positions = drivers.map(() => []); for (let lap = 0; lap < laps; lap += 1) { drivers .map((driver, index) => ({ index, at: driver.times[lap] })) .filter((item) => item.at !== undefined) .sort((a, b) => a.at - b.at || a.index - b.index) .forEach((item, place) => { positions[item.index][lap] = place + 1; }); drivers.forEach((driver, index) => { if (positions[index][lap] === undefined) positions[index][lap] = null; }); } return { laps, series: drivers.map((driver, index) => ({ name: driver.name, color: LAP_CHART_COLORS[index % LAP_CHART_COLORS.length], positions: positions[index] })), }; } // Which lap numbers get a tick: about 10 along the axis, always the last. export function lapChartTicks(laps) { const step = laps <= 12 ? 1 : laps <= 30 ? 2 : laps <= 60 ? 5 : laps <= 150 ? 10 : 25; const ticks = []; for (let lap = step; lap < laps; lap += step) ticks.push(lap); if (laps - (ticks[ticks.length - 1] || 0) < step / 2 && ticks.length) ticks.pop(); ticks.push(laps); return laps > 1 ? [1, ...ticks.filter((lap) => lap > 1)] : ticks; } // The chart as an inline SVG: place 1 on top, a line per driver, names on the right at their // official finish place (with a dotted lead-in from where their last lap ended). export function renderLapChartSvg(chart, { escapeHtml, title = "", lapLabel = "Lap" } = {}) { if (!chart) return ""; const rows = chart.series.length; const rowH = 22; const left = 28; const right = 150; const top = 12; const width = 720; const plotW = width - left - right; const height = top + rows * rowH + 30; const x = (lap) => left + (chart.laps === 1 ? 0 : ((lap - 1) / (chart.laps - 1)) * plotW); const y = (place) => top + (place - 0.5) * rowH; const grid = Array.from({ length: rows }, (_, index) => `${index + 1}`).join(""); const ticks = lapChartTicks(chart.laps).map((lap) => `${lap}`).join(""); const dots = chart.laps <= 40; const lines = chart.series.map((serie, index) => { const points = serie.positions.map((place, lap) => (place ? [x(lap + 1), y(place)] : null)).filter(Boolean); if (!points.length) return ""; const [lastX, lastY] = points[points.length - 1]; const labelY = y(index + 1); return `${escapeHtml(serie.name)} ${dots ? points.map(([px, py]) => ``).join("") : ""} ${index + 1}. ${escapeHtml(serie.name.length > 18 ? `${serie.name.slice(0, 17)}…` : serie.name)}`; }).join(""); return ` ${grid}${ticks}${escapeHtml(lapLabel)}${lines}`; }