Files
Live_RC/src/lap_chart_logic.js
T

74 lines
5.0 KiB
JavaScript

// 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) => `<line x1="${left}" x2="${left + plotW}" y1="${y(index + 1)}" y2="${y(index + 1)}" stroke="currentColor" stroke-opacity="0.14"/><text x="${left - 8}" y="${y(index + 1) + 4}" text-anchor="end" fill="currentColor" fill-opacity="0.7" font-size="11">${index + 1}</text>`).join("");
const ticks = lapChartTicks(chart.laps).map((lap) => `<text x="${x(lap)}" y="${height - 8}" text-anchor="middle" fill="currentColor" fill-opacity="0.7" font-size="11">${lap}</text>`).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 `<g class="lap-chart-serie"><title>${escapeHtml(serie.name)}</title>
<polyline points="${points.map(([px, py]) => `${px.toFixed(1)},${py.toFixed(1)}`).join(" ")}" fill="none" stroke="${serie.color}" stroke-width="2.4" stroke-linejoin="round" stroke-linecap="round"/>
${dots ? points.map(([px, py]) => `<circle cx="${px.toFixed(1)}" cy="${py.toFixed(1)}" r="2.6" fill="${serie.color}"/>`).join("") : ""}
<line x1="${lastX.toFixed(1)}" y1="${lastY.toFixed(1)}" x2="${left + plotW + 10}" y2="${labelY}" stroke="${serie.color}" stroke-width="1.2" stroke-dasharray="2 3"/>
<text x="${left + plotW + 14}" y="${labelY + 4}" fill="${serie.color}" font-size="12" font-weight="700">${index + 1}. ${escapeHtml(serie.name.length > 18 ? `${serie.name.slice(0, 17)}…` : serie.name)}</text></g>`;
}).join("");
return `<svg class="lap-chart" viewBox="0 0 ${width} ${height}" role="img" aria-label="${escapeHtml(title)}" xmlns="http://www.w3.org/2000/svg">
${grid}${ticks}<text x="${left + plotW}" y="${height - 20}" text-anchor="end" fill="currentColor" fill-opacity="0.6" font-size="10" font-style="italic">${escapeHtml(lapLabel)}</text>${lines}</svg>`;
}