// THE TRAIL'S PLAN (the by-date chart strip under the instruments, 11 Nov): which // weights it shows at each moment of the cut, and where each one stands then. // // Before this the strip laid every STATED weight of the whole cut on one fixed // pair of axes from the first frame: a remembered weight ("≈390, as of Feb // 2023", "368 as of Jun 2008") was never a point, a 2013 weigh-in sat pinned at // the left edge of a 2013–2026 axis for minutes, and one straight line crossed // a decade of nothing to the 2025–26 points squeezed into the last pixels. // // Now the chart is a sequence of STATES, one per moment a weight is said: the // points said so far -- stated ones, and remembered ones (`remembered`) -- at // their own dates (asOf, else the day said) and values; the axes FIT what is // shown (`bounds: "fit"`; "fixed" pins them to the whole cut's points); and a // stretch of time with no statement longer than the gap threshold (`gap`) is not // drawn as data: the time axis is cut there into CLUSTERS, each with its own // years and a small break mark at the foot between them, so the dense recent run // gets the width. The page (chrome-instruments.mjs trailHtml) moves everything // from one state's places to the next's over TRAIL_REFLOW seconds. // // THE LINE (12 Nov, rev9-a9: "the chart is butchered" -- remembered weights left // as loose rings for minutes, a dotted line from 2013 straight over the 2023–25 // points it ignored): ONE line through every weight shown, stated and // remembered alike, in date order. Its `pairs` are neighbours by date among all // the shown points, so it never passes over a point; a pair with a remembered // end is `quiet` (the page draws it in the ghost colour, thinner), and a pair // across a break is `gap` (dotted). SEVERAL WEIGHTS FOR ONE DAY ("390", later // "400", both as of Feb 2023) are one point of the line -- the one said last -- // the earlier ones stay as marks beside it, so the line never zigzags up and // down one date. // // THE WIDTH (12 Nov, rev9-a9 5:30–6:00: nine weights of July–November 2026 piled into the right edge): half of it // by days, half by the weights each stretch between said days holds (trailLayout, DAY_SHARE), so a dense run is // spread and an empty year still reads as longer than a week. // // PURE: the measure slice and a box in, plain data out. import { dayNumber } from "./measure-draw.mjs"; /** Seconds a reflow takes: the axes and every point moving to the new state's places. */ export const TRAIL_REFLOW = 0.4; /** `gap: "auto"`: a stretch is a break when longer than this many days, or `GAP_MEDIANS` times the median spacing. */ export const GAP_FLOOR_DAYS = 365; export const GAP_MEDIANS = 8; /** The part of the strip's width shared out by days; the rest goes by the weights each stretch holds (trailLayout). */ export const DAY_SHARE = 0.5; const r2 = (v) => Math.round(v * 100) / 100; /** * The trail's points, in the order the cut says them: `{ n, t, row, value, qualifier, day, kind }`, `kind` * "stated" or "remembered" (only with `remembered`), `day` the day number of its asOf, else its date. */ export function trailPoints(measure, { remembered = true } = {}) { const out = []; for (const e of measure?.events ?? []) { // A row with no number is no point: `Number(null)` and `Number("")` are 0, which drew a 0 lb point (12 Nov). if (!Number.isFinite(e.t) || e.value === null || e.value === undefined || e.value === "" || typeof e.value === "boolean" || !Number.isFinite(Number(e.value))) continue; if (e.kind !== "stated" && !(remembered && e.kind === "remembered")) continue; const day = dayNumber(e.asOf ?? e.date); if (!Number.isFinite(day)) continue; out.push({ t: e.t, row: e.row ?? null, value: Number(e.value), qualifier: e.qualifier ?? null, day, kind: e.kind }); } out.sort((a, b) => a.t - b.t); return out.map((p, n) => ({ ...p, n })); } /** The gap threshold in days: `gap` a number of days, 0/false never, "auto" from the data (see GAP_FLOOR_DAYS). */ export function gapThreshold(points, gap = "auto") { if (gap === 0 || gap === false) return Infinity; if (typeof gap === "number" && gap > 0) return gap; const days = [...new Set(points.map((p) => p.day))].sort((a, b) => a - b); const steps = days.slice(1).map((d, i) => d - days[i]).sort((a, b) => a - b); const median = steps.length ? steps[Math.floor(steps.length / 2)] : 0; return Math.max(GAP_FLOOR_DAYS, GAP_MEDIANS * median); } /** Days, sorted, cut into clusters where a step is longer than `thr`: `[{ first, last }]`. */ export function clustersOf(days, thr) { const ds = [...new Set(days)].sort((a, b) => a - b); const out = []; for (const d of ds) { const c = out[out.length - 1]; if (c && d - c.last <= thr) c.last = d; else out.push({ first: d, last: d }); } return out; } const yearOf = (d) => new Date(d * 86400000).getUTCFullYear(); const jan1 = (y) => Date.UTC(y, 0, 1) / 86400000; /** * One state's places: the y bounds (`lo`/`hi`, tens of the unit around the values shown and the mark), the * clusters along x (each `{ first, last, from, to, x0, x1 }`: its days, its padded span, its pixels), the breaks * between them (`gaps`: `{ key, x }`), the year labels (`years`: `{ key, text, x, tick }`), and `X(day)` / `Y(v)`. * * `box` = `{ x0, x1, y0, y1, gapPx, yearPx }` in page pixels. * `dayShare` (default DAY_SHARE): the part of the width shared out by days -- 1 lays x by days alone (the analysis * item's date axis, analysis-plan.mjs, where a span's width says its days). */ export function trailLayout(points, box, { thr = Infinity, mark = null, dayShare = DAY_SHARE } = {}) { const { x0, x1, y0, y1 } = box; const gapPx = box.gapPx ?? 22; const yearPx = box.yearPx ?? 34; const vals = points.map((p) => p.value).concat(mark !== null && mark !== undefined ? [mark] : []); const lo = vals.length ? Math.floor((Math.min(...vals) - 5) / 10) * 10 : 0; const hi = vals.length ? Math.ceil((Math.max(...vals) + 5) / 10) * 10 : 10; const cl = clustersOf(points.map((p) => p.day), thr); const spread = cl.reduce((s, c) => s + (c.last - c.first), 0); const pad = Math.max(20, 0.04 * spread); for (const c of cl) { const p = c.last - c.first < 1 ? Math.max(pad, 60) : pad; c.from = c.first - p; c.to = c.last + p; } // THE WIDTH BY DAYS AND BY WEIGHTS (12 Nov, rev9-a9: nine weights of July–November 2026 shared 31 px of a 2023–26 // cluster, under a pixel apart). Each stretch between two neighbouring days a weight is said for (and a cluster's // padding) gets DAY_SHARE of the width by its days and the rest by the weights it holds -- each said day owning // an equal slot, half on either side of it -- so a crowded run is spread and an empty year still reads as longer. const widthDays = cl.reduce((s, c) => s + (c.to - c.from), 0) || 1; const avail = Math.max(0, x1 - x0 - gapPx * Math.max(0, cl.length - 1)); const said = new Set(points.map((p) => p.day)); const slots = said.size || 1; let at = x0; for (const c of cl) { const days = [...said].filter((d) => d >= c.first && d <= c.last).sort((a, b) => a - b); const knots = [c.from, ...days, c.to]; let x = at; c.knots = knots.map((d, i) => { if (i > 0) { const share = (i > 1 ? 0.5 : 0) + (i < knots.length - 1 ? 0.5 : 0); x += avail * (dayShare * (d - knots[i - 1]) / widthDays + (1 - dayShare) * share / slots); } return { d, x }; }); c.x0 = r2(at); c.x1 = r2(x); at = x + gapPx; } const X = (d) => { const c = cl.find((k) => d >= k.from - 1e-9 && d <= k.to + 1e-9) ?? null; if (!c) return null; const i = Math.max(1, c.knots.findIndex((k, j) => j > 0 && d <= k.d + 1e-9)); const a = c.knots[i - 1]; const b = c.knots[i] ?? a; return r2(b.d > a.d ? a.x + ((d - a.d) / (b.d - a.d)) * (b.x - a.x) : a.x); }; const Y = (v) => r2(y1 - ((v - lo) / Math.max(1, hi - lo)) * (y1 - y0)); const gaps = cl.slice(1).map((c, i) => ({ key: `${cl[i].last}_${c.first}`, x: r2((cl[i].x1 + c.x0) / 2) })); // The years: each cluster's first year labelled at its first point (unless that year's 1 January is a tick in it), // each 1 January inside a cluster's span a tick; then thinned left to right so no two labels are closer than // `yearPx`, a cluster's own year first. const cands = []; for (const c of cl) { const firstYear = yearOf(c.first); if (jan1(firstYear) < c.from) cands.push({ key: `c${firstYear}`, text: String(firstYear), x: X(c.first), tick: false, lead: true }); for (let y = yearOf(c.from) + 1; jan1(y) <= c.to; y += 1) cands.push({ key: `t${y}`, text: String(y), x: X(jan1(y)), tick: true }); } cands.sort((a, b) => a.x - b.x || (b.lead ? 1 : 0) - (a.lead ? 1 : 0)); const years = []; for (const y of cands) if (!years.length || y.x - years[years.length - 1].x >= yearPx) years.push(y); return { lo, hi, clusters: cl, gaps, years, X, Y }; } /** * The line through a state's shown points: one point per day -- of several said for one day, the one said last -- * in date order. `[{ n, day, kind, ... }]`. * * @param {ReturnType} shown */ export function trailLine(shown) { const byDay = new Map(); for (const p of [...shown].sort((a, b) => a.t - b.t || a.n - b.n)) byDay.set(p.day, p); return [...byDay.values()].sort((a, b) => a.day - b.day); } /** * The trail's states: one per moment a point is said (`at`), each with the points shown (`shown`, their `n`), its * layout, each shown point's place (`pos`: n -> {x, y}), the points the line goes through (`line`, their `n` in date * order: trailLine) and the line's pairs (`pairs`: neighbours along it, `{ key, a, b, gap, quiet }` -- `gap` when * they stand in two clusters (a dotted pair across a break), `quiet` when either end is remembered). `bounds: * "fixed"` lays every state on the layout of all the cut's points (the strip as it was, but with its breaks). * * @param {ReturnType} points */ export function trailStates(points, box, { gap = "auto", mark = null, bounds = "fit" } = {}) { const thr = gapThreshold(points, gap); const all = bounds === "fixed" ? trailLayout(points, box, { thr, mark }) : null; const times = [...new Set(points.map((p) => p.t))].sort((a, b) => a - b); return times.map((at) => { const shown = points.filter((p) => p.t <= at + 1e-9); const layout = all ?? trailLayout(shown, box, { thr, mark }); const pos = new Map(shown.map((p) => [p.n, { x: layout.X(p.day), y: layout.Y(p.value) }])); const cluster = (d) => layout.clusters.findIndex((c) => d >= c.from - 1e-9 && d <= c.to + 1e-9); const line = trailLine(shown); const pairs = line.slice(1).map((b, i) => { const a = line[i]; return { key: `${a.n}_${b.n}`, a: a.n, b: b.n, gap: cluster(a.day) !== cluster(b.day), quiet: a.kind !== "stated" || b.kind !== "stated" }; }); return { at, shown: shown.map((p) => p.n), layout, pos, line: line.map((p) => p.n), pairs, thr }; }); }