// Charts for the CV-ICRL explainer. Plain SVG, no libraries, no outside requests. // All values come from data.json (built and checked by scripts/build_explainer.py and // scripts/check_explainer.py). Values the page prints are stored ready to print: AER and token levels as // strings, the rest in thousandths. Nothing here rounds a result. "use strict"; const SVGNS = "http://www.w3.org/2000/svg"; const ARMS = ["normal", "zero", "frozen", "frozen_final"]; const ARM_TEXT = { normal: "note written normally", zero: "note always 0", frozen: "note frozen at the first guess", frozen_final: "note held high from the start", }; const state = { task: "sc", arm: "normal", stream: 0 }; const LABEL = { sc: "SimpleCrossing", bup: "BlockedUnlockPickup" }; let DATA = null; // ---------- small helpers ---------- function sv(tag, attrs, parent) { const e = document.createElementNS(SVGNS, tag); for (const k in attrs) e.setAttribute(k, attrs[k]); if (parent) parent.appendChild(e); return e; } function ht(tag, attrs, text, parent) { const e = document.createElement(tag); if (attrs) for (const k in attrs) e.setAttribute(k, attrs[k]); if (text !== undefined && text !== null) e.textContent = text; if (parent) parent.appendChild(e); return e; } const milli = (v) => (v / 1000).toFixed(3); const int = (n) => n.toLocaleString("en-US"); const tickText = (v) => (Number.isInteger(v) ? int(v) : String(v)); function tooltipFor(card) { let tip = card.querySelector(".tooltip"); if (!tip) tip = ht("div", { class: "tooltip", role: "status" }, null, card); return tip; } // rows: [{key: "line"|"dot"|"rect"|null, color, value, label}] function showTip(card, tip, px, py, title, rows) { tip.replaceChildren(); ht("div", { class: "t" }, title, tip); for (const r of rows) { const row = ht("div", { class: "row" }, null, tip); if (r.key) { const k = ht("i", { class: r.key === "line" ? "key-line" : r.key === "dot" ? "key-dot" : "key-rect" }, null, row); if (r.key === "line") k.style.borderColor = r.color; else k.style.background = r.color; if (r.dash) k.classList.add("ref"); } ht("strong", null, r.value, row); ht("span", null, r.label, row); } tip.style.display = "block"; const cw = card.clientWidth, tw = tip.offsetWidth; let left = px + 14; if (left + tw > cw - 4) left = Math.max(4, px - tw - 14); tip.style.left = left + "px"; tip.style.top = Math.max(4, py - 10) + "px"; } function hideTip(tip) { tip.style.display = "none"; } // A plot frame: grid, ticks, axis labels. Returns scales and the svg. function frame(plot, o) { plot.replaceChildren(); const W = Math.max(260, Math.floor(plot.clientWidth)); const m = { l: o.ml || 42, r: o.mr || 14, t: o.mt || 10, b: o.mb || 38 }; const H = o.height; const svg = sv("svg", { width: W, height: H, viewBox: `0 0 ${W} ${H}`, role: "img", "aria-label": o.aria || "" }, plot); const x = (v) => m.l + ((v - o.x0) / (o.x1 - o.x0)) * (W - m.l - m.r); const y = (v) => m.t + (1 - (v - o.y0) / (o.y1 - o.y0)) * (H - m.t - m.b); for (const t of o.yticks || []) { sv("line", { x1: m.l, x2: W - m.r, y1: y(t), y2: y(t), class: t === 0 ? "axis" : "grid" }, svg); sv("text", { x: m.l - 6, y: y(t) + 4, "text-anchor": "end", class: "tick" }, svg).textContent = tickText(t); } for (const t of o.xticks || []) { if (o.xgrid) sv("line", { x1: x(t), x2: x(t), y1: m.t, y2: H - m.b, class: t === 0 ? "axis" : "grid" }, svg); sv("text", { x: x(t), y: H - m.b + 15, "text-anchor": "middle", class: "tick" }, svg).textContent = tickText(t); } if (o.xlabel) sv("text", { x: (m.l + W - m.r) / 2, y: H - 6, "text-anchor": "middle", class: "alabel" }, svg).textContent = o.xlabel; if (o.ylabel) sv("text", { x: 12, y: (m.t + H - m.b) / 2, "text-anchor": "middle", class: "alabel", transform: `rotate(-90 12 ${(m.t + H - m.b) / 2})` }, svg).textContent = o.ylabel; return { svg, x, y, W, H, m }; } function stepTicks(xmax, W) { const step = xmax <= 2000 ? 500 : W < 420 ? 4000 : 2000; const out = []; for (let t = 0; t <= xmax; t += step) out.push(t); return out; } // note value at a step, from the change points [[step, milli], ...] function tokAt(tok, step) { let v = tok[0][1]; for (const [s, val] of tok) { if (s > step) break; v = val; } return v; } // Charts that share the move axis (the note and the episode scores of the selected run): a position shown in // one of them is shown in the others too. Reset whenever those charts are drawn again. let LINKED = []; // keyboard + pointer crosshair over a frame. pick(px) -> index; draw(i) -> {x, title, rows, y?} // link: {stepOf(i), indexOf(step)} joins the chart to LINKED, so the same move shows in every linked chart. function crosshair(card, f, n, pick, draw, link) { const tip = tooltipFor(card); const line = sv("line", { class: "cross", y1: f.m.t, y2: f.H - f.m.b, visibility: "hidden" }, f.svg); const overlay = sv("rect", { x: f.m.l, y: 0, width: f.W - f.m.l - f.m.r, height: f.H, fill: "transparent" }, f.svg); let cur = 0; const plotBox = () => f.svg.getBoundingClientRect(); const ctl = link ? { showStep: (s) => show(link.indexOf(s), true), hide: () => hide(true) } : null; if (ctl) LINKED.push(ctl); function show(i, passive) { cur = Math.max(0, Math.min(n - 1, i)); const d = draw(cur); line.setAttribute("x1", d.x); line.setAttribute("x2", d.x); line.setAttribute("visibility", "visible"); const pb = plotBox(), cb = card.getBoundingClientRect(); showTip(card, tip, pb.left - cb.left + d.x, pb.top - cb.top + (d.y !== undefined ? d.y : f.m.t + 10), d.title, d.rows); if (ctl && !passive) for (const o of LINKED) if (o !== ctl) o.showStep(link.stepOf(cur)); } function hide(passive) { line.setAttribute("visibility", "hidden"); hideTip(tip); if (draw.clear) draw.clear(); if (ctl && passive !== true) for (const o of LINKED) if (o !== ctl) o.hide(); } const fromPointer = (e) => { const pb = plotBox(); show(pick(e.clientX - pb.left)); }; overlay.addEventListener("pointerdown", fromPointer); overlay.addEventListener("pointermove", fromPointer); overlay.addEventListener("pointerleave", () => hide()); overlay.addEventListener("pointercancel", () => hide()); f.svg.setAttribute("tabindex", "0"); f.svg.addEventListener("focus", () => show(cur)); f.svg.addEventListener("blur", () => hide()); f.svg.addEventListener("keydown", (e) => { const big = Math.max(1, Math.round(n / 20)); if (e.key === "ArrowRight") { show(cur + (e.shiftKey ? big : 1)); e.preventDefault(); } if (e.key === "ArrowLeft") { show(cur - (e.shiftKey ? big : 1)); e.preventDefault(); } if (e.key === "Escape") hide(); }); } // the note as a step line (it changes only at its change points) function notePath(f, tok, xmax) { let d = ""; tok.forEach(([s, v], j) => { if (s > xmax) return; d += j === 0 ? `M${f.x(s).toFixed(1)},${f.y(v / 1000).toFixed(1)}` : `H${f.x(s).toFixed(1)}V${f.y(v / 1000).toFixed(1)}`; }); return d + `H${f.x(xmax).toFixed(1)}`; } function table(container, head, rows) { container.replaceChildren(); const t = ht("table", null, null, container); const tr = ht("tr", null, null, ht("thead", null, null, t)); for (const h of head) ht("th", { scope: "col" }, h, tr); const tb = ht("tbody", null, null, t); for (const r of rows) { const row = ht("tr", null, null, tb); r.forEach((c, j) => ht(j === 0 ? "th" : "td", j === 0 ? { scope: "row" } : null, c, row)); } } // ---------- chart 1: the note over the run ---------- function tokenChart(card, task, arm, i, opts) { const T = DATA.tasks[task], A = T.arms[arm], S = A.streams[i]; const R = opts.ref && arm !== "normal" ? T.arms.normal.streams[i] : null; // the same run with the normal note const ds = DATA.ds, xmax = opts.xmax || DATA.n_steps, kmax = Math.floor((xmax - 1) / ds); const plot = card.querySelector(".plot"); const W0 = plot.clientWidth; const f = frame(plot, { height: opts.height || 230, x0: 0, x1: xmax, y0: -0.1, y1: 1.12, yticks: [0, 0.5, 1], xticks: stepTicks(xmax, W0), xlabel: "move of the run", ylabel: "note", aria: `${LABEL[task]}, ${arm}: the note over ${int(xmax)} moves` }); const [lo, hi] = T.train_range; sv("rect", { x: f.x(0), width: f.x(xmax) - f.x(0), y: f.y(hi / 1000), height: f.y(lo / 1000) - f.y(hi / 1000), class: "band" }, f.svg); const pts = (arr, k1) => arr.slice(0, k1 + 1).map((v, k) => `${f.x(k * ds).toFixed(1)},${f.y(v / 1000).toFixed(1)}`).join(" "); sv("polyline", { points: pts(S.raw, kmax), class: "ln orange" }, f.svg); if (opts.mean) sv("polyline", { points: pts(A.tok_mean, kmax), class: "ln gray" }, f.svg); if (R) sv("path", { d: notePath(f, R.tok, xmax), class: "ln ref" }, f.svg); sv("path", { d: notePath(f, S.tok, xmax), class: "ln blue" }, f.svg); crosshair(card, f, kmax + 1, (px) => Math.round(((px - f.m.l) / (f.W - f.m.l - f.m.r)) * xmax / ds), (k) => { const step = k * ds; const rows = [{ key: "line", color: "var(--blue)", value: milli(tokAt(S.tok, step)), label: "this run's note" }, { key: "line", color: "var(--orange)", value: milli(S.raw[k]), label: "critic's guess" }]; if (opts.mean) rows.push({ key: "line", color: "var(--gray)", value: milli(A.tok_mean[k]), label: "average note, 20 runs" }); if (R) rows.push({ key: "line", dash: true, color: "var(--blue)", value: milli(tokAt(R.tok, step)), label: "same run, normal note" }); return { x: f.x(step), title: `move ${int(step)}`, rows }; }, opts.link ? { stepOf: (k) => k * ds, indexOf: (s) => Math.round(s / ds) } : null); return f; } // ---------- chart 2: episode scores over the run ---------- function episodeChart(card, task, arm, i) { const T = DATA.tasks[task], A = T.arms[arm], S = A.streams[i], W = DATA.window, n = DATA.n_steps, ds = DATA.ds; const R = arm !== "normal" ? T.arms.normal.streams[i] : null; // the same run with the normal note const plot = card.querySelector(".plot"); const f = frame(plot, { height: 230, x0: 0, x1: n, y0: 0, y1: 1.05, yticks: [0, 0.5, 1], xticks: stepTicks(n, plot.clientWidth), xlabel: "move of the run (at the end of each episode)", ylabel: "score", aria: `${LABEL[task]}, ${arm}: the score of each of the ${S.ep.length} finished episodes of run ${i + 1}` }); // 20-run average per window, drawn as a step line across each window let d = ""; A.win_ret.forEach((v, w) => { d += `${w === 0 ? "M" : "L"}${f.x(w * W).toFixed(1)},${f.y(v / 1000).toFixed(1)}H${f.x((w + 1) * W).toFixed(1)}`; }); const dense = S.ep.length > 150, r = dense ? 2.5 : 4; for (const [e, v] of S.ep) { sv("circle", { cx: f.x(e).toFixed(1), cy: f.y(v / 1000).toFixed(1), r, class: dense ? "dot dense" : "dot" }, f.svg); } sv("path", { d, class: "ln gray" }, f.svg); if (R) { let dr = ""; R.win.forEach((v, w) => { dr += `${w === 0 ? "M" : "L"}${f.x(w * W).toFixed(1)},${f.y(v / 1000).toFixed(1)}H${f.x((w + 1) * W).toFixed(1)}`; }); sv("path", { d: dr, class: "ln ref" }, f.svg); } const ring = sv("circle", { r: r + 3, class: "hl", visibility: "hidden" }, f.svg); const ends = S.ep.map((p) => p[0]); function nearest(step) { let best = 0; for (let j = 0; j < ends.length; j++) if (Math.abs(ends[j] - step) < Math.abs(ends[best] - step)) best = j; return best; } // the crosshair sits at the move (the same position as in the note chart); the ring marks the nearest episode end const draw = (k) => { const step = Math.min(n - 1, k * ds), w = Math.floor(step / W), j = nearest(step); const [e, v] = S.ep[j]; ring.setAttribute("cx", f.x(e)); ring.setAttribute("cy", f.y(v / 1000)); ring.setAttribute("visibility", "visible"); const inWin = S.ep.filter((p) => Math.floor(p[0] / W) === w).length; const rows = [ { key: "dot", color: "var(--blue)", value: milli(v), label: `nearest episode, ended at move ${int(e)}` }, { key: "dot", color: "var(--blue)", value: milli(S.win[w]), label: `this run, moves ${int(w * W)}–${int((w + 1) * W - 1)} (${inWin} episodes)` }, { key: "line", color: "var(--gray)", value: milli(A.win_ret[w]), label: "same moves, average of 20 runs" }]; if (R) rows.push({ key: "line", dash: true, color: "var(--blue)", value: milli(R.win[w]), label: "same run and moves, normal note" }); return { x: f.x(step), y: f.y(v / 1000), title: `move ${int(step)}`, rows }; }; draw.clear = () => ring.setAttribute("visibility", "hidden"); const npos = Math.floor((n - 1) / ds) + 1; crosshair(card, f, npos, (px) => Math.round(((px - f.m.l) / (f.W - f.m.l - f.m.r)) * n / ds), draw, { stepOf: (k) => k * ds, indexOf: (s) => Math.round(s / ds) }); } // ---------- chart 3: paper vs ours ---------- function barsChart(card) { const plot = card.querySelector(".plot"); const rowsPerTask = ["normal", "zero", "frozen", "frozen_final"], bh = 12, gap = 2, rowH = 2 * bh + gap + 14, head = 26; const H = 2 * (head + rowsPerTask.length * rowH) + 44; const f = frame(plot, { height: H, x0: 0, x1: 1, y0: 0, y1: 1, xticks: [0, 0.25, 0.5, 0.75, 1], xgrid: true, ml: 70, mr: 46, mt: 4, mb: 38, xlabel: "average score (mean of 20 runs)", aria: "Average score per task and way of writing the note: the paper's Table 11 next to our reproduction" }); const tip = tooltipFor(card); let y0 = f.m.t; for (const task of ["sc", "bup"]) { const T = DATA.tasks[task]; sv("text", { x: 0, y: y0 + 16, class: "rlabel", "font-weight": "600" }, f.svg).textContent = LABEL[task]; y0 += head; for (const arm of rowsPerTask) { // frozen_final is our extra test: no paper bar, a note in its place const A = T.arms[arm], paper = T.paper[arm] ? T.paper[arm].split(" ± ") : null, ours = parseFloat(A.aer_mean); sv("text", { x: f.m.l - 8, y: y0 + bh + 5, "text-anchor": "end", class: "rlabel" }, f.svg).textContent = arm; const bar = (yy, v, cls) => { const w = Math.max(1, f.x(v) - f.x(0)), rr = Math.min(4, w / 2); sv("path", { d: `M${f.x(0)},${yy}H${f.x(0) + w - rr}a${rr},${rr} 0 0 1 ${rr},${rr}V${yy + bh - rr}a${rr},${rr} 0 0 1 ${-rr},${rr}H${f.x(0)}Z`, class: cls }, f.svg); }; if (paper) { bar(y0, parseFloat(paper[0]), "bar-paper"); sv("text", { x: f.x(parseFloat(paper[0])) + 5, y: y0 + bh - 2, class: "vlabel" }, f.svg).textContent = paper[0]; } else { sv("text", { x: f.x(0) + 2, y: y0 + bh - 2, class: "alabel" }, f.svg).textContent = "not in the paper (our extra test)"; } bar(y0 + bh + gap, ours, "bar-ours"); sv("text", { x: f.x(ours) + 5, y: y0 + 2 * bh + gap - 2, class: "vlabel" }, f.svg).textContent = A.aer_mean; const hit = sv("rect", { x: 0, y: y0 - 4, width: f.W, height: 2 * bh + gap + 8, fill: "transparent", tabindex: "0" }, f.svg); const show = () => { const pb = f.svg.getBoundingClientRect(), cb = card.getBoundingClientRect(); showTip(card, tip, pb.left - cb.left + f.x(Math.max(ours, paper ? parseFloat(paper[0]) : 0)), pb.top - cb.top + parseFloat(hit.getAttribute("y")), `${LABEL[task]}, ${arm}`, [ paper ? { key: "rect", color: "var(--paper)", value: T.paper[arm], label: "paper (mean ± SD)" } : { key: null, value: "–", label: "not in the paper (our extra test)" }, { key: "rect", color: "var(--blue)", value: `${A.aer_mean} ± ${A.aer_sd}`, label: "ours (mean ± SD)" }]); }; hit.addEventListener("pointerenter", show); hit.addEventListener("focus", show); hit.addEventListener("pointerleave", () => hideTip(tip)); hit.addEventListener("blur", () => hideTip(tip)); y0 += rowH; } } const rows = []; for (const task of ["sc", "bup"]) for (const arm of rowsPerTask) { const T = DATA.tasks[task], A = T.arms[arm]; rows.push([`${LABEL[task]}, ${arm}`, T.paper[arm] || "not in the paper", `${A.aer_mean} ± ${A.aer_sd}`]); } table(document.getElementById("t-bars"), ["Task, note", "Paper (mean ± SD)", "Ours (mean ± SD)"], rows); } // ---------- chart 4: every run's score, by arm ---------- function stripChart(card) { const plot = card.querySelector(".plot"); const order = ["zero", "frozen", "normal", "frozen_final"], rowH = 62, head = 26; const H = 2 * (head + order.length * rowH) + 40; const f = frame(plot, { height: H, x0: 0, x1: 1, y0: 0, y1: 1, xticks: [0, 0.25, 0.5, 0.75, 1], xgrid: true, ml: 10, mr: 14, mt: 4, mb: 38, xlabel: "average score of one run", aria: "Every run's average score for each way of writing the note, both tasks" }); const tip = tooltipFor(card); let y0 = f.m.t; for (const task of ["sc", "bup"]) { const T = DATA.tasks[task], F = T.facts; sv("text", { x: 0, y: y0 + 16, class: "rlabel", "font-weight": "600" }, f.svg).textContent = LABEL[task]; y0 += head; const desc = { zero: "zero · note always 0", frozen: `frozen · note stuck at the first guess (${F.m0} on average)`, normal: `normal · note rises from ${F.m0} to ${F.normal_final_token} (averages)`, frozen_final: `frozen_final · note held at ${F.ff_token} (average) from the start`, }; for (const arm of order) { const A = T.arms[arm], cy = y0 + 44; sv("text", { x: f.m.l, y: y0 + 12, class: "alabel" }, f.svg).textContent = desc[arm]; const xs = A.aer.map((s) => parseFloat(s)); xs.forEach((v, j) => sv("circle", { cx: f.x(v).toFixed(1), cy: cy + ((j % 5) - 2) * 2, r: 4, class: "dot" }, f.svg)); const mean = parseFloat(A.aer_mean); sv("line", { x1: f.x(mean), x2: f.x(mean), y1: cy - 11, y2: cy + 9, class: "mean-tick" }, f.svg); const right = f.x(mean) > f.W - 90; sv("text", { x: f.x(mean) + (right ? -5 : 5), y: cy - 13, "text-anchor": right ? "end" : "start", class: "vlabel" }, f.svg) .textContent = `mean ${A.aer_mean}`; const ring = sv("circle", { r: 7, class: "hl", visibility: "hidden" }, f.svg); const hit = sv("rect", { x: f.m.l, y: y0 + 18, width: f.W - f.m.l - f.m.r, height: rowH - 18, fill: "transparent", tabindex: "0" }, f.svg); let cur = 0; const show = (j) => { cur = (j + 20) % 20; const cx = f.x(xs[cur]), yy = cy + ((cur % 5) - 2) * 2; ring.setAttribute("cx", cx); ring.setAttribute("cy", yy); ring.setAttribute("visibility", "visible"); const pb = f.svg.getBoundingClientRect(), cb = card.getBoundingClientRect(); showTip(card, tip, pb.left - cb.left + cx, pb.top - cb.top + yy, `${LABEL[task]}, ${arm}, run ${cur + 1}`, [ { key: "dot", color: "var(--blue)", value: A.aer[cur], label: `average score (${A.episodes[cur]} episodes)` }]); }; const hide = () => { ring.setAttribute("visibility", "hidden"); hideTip(tip); }; hit.addEventListener("pointermove", (e) => { const pb = f.svg.getBoundingClientRect(), px = e.clientX - pb.left; let best = 0; xs.forEach((v, j) => { if (Math.abs(f.x(v) - px) < Math.abs(f.x(xs[best]) - px)) best = j; }); show(best); }); hit.addEventListener("pointerleave", hide); hit.addEventListener("focus", () => show(cur)); hit.addEventListener("blur", hide); hit.addEventListener("keydown", (e) => { if (e.key === "ArrowRight") { show(cur + 1); e.preventDefault(); } if (e.key === "ArrowLeft") { show(cur - 1); e.preventDefault(); } }); y0 += rowH; } } const rows = []; for (const task of ["sc", "bup"]) { const T = DATA.tasks[task]; for (let j = 0; j < 20; j++) rows.push([`${LABEL[task]}, run ${j + 1}`, ...order.map((a) => T.arms[a].aer[j])]); rows.push([`${LABEL[task]}, mean of 20`, ...order.map((a) => T.arms[a].aer_mean)]); } table(document.getElementById("t-strip"), ["Task, run", ...order], rows); } // ---------- the interactive part ---------- function tiles() { const T = DATA.tasks[state.task], A = T.arms[state.arm], i = state.stream; const box = document.getElementById("tiles"); box.replaceChildren(); const tile = (label, value, sub) => { const t = ht("div", { class: "tile" }, null, box); ht("div", { class: "label" }, label, t); ht("div", { class: "value" }, value, t); ht("div", { class: "sub" }, sub, t); }; tile(`Run ${i + 1}: average score`, A.aer[i], `${int(A.episodes[i])} finished episodes`); tile("Average over the 20 runs", A.aer_mean, `spread across runs (SD) ${A.aer_sd}`); if (T.paper[state.arm]) { const [m, sd] = T.paper[state.arm].split(" ± "); tile("Paper, Table 11", m, `SD ${sd}`); } else tile("Paper, Table 11", "–", "not in the paper: our extra test"); } function tablesInteractive() { const T = DATA.tasks[state.task], A = T.arms[state.arm], S = A.streams[state.stream], ds = DATA.ds, W = DATA.window; const rows = []; for (let step = 0; step < DATA.n_steps; step += 500) { const k = step / ds; rows.push([int(step), milli(tokAt(S.tok, step)), milli(S.raw[k]), milli(A.tok_mean[k])]); } table(document.getElementById("t-token"), ["Move", "This run's note", "Critic's guess", "Average note, 20 runs"], rows); const er = A.win_ret.map((v, w) => [`${int(w * W)}–${int((w + 1) * W - 1)}`, String(S.ep.filter((p) => Math.floor(p[0] / W) === w).length), milli(S.win[w]), milli(v)]); table(document.getElementById("t-ep"), ["Moves", "Episodes ended (this run)", "Average score (this run)", "Average score (20 runs)"], er); } function renderInteractive() { const T = DATA.tasks[state.task], A = T.arms[state.arm], i = state.stream; document.getElementById("stream-out").textContent = `run ${i + 1}`; document.getElementById("c-token-sub").textContent = `${LABEL[state.task]} · ${ARM_TEXT[state.arm]} · run ${i + 1}`; document.getElementById("c-ep-sub").textContent = `${LABEL[state.task]} · ${ARM_TEXT[state.arm]} · run ${i + 1}: ${int(A.episodes[i])} finished episodes in ${int(DATA.n_steps)} moves`; tiles(); for (const id of ["c-token-ref", "c-ep-ref"]) document.getElementById(id).hidden = state.arm === "normal"; LINKED = []; tokenChart(document.getElementById("c-token"), state.task, state.arm, i, { mean: true, ref: true, link: true }); episodeChart(document.getElementById("c-ep"), state.task, state.arm, i); tablesInteractive(); } function renderAll() { tokenChart(document.getElementById("c-intro"), "sc", "normal", 0, { xmax: 2000, height: 200 }); renderInteractive(); barsChart(document.getElementById("c-bars")); stripChart(document.getElementById("c-strip")); } function wireControls() { const seg = (id, key) => { const g = document.getElementById(id); g.addEventListener("click", (e) => { const b = e.target.closest("button"); if (!b) return; state[key] = b.dataset.v; for (const x of g.querySelectorAll("button")) x.setAttribute("aria-pressed", String(x === b)); renderInteractive(); }); }; seg("seg-task", "task"); seg("seg-arm", "arm"); const range = document.getElementById("stream"); const set = (v) => { state.stream = Math.max(0, Math.min(19, v)); range.value = state.stream + 1; renderInteractive(); }; range.addEventListener("input", () => set(parseInt(range.value, 10) - 1)); document.getElementById("prev").addEventListener("click", () => set(state.stream - 1)); document.getElementById("next").addEventListener("click", () => set(state.stream + 1)); const btn = document.getElementById("theme"), modes = ["auto", "light", "dark"]; let mode = 0; btn.addEventListener("click", () => { mode = (mode + 1) % 3; if (modes[mode] === "auto") delete document.documentElement.dataset.theme; else document.documentElement.dataset.theme = modes[mode]; btn.textContent = `Theme: ${modes[mode]}`; btn.setAttribute("aria-label", modes[mode] === "auto" ? "Colour theme: follows your system" : `Colour theme: ${modes[mode]}`); }); } (async function main() { try { DATA = await (await fetch("data.json")).json(); } catch (err) { for (const p of document.querySelectorAll(".plot")) p.textContent = "The chart data (data.json) could not be loaded."; return; } wireControls(); renderAll(); let last = document.querySelector("main").clientWidth, timer = null; new ResizeObserver(() => { const w = document.querySelector("main").clientWidth; if (w === last) return; last = w; clearTimeout(timer); timer = setTimeout(renderAll, 120); }).observe(document.querySelector("main")); })();