// THE FINALE'S DETAILED DIET RUNS (module `runs-finale`, `deck.finale.dietRuns`): // the whole record of his diets drawn large in the finale -- the measure // slice's `record` (diet-record.mjs), every run from the earliest row a bar in // its diet's ink along a time axis, a stretch of years with nothing on record // drawn as a break (compressed, marked), the rows that stopped a diet or named // the medication as marks under the bars, his stated weights as a light line // over them; the longest runs (and the one still going) labelled with the // diet's word, its number when the cut showed it ("DIET #6", the shelf's), its // dates and its days -- the open one "N days and counting" to the in-record // date. It stands in the finale's timeline panel under the large rail (clear // of the side video, the lower column's instruments and the promise clock that // takes it over), sweeps in left to right over the finale's first seconds, // then holds. // // PURE: a schedule and a render block in, `{ html, items }` out. import { formatDeckDate } from "./attribution.mjs"; import { finaleLayout } from "./chrome-finale.mjs"; import { rgba } from "./chrome-deck.mjs"; import { compileCues, cuePageHtml, esc, r4 } from "./chrome-cues.mjs"; import { dietRunsOn, dietRunsWeights, FINALE_MOTION, resolveFinale } from "./finale.mjs"; import { daysBetween, measureOf, shortDate } from "./measure-extras.mjs"; import { DIET_COLORS, DIET_LABELS, MEASURE_RED, resolveMeasureExtra } from "./modules/measure-extras-settings.mjs"; /** A stretch with nothing on record longer than this is drawn as a break, days. */ export const RUNS_BREAK_DAYS = 730; /** The motion: in after the finale's grow, swept left to right, s. */ export const RUNS_MOTION = Object.freeze({ delay: 0.2, sweep: 2.6, label: 0.3 }); /** The panel's least height (1080p px) under the rail; less, and there is no room. */ export const RUNS_MIN_HEIGHT = 100; const DESIGN = Object.freeze({ top: 5, weights: 34, barY: 36, barH: 18, rowA: 68, rowB: 95, rowH: 27, years: 12, brk: 44 }); /** With the weight layer (`dietRuns.weights`): every weight he gave over the bars, on the dial's scale. */ const LAYERED = Object.freeze({ ...DESIGN, top: 6, weights: 110, barY: 118, rowA: 152, rowB: 180 }); /** The record the slice carries, when the finale asks for it. */ export const recordOf = (schedule) => { const r = measureOf(schedule)?.record; return r && Array.isArray(r.runs) && r.runs.length ? r : null; }; /** * Where it stands: the finale's timeline panel under the large rail's track, * from the rail's left end to its right, down to the panel's foot -- or null * when that is under RUNS_MIN_HEIGHT. */ export function runsFinaleGeometry(schedule, render) { if (!schedule?.finale) return null; const L = finaleLayout(schedule, render); const kh = L.geo.H / 1080; const y = Math.round(L.rail.track.y + 26 * kh); const bottom = Math.round(L.rail.bottom - 4 * kh); if (bottom - y < RUNS_MIN_HEIGHT * kh) return null; const x = L.rail.track.x0; const even = (v) => Math.round(v / 2) * 2; return { x, y, width: even(L.rail.track.x1 - x), height: even(bottom - y) }; } /** The in-record date at the finale: the latest date of anything the cut showed. */ function recordDate(m) { const ds = (m.events ?? []).map((e) => e.date).filter(Boolean).sort(); return ds[ds.length - 1] ?? m.asOfRender ?? null; } /** * The time axis: dates to px, every stretch with nothing on record longer than * RUNS_BREAK_DAYS squeezed to `brk` px and marked. `x(date)`, `breaks: [{ x, * from, to }]`, the years' ticks. */ export function runsAxis(dates, width, { brk = DESIGN.brk, pad = 8 } = {}) { const ds = [...new Set(dates.filter(Boolean))].sort(); const spans = []; for (let i = 1; i < ds.length; i += 1) spans.push({ a: ds[i - 1], b: ds[i], days: Math.max(0, daysBetween(ds[i - 1], ds[i]) ?? 0) }); const broken = spans.filter((s) => s.days > RUNS_BREAK_DAYS); const linear = spans.filter((s) => s.days <= RUNS_BREAK_DAYS).reduce((n, s) => n + s.days, 0); const k = Math.max(0.01, (width - 2 * pad - broken.length * brk) / Math.max(1, linear)); const stops = [{ date: ds[0], x: pad }]; for (const s of spans) stops.push({ date: s.b, x: stops[stops.length - 1].x + (s.days > RUNS_BREAK_DAYS ? brk : s.days * k), broken: s.days > RUNS_BREAK_DAYS }); const x = (d) => { if (!d) return stops[stops.length - 1].x; if (d <= stops[0].date) return stops[0].x; for (let i = 1; i < stops.length; i += 1) { if (d <= stops[i].date) { const a = stops[i - 1], b = stops[i]; if (b.broken) return a.x + (d === b.date ? b.x - a.x : Math.min(b.x - a.x, 6)); return a.x + (daysBetween(a.date, d) ?? 0) * k; } } return stops[stops.length - 1].x + (daysBetween(stops[stops.length - 1].date, d) ?? 0) * k; }; const breaks = stops.map((s, i) => (s.broken ? { x: r4((stops[i - 1].x + s.x) / 2), x0: stops[i - 1].x, x1: s.x, from: stops[i - 1].date, to: s.date } : null)).filter(Boolean); const years = []; const y0 = Number(String(ds[0]).slice(0, 4)), y1 = Number(String(ds[ds.length - 1]).slice(0, 4)); for (let y = y0 + 1; y <= y1; y += 1) { const d = `${y}-01-01`; if (breaks.some((b) => d > b.from && d < b.to)) continue; years.push({ year: y, x: r4(x(d)) }); } return { x, breaks, years, k }; } /** The finale's detailed diet runs page over the finale's `window`: `{ html, items }`. */ export function runsFinaleHtml(schedule, render, window, opts = {}) { const m = measureOf(schedule); const rec = recordOf(schedule); const geo = runsFinaleGeometry(schedule, render); if (!m || !rec || !geo) throw new Error("runs-finale: needs a finale, the measure slice's diet record (render.chrome.deck.finale.dietRuns) and room under the rail"); const pal = render.palette; // The weight layer (`dietRuns.weights`): every weight he gave, at the day it applied (or was said). const layer = dietRunsWeights(resolveFinale(render).dietRuns); const D = layer ? LAYERED : DESIGN; const s = (render.width ?? 1920) / 1920; const px = (v) => Math.max(1, Math.round(v * s)); const cfg = resolveMeasureExtra("diet", render) ?? resolveMeasureExtra("diet", { chrome: { deck: {} } }); const colors = { ...DIET_COLORS, ...(cfg?.colors ?? {}) }; const labels = { ...DIET_LABELS, ...(cfg?.labels ?? {}) }; const W = geo.width, H = geo.height; const today = recordDate(m); const points = Array.isArray(rec.points) ? rec.points : []; const claims = layer && Array.isArray(rec.claims) ? rec.claims : []; const dates = [rec.from, ...rec.runs.flatMap((r) => [r.since, r.end]), ...rec.marks.map((x) => x.date), ...rec.weights.map((w) => w.date), ...points.map((p) => p.date), ...claims.flatMap((c) => [c.applied, c.said]), today].filter(Boolean); const A = runsAxis(dates, W, { brk: px(D.brk), pad: px(layer ? 34 : 8) }); // The number the cut gave a diet (the shelf's "DIET #6"): its state's, by regime and since. const shownN = new Map(); for (const st of m.state ?? []) if (st?.diet?.since) shownN.set(`${st.diet.regime}|${st.diet.since}`, st.diet.n); const crossesBreak = (r) => A.breaks.some((b) => r.since <= b.from && (r.end ?? today) >= b.to); const runs = rec.runs.map((r, i) => { const end = r.end ?? today; const x0 = A.x(r.since); // A run across a break has no record of its end: drawn to the break, fading into it. const brk = crossesBreak(r) ? A.breaks.find((b) => r.since <= b.from) : null; const x1 = brk ? brk.x0 + px(4) : A.x(end); const days = brk ? null : Math.max(0, daysBetween(r.since, end) ?? 0); const word = (r.name ?? labels[r.key] ?? r.key).toUpperCase(); const n = shownN.get(`${r.key}|${r.since}`) ?? null; return { ...r, i, x0: r4(x0), x1: r4(Math.max(x0 + px(3), x1)), days, word, n, open: r.end === null, faded: !!brk, color: colors[r.key] ?? colors.other }; }); // THE LABELS: the run still going first, then the longest; each in the first of two rows under the bars where // it meets no other (its words ~0.56 em a character), at its bar's middle, clamped to the panel. const fs = px(13); const est = (t, size, bold = 0.58) => t.length * size * bold; const rows = [[], []]; const placed = []; const order = [...runs].sort((a, b) => (b.open - a.open) || ((b.days ?? 1e6) - (a.days ?? 1e6))); for (const r of order) { const l1 = `${r.word}${r.n !== null ? ` · DIET #${r.n}` : ""}`; const from = formatDeckDate(r.since); const l2 = r.days === null ? `from ${from}` : r.open ? `${shortDate(r.since, { year: true })} · ${r.days} days and counting` : `${shortDate(r.since, { year: true })} – ${shortDate(r.end, { year: true })} · ${r.days} day${r.days === 1 ? "" : "s"}`; const w = Math.ceil(Math.max(est(l1, fs, 0.62), est(l2, px(12), 0.55))) + px(6); if ((r.x1 - r.x0) < px(14) && !r.open) continue; const mid = (r.x0 + r.x1) / 2; const lx = Math.max(0, Math.min(W - w, Math.round(r.open ? r.x1 - w : mid - w / 2))); const row = rows.findIndex((list) => list.every((o) => lx + w + px(10) <= o.x || o.x + o.w + px(10) <= lx)); if (row < 0) continue; rows[row].push({ x: lx, w }); placed.push({ r, row, x: lx, w, l1, l2 }); } // THE WEIGHTS. Without the layer: a light line of his stated weights over the bars. With it (`weights`): // EVERY weight he gave on the dial's scale -- at the day it applied ("applied") or was said ("said") -- the ones // he gave at the time solid and joined (his claimed trajectory; a bound is no reading, so not joined), the ones told later hollow with a thin leader // down to the day he said it, so a retold peak piles up at one period from its tellings; a bound (">") a caret // over its point, an "about" (≈) a ring round it, a range a bar. const ws = rec.weights.filter((w) => w.date); const scale = m.scale ?? {}; const vals = layer ? [...claims.map((c) => c.value), ...points.map((p) => p.value)] : [...ws.map((w) => w.value), ...points.map((p) => p.value)]; const lo = layer ? Math.min(Number.isFinite(scale.min) ? scale.min : Infinity, ...vals) : Math.min(...vals); const hi = layer ? Math.max(Number.isFinite(scale.max) ? scale.max : -Infinity, ...vals) : Math.max(...vals); const wy = (v) => r4(px(D.top) + (hi > lo ? (hi - v) / (hi - lo) : 0.5) * px(D.weights - D.top - 4)); const xOfClaim = (c) => r4(A.x(layer === "said" ? c.said : c.applied)); const line = layer ? claims.filter((c) => !c.retold && !["<", ">"].includes(c.qualifier)).map((c) => [xOfClaim(c), wy(c.value)]) : ws.map((w) => [r4(A.x(w.date)), wy(w.value)]); const pts = line; const segs = []; for (let i = 1; i < pts.length; i += 1) { const broken = A.breaks.some((b) => pts[i - 1][0] <= b.x0 + 1 && pts[i][0] >= b.x1 - 1); if (!broken) segs.push(`M${pts[i - 1][0]},${pts[i - 1][1]}L${pts[i][0]},${pts[i][1]}`); } const axisY = px(D.barY + D.barH + 1); const grid = layer ? [250, 300, 350, 400].filter((v) => v >= lo && v <= hi).map((v) => `