Download static/visuals/render.js from Ashish-K/stepwise: direct link, hf CLI and curl.
- Browser
- Download file 25.6 kB
-
https://huggingface.co/spaces/Ashish-K/stepwise/resolve/main/static/visuals/render.js
- Command line
-
hf download hf://spaces/Ashish-K/stepwise/static/visuals/render.js
-
curl -L -o render.js https://huggingface.co/spaces/Ashish-K/stepwise/resolve/main/static/visuals/render.js
25.6 kB
| // Drawing for visual classes: static figures (quiz questions) and the step-through explorer (sliders). | |
| // Everything is drawn from the same model and solver the question generator used. | |
| import { Frac, shares, ratioStr, inr, rupee } from "./core.js"; | |
| import * as GW from "./goodwill.js"; | |
| import * as LP from "./lp.js"; | |
| import * as RV from "./revaluation.js"; | |
| import * as RR from "./render_reval.js"; | |
| import * as OP from "./optics.js"; | |
| import * as RO from "./render_optics.js"; | |
| import * as MK from "./market.js"; | |
| import * as RM from "./render_market.js"; | |
| const NS = "http://www.w3.org/2000/svg"; | |
| function el(tag, attrs = {}, ...kids) { | |
| const svg = ["svg", "g", "rect", "line", "polygon", "polyline", "circle", "text", "path", "tspan", "title", "defs", "marker"].includes(tag); | |
| const n = svg ? document.createElementNS(NS, tag) : document.createElement(tag); | |
| for (const [k, v] of Object.entries(attrs || {})) { | |
| if (v == null || v === false) continue; | |
| if (k === "text") n.textContent = v; else if (k.startsWith("on")) n.addEventListener(k.slice(2), v); else n.setAttribute(k, v === true ? "" : v); | |
| } | |
| for (const k of kids.flat()) if (k != null) n.append(k.nodeType ? k : document.createTextNode(String(k))); | |
| return n; | |
| } | |
| const HUES = ["#4c78c8", "#d0892e", "#3a9a6a", "#a65fb8"]; | |
| const reduced = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches; | |
| // ============ goodwill ============ | |
| function shareBars(inst, sol, { showDelta = false, fractions = true } = {}) { | |
| const W = 600, rowH = 34, pad = 64; const svg = el("svg", { viewBox: `0 0 ${W} ${rowH * 2 + 40}`, class: "vz-svg", role: "img", "aria-label": `Old shares ${ratioStr(inst.old)}, new shares ${ratioStr(inst.new)}` }); | |
| const rows = [["Old", sol.oldS], ["New", sol.newS]]; | |
| rows.forEach(([label, S], r) => { | |
| const y = 8 + r * (rowH + 14); let x = pad; const span = W - pad - 8; | |
| svg.append(el("text", { x: 0, y: y + rowH / 2 + 5, class: "vz-lab", text: label })); | |
| S.forEach((s, i) => { | |
| const w = s.val() * span; if (w <= 0) return; | |
| const d = sol.delta[i]; const tone = showDelta && r === 1 ? (d.sign() > 0 ? "gain" : d.sign() < 0 ? "sac" : "") : ""; | |
| svg.append(el("rect", { x, y, width: Math.max(0, w - 2), height: rowH, rx: 4, fill: HUES[i % 4], class: `vz-seg ${tone}` }, el("title", { text: `${inst.partners[i]}: ${s}` }))); | |
| if (w > 46) svg.append(el("text", { x: x + 6, y: y + rowH / 2 + 5, class: "vz-seg-t", text: fractions ? `${inst.partners[i]} ${s}` : inst.partners[i] })); | |
| else if (w > 18) svg.append(el("text", { x: x + 4, y: y + rowH / 2 + 5, class: "vz-seg-t", text: inst.partners[i][0] })); | |
| x += w; | |
| }); | |
| }); | |
| return svg; | |
| } | |
| function shareTable(inst, sol, { delta = false, amounts = false, terms = false } = {}) { | |
| const t = el("table", { class: "vz-table" }); | |
| if (terms) { // before answering: the ratios as given, not the worked fractions | |
| t.append(el("thead", null, el("tr", null, el("th", { text: "Partner" }), el("th", { class: "num", text: "Old ratio" }), el("th", { class: "num", text: "New ratio" })))); | |
| const tb = el("tbody"); inst.partners.forEach((p, i) => tb.append(el("tr", null, el("td", null, el("span", { class: "vz-dot", style: `background:${HUES[i % 4]}` }), p), el("td", { class: "num", text: inst.old[i] || "—" }), el("td", { class: "num", text: inst.new[i] || "—" })))); | |
| t.append(tb); | |
| return el("div", null, t, el("p", { class: "vz-cap", text: "Ratio terms are not shares: compare shares by the bar widths, or turn each ratio into fractions." })); | |
| } | |
| t.append(el("thead", null, el("tr", null, el("th", { text: "Partner" }), el("th", { text: "Old share" }), el("th", { text: "New share" }), | |
| delta ? el("th", { text: "Gain / sacrifice" }) : null, amounts ? el("th", { text: `× ${rupee(inst.goodwill)}` }) : null))); | |
| const tb = el("tbody"); | |
| inst.partners.forEach((p, i) => { | |
| const d = sol.delta[i]; const cls = d.sign() > 0 ? "gain" : d.sign() < 0 ? "sac" : ""; | |
| tb.append(el("tr", null, el("td", null, el("span", { class: "vz-dot", style: `background:${HUES[i % 4]}` }), p), el("td", { class: "num", text: inst.old[i] ? String(sol.oldS[i]) : "—" }), el("td", { class: "num", text: inst.new[i] ? String(sol.newS[i]) : "—" }), | |
| delta ? el("td", { class: `num ${cls}`, text: d.sign() === 0 ? "no change" : `${d.sign() > 0 ? "gains" : "sacrifices"} ${d.abs()}` }) : null, | |
| amounts ? el("td", { class: `num ${cls}`, text: d.sign() === 0 ? "—" : `${d.sign() > 0 ? "Dr" : "Cr"} ${inr(sol.amt[i].val())}` }) : null)); | |
| }); | |
| t.append(tb); return t; | |
| } | |
| function journal(entries, { title = "Journal", wrong = false } = {}) { | |
| const t = el("table", { class: `vz-table vz-journal ${wrong ? "wrong" : ""}` }); | |
| t.append(el("thead", null, el("tr", null, el("th", { text: "Particulars" }), el("th", { class: "num", text: "Dr (₹)" }), el("th", { class: "num", text: "Cr (₹)" })))); | |
| const tb = el("tbody"); | |
| for (const e of entries) { | |
| for (const l of e.dr) tb.append(el("tr", { class: "dr" }, el("td", { text: `${l.acct} ...Dr.` }), el("td", { class: "num", text: inr(l.amt) }), el("td"))); | |
| for (const l of e.cr) tb.append(el("tr", { class: "cr" }, el("td", { class: "to", text: `To ${l.acct}` }), el("td"), el("td", { class: "num", text: inr(l.amt) }))); | |
| if (e.narr) tb.append(el("tr", { class: "narr" }, el("td", { colspan: 3, text: `(${e.narr})` }))); | |
| } | |
| t.append(tb); | |
| return el("div", { class: "vz-block" }, el("div", { class: "vz-cap", text: title }), t); | |
| } | |
| function gwFigure(fig, { reveal = false } = {}) { | |
| const inst = fig.inst; const sol = GW.solve(inst); const show = fig.show || []; | |
| const box = el("div", { class: "vz" }); | |
| box.append(el("div", { class: "vz-cap", text: `${inst.firm} · goodwill ${rupee(inst.goodwill)}${inst.capitals === "fixed" ? " · fixed capitals" : ""}` }), shareBars(inst, sol, { showDelta: reveal, fractions: reveal }), shareTable(inst, sol, { delta: reveal, amounts: reveal, terms: !reveal })); | |
| if (show.includes("entry") && fig.entry) box.append(journal([fig.entry], { title: "The accountant's entry", wrong: true })); | |
| if (reveal) box.append(journal(sol.entries, { title: "Correct entry" })); | |
| if (reveal && fig.after) { | |
| const s2 = GW.solve(fig.after); | |
| box.append(el("div", { class: "vz-cap vz-after", text: "After the change" }), shareBars(fig.after, s2, { showDelta: true }), shareTable(fig.after, s2, { delta: true, amounts: true }), journal(s2.entries, { title: "Entry after the change" })); | |
| } | |
| return box; | |
| } | |
| // ============ linear programming ============ | |
| function clip(poly, k) { // Sutherland–Hodgman against a·x + b·y (op) c | |
| const f = (p) => (k.op === "<=" ? k.c - (k.a * p[0] + k.b * p[1]) : k.a * p[0] + k.b * p[1] - k.c); | |
| const out = []; | |
| for (let i = 0; i < poly.length; i++) { | |
| const P = poly[i], Q = poly[(i + 1) % poly.length]; const fp = f(P), fq = f(Q); | |
| if (fp >= -1e-9) out.push(P); | |
| if ((fp >= -1e-9) !== (fq >= -1e-9)) { const t = fp / (fp - fq); out.push([P[0] + t * (Q[0] - P[0]), P[1] + t * (Q[1] - P[1])]); } | |
| } | |
| return out; | |
| } | |
| const nice = (m) => { const steps = [1, 2, 5, 10, 20, 25, 50, 100]; for (const s of steps) if (m / s <= 8) return s; return 200; }; | |
| function lpSvg(inst, { stage = 9, wrongSide = null, iso = null, isoK = null, labels = true, highlight = null, region = true } = {}) { | |
| const sol = LP.solve(inst); | |
| const ints = inst.cons.flatMap((k) => [k.a ? k.c / k.a : 0, k.b ? k.c / k.b : 0]); | |
| const mx = Math.max(...sol.corners.map((p) => p.x.val()), ...inst.cons.map((k) => (k.a ? k.c / k.a : 0)), 4) * 1.15; | |
| const my = Math.max(...sol.corners.map((p) => p.y.val()), ...inst.cons.map((k) => (k.b ? k.c / k.b : 0)), 4) * 1.15; | |
| const W = 520, H = 400, L = 44, B = 34, T = 10, R = 12; | |
| const X = (x) => L + (x / mx) * (W - L - R), Y = (y) => H - B - (y / my) * (H - B - T); | |
| const svg = el("svg", { viewBox: `0 0 ${W} ${H}`, class: "vz-svg vz-lp", role: "img", "aria-label": `Graph of ${LP.lppStr(inst)}` }); | |
| // grid + axes | |
| const sx = nice(mx), sy = nice(my); | |
| for (let x = sx; x < mx; x += sx) svg.append(el("line", { x1: X(x), y1: Y(0), x2: X(x), y2: Y(my), class: "vz-grid" }), el("text", { x: X(x), y: H - B + 16, class: "vz-tick", "text-anchor": "middle", text: x })); | |
| for (let y = sy; y < my; y += sy) svg.append(el("line", { x1: X(0), y1: Y(y), x2: X(mx), y2: Y(y), class: "vz-grid" }), el("text", { x: L - 6, y: Y(y) + 4, class: "vz-tick", "text-anchor": "end", text: y })); | |
| svg.append(el("line", { x1: X(0), y1: Y(0), x2: X(mx), y2: Y(0), class: "vz-axis" }), el("line", { x1: X(0), y1: Y(0), x2: X(0), y2: Y(my), class: "vz-axis" }), | |
| el("text", { x: X(mx) - 4, y: Y(0) - 6, class: "vz-lab", "text-anchor": "end", text: "x" }), el("text", { x: X(0) + 6, y: Y(my) + 12, class: "vz-lab", text: "y" })); | |
| // region: viewport box clipped by each constraint shown so far | |
| const cons = inst.cons.map((k, i) => (i === wrongSide ? { ...k, op: k.op === "<=" ? ">=" : "<=" } : k)); | |
| const shown = cons.slice(0, Math.min(cons.length, stage)); | |
| let poly = [[0, 0], [mx, 0], [mx, my], [0, my]]; for (const k of shown) poly = clip(poly, k); | |
| if (region && stage >= 1 && poly.length >= 3) svg.append(el("polygon", { points: poly.map((p) => `${X(p[0])},${Y(p[1])}`).join(" "), class: `vz-region ${stage < cons.length ? "partial" : ""}` })); | |
| // constraint lines | |
| cons.forEach((k, i) => { | |
| if (i >= stage) return; | |
| const pts = []; if (k.b) pts.push([0, k.c / k.b]); if (k.a) pts.push([k.c / k.a, 0]); if (!k.b) pts.push([k.c / k.a, my]); if (!k.a) pts.push([mx, k.c / k.b]); | |
| const [p, q] = pts; const name = (inst.ctx.res[k.res] && inst.ctx.res[k.res].name) || "market"; | |
| svg.append(el("line", { x1: X(p[0]), y1: Y(p[1]), x2: X(q[0]), y2: Y(q[1]), class: `vz-con c${i} ${highlight === i ? "hl" : ""}` })); | |
| if (labels) { const lx = Math.min(X(p[0]) + 8, W - 150), ly = Math.max(Y(p[1]) + (k.b ? 14 : 30), 22); svg.append(el("text", { x: lx, y: ly, class: `vz-con-t c${i}`, text: `${name}: ${LP.ineqStr(inst.cons[i])}` })); } | |
| }); | |
| // iso-profit line Z = k | |
| if (iso && isoK != null) { | |
| const { p, q } = inst.obj; const a = [0, isoK / q], b = [isoK / p, 0]; | |
| svg.append(el("line", { x1: X(a[0]), y1: Y(a[1]), x2: X(b[0]), y2: Y(b[1]), class: "vz-iso" }), el("text", { x: X(0) + 8, y: Math.max(T + 30, Math.min(Y(a[1]) - 8, H - B - 40)), class: "vz-iso-t", text: `Z = ${inr(isoK)}` })); | |
| } | |
| // corners | |
| if (stage >= 9 && labels !== "nocorners") { | |
| for (const c of sol.corners) { | |
| const best = sol.ties.includes(c); | |
| svg.append(el("circle", { cx: X(c.x.val()), cy: Y(c.y.val()), r: best && iso ? 6 : 4.5, class: `vz-corner ${best && iso ? "best" : ""}` })); | |
| svg.append(el("text", { x: X(c.x.val()) + 7, y: Y(c.y.val()) - 7, class: "vz-corner-t", text: LP.ptStr(c) })); | |
| } | |
| } | |
| return svg; | |
| } | |
| function zTable(inst) { | |
| const sol = LP.solve(inst); const t = el("table", { class: "vz-table" }); | |
| t.append(el("thead", null, el("tr", null, el("th", { text: "Corner" }), el("th", { class: "num", text: `Z = ${inst.obj.p}x + ${inst.obj.q}y` })))); | |
| const tb = el("tbody"); for (const c of sol.corners) tb.append(el("tr", { class: sol.ties.includes(c) ? "best" : "" }, el("td", { text: LP.ptStr(c) }), el("td", { class: "num", text: inr(c.z.val()) }))); | |
| t.append(tb); return t; | |
| } | |
| function lpFigure(fig, { reveal = false } = {}) { | |
| const inst = fig.inst; const show = fig.show || []; const sol = LP.solve(inst); | |
| const box = el("div", { class: "vz" }, el("div", { class: "vz-cap", text: LP.lppStr(inst) })); | |
| const corners = show.includes("corners") || reveal; | |
| box.append(lpSvg(inst, { stage: corners ? 9 : inst.cons.length, wrongSide: fig.wrongSide ?? null, iso: (show.includes("iso") || reveal) && sol.optimumExists, isoK: sol.best ? sol.best.z.val() : null, region: show.includes("region") || reveal })); | |
| if (reveal) box.append(zTable(inst)); | |
| if (reveal && fig.after) { const s2 = LP.solve(fig.after); box.append(el("div", { class: "vz-cap vz-after", text: `After the change: ${LP.lppStr(fig.after)}` }), lpSvg(fig.after, { iso: s2.optimumExists, isoK: s2.best ? s2.best.z.val() : null }), zTable(fig.after)); } | |
| return box; | |
| } | |
| // ============ public: static figure ============ | |
| export function renderFigure(fig, opts = {}) { | |
| if (!fig || !fig.cls) return null; | |
| try { return fig.cls === "goodwill" ? gwFigure(fig, opts) : fig.cls === "lp" ? lpFigure(fig, opts) : fig.cls === "reval" ? RR.revalFigure(fig, opts) : fig.cls === "optics" ? RO.opticsFigure(fig, opts) : fig.cls === "market" ? RM.marketFigure(fig, opts) : null; } | |
| catch (e) { console.error("figure", e); return el("p", { class: "muted", text: "(The diagram could not be drawn.)" }); } | |
| } | |
| // ============ public: explorer (step-through + controls) ============ | |
| export function explorer(cls, inst0) { | |
| if (cls === "reval") return RR.revalExplorer(inst0); | |
| if (cls === "optics") return RO.opticsExplorer(inst0); | |
| if (cls === "market") return RM.marketExplorer(inst0); | |
| const root = el("div", { class: "vz vz-explorer" }); | |
| let inst = JSON.parse(JSON.stringify(inst0)); let step = 0; | |
| const STEPS = cls === "goodwill" | |
| ? ["Old shares", "New shares", "Who gains, who sacrifices", "How much: goodwill × change", "The journal entry"] | |
| : ["The first constraint", "The second constraint", "The feasible region", "Corner points and Z", "Slide the profit line"]; | |
| const say = { | |
| goodwill: [ | |
| () => `${inst.partners.join(", ")} share profits ${ratioStr(inst.old.filter((x, i) => !(inst.scenario === "admission" && i === inst.incoming)))}. Each coloured block is one partner's share of future profit.`, | |
| () => `The new ratio is ${ratioStr(inst.new.filter((x, i) => !(inst.scenario === "retirement" && i === inst.retiring)))}. Compare each partner's block in the two bars: who has more, who has less?`, | |
| () => `Gain = new share − old share. A partner whose block grew gains (green); one whose block shrank sacrifices (orange). Equal blocks: no change, so not in the entry.`, | |
| () => `Each amount = ${rupee(inst.goodwill)} × that partner's gain or sacrifice. The gainers' amounts add up to the sacrificers' amounts.`, | |
| () => inst.scenario === "admission" ? `The new partner's premium goes to the sacrificing partners in their sacrificing ratio. Goodwill A/c is never opened (AS 26).` : `The partner who gains pays: gainers are debited, sacrificers credited.${inst.capitals === "fixed" ? " Fixed capitals, so Current A/cs." : ""} Goodwill A/c is never opened (AS 26).`, | |
| ], | |
| lp: [ | |
| () => `${LP.ineqStr(inst.cons[0])}: draw the line through its intercepts, then shade the side that satisfies it (test the origin).`, | |
| () => `${LP.ineqStr(inst.cons[1])}: same again. Only points on the correct side of BOTH lines survive.`, | |
| () => `The feasible region is what is left, with x ≥ 0 and y ≥ 0. ${LP.solve(inst).bounded ? "It is bounded." : "It is unbounded: it goes on for ever away from the origin."}`, | |
| () => `The corners are where boundaries meet. Work out Z at each one.`, | |
| () => `The line ${inst.obj.p}x + ${inst.obj.q}y = Z moves parallel to itself. The last corner it touches is the ${inst.obj.sense === "max" ? "maximum" : "minimum"}. Move the sliders and watch the best corner change — or not.`, | |
| ], | |
| }[cls]; | |
| const fig = el("div", { class: "vz-stage" }); const text = el("p", { class: "vz-say", "aria-live": "polite" }); const controls = el("div", { class: "vz-controls" }); | |
| const stepper = el("div", { class: "vz-steps" }); | |
| const back = el("button", { class: "btn ghost", type: "button", text: "← Back", onclick: () => { if (step > 0) { step--; draw(); } } }); | |
| const next = el("button", { class: "btn", type: "button", text: "Next →", onclick: () => { if (step < STEPS.length - 1) { step++; draw(true); } } }); | |
| const dots = el("ol", { class: "vz-dots" }); | |
| stepper.append(back, dots, next); | |
| root.append(el("div", { class: "vz-cap", text: cls === "goodwill" ? `${inst.firm} · ${GW.SCENARIOS[inst.scenario]}` : `${inst.ctx.maker} · ${LP.SCENARIOS[inst.scenario]}` }), stepper, text, fig, controls); | |
| function buildControls() { | |
| controls.replaceChildren(); | |
| if (cls === "goodwill") { | |
| const row = el("div", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "New ratio" })); | |
| inst.partners.forEach((p, i) => { | |
| if ((inst.scenario === "retirement" && i === inst.retiring)) return; | |
| const val = el("span", { class: "vz-num", text: inst.new[i] }); | |
| row.append(el("span", { class: "vz-step" }, el("span", { class: "vz-dot", style: `background:${HUES[i % 4]}` }), p, | |
| el("button", { type: "button", class: "react", "aria-label": `less for ${p}`, text: "−", onclick: () => { if (inst.new[i] > 1) { inst.new[i]--; draw(); buildControls(); } } }), val, | |
| el("button", { type: "button", class: "react", "aria-label": `more for ${p}`, text: "+", onclick: () => { if (inst.new[i] < 9) { inst.new[i]++; draw(); buildControls(); } } }))); | |
| }); | |
| const gs = [30000, 48000, 60000, 72000, 90000, 120000, 150000, 180000, 240000, 300000, 360000, 480000, 600000]; | |
| const gi = el("input", { type: "range", min: 0, max: gs.length - 1, step: 1, value: Math.max(0, gs.findIndex((g) => g >= inst.goodwill)), "aria-label": "Goodwill" }); | |
| const gl = el("span", { class: "vz-num", text: rupee(inst.goodwill) }); | |
| gi.addEventListener("input", () => { inst.goodwill = gs[Number(gi.value)]; gl.textContent = rupee(inst.goodwill); draw(); }); | |
| const fixed = el("input", { type: "checkbox", checked: inst.capitals === "fixed" || null }); | |
| fixed.addEventListener("change", () => { inst.capitals = fixed.checked ? "fixed" : "fluctuating"; draw(); }); | |
| controls.append(row, el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: "Goodwill" }), gi, gl), | |
| inst.scenario !== "admission" ? el("label", { class: "vz-ctl" }, fixed, " Capitals are fixed (use Current A/cs)") : null, | |
| el("button", { type: "button", class: "react", text: "Reset", onclick: () => { inst = JSON.parse(JSON.stringify(inst0)); buildControls(); draw(); } })); | |
| } else { | |
| const slider = (label, get, set, min, max, stp) => { | |
| const out = el("span", { class: "vz-num", text: get() }); const inp = el("input", { type: "range", min, max, step: stp, value: get(), "aria-label": label }); | |
| inp.addEventListener("input", () => { set(Number(inp.value)); out.textContent = get(); draw(); }); | |
| return el("label", { class: "vz-ctl" }, el("span", { class: "vz-ctl-l", text: label }), inp, out); | |
| }; | |
| const word = inst.obj.sense === "max" ? "Profit" : "Cost"; | |
| controls.append( | |
| slider(`${word} per ${inst.ctx.xs} (p)`, () => inst.obj.p, (v) => (inst.obj.p = v), 5, Math.max(inst.obj.p * 3, 50), 5), | |
| slider(`${word} per ${inst.ctx.ys} (q)`, () => inst.obj.q, (v) => (inst.obj.q = v), 5, Math.max(inst.obj.q * 3, 50), 5), | |
| ...inst.cons.slice(0, 2).map((k, i) => slider(`${inst.ctx.res[k.res].name} ${inst.obj.sense === "max" ? "available" : "needed"}`, () => inst.cons[i].c, (v) => (inst.cons[i].c = v), Math.ceil(k.c / 2), k.c * 2, 1)), | |
| el("button", { type: "button", class: "react", text: "Reset", onclick: () => { inst = JSON.parse(JSON.stringify(inst0)); buildControls(); draw(); } })); | |
| } | |
| } | |
| let anim = 0; | |
| function draw(animate = false) { | |
| cancelAnimationFrame(anim); | |
| dots.replaceChildren(...STEPS.map((s, i) => el("li", { class: i === step ? "on" : i < step ? "done" : "" }, el("button", { type: "button", "aria-label": s, title: s, onclick: () => { step = i; draw(true); } })))); | |
| back.disabled = step === 0; next.disabled = step === STEPS.length - 1; | |
| text.replaceChildren(el("strong", { text: `${step + 1}. ${STEPS[step]}. ` }), say[step]()); | |
| controls.hidden = step < STEPS.length - 1 && cls === "lp"; | |
| fig.replaceChildren(); | |
| if (cls === "goodwill") { | |
| const sol = GW.solve(inst); | |
| const same = sol.delta.every((d) => d.sign() === 0); | |
| if (same) { fig.append(shareBars(inst, sol), el("p", { class: "muted", text: "Nobody's share changed, so there is nothing to adjust." })); return; } | |
| if (step === 0) fig.append(shareBars({ ...inst, new: inst.old }, { ...sol, newS: sol.oldS, delta: sol.delta.map(() => new Frac(0)) })); | |
| else fig.append(shareBars(inst, sol, { showDelta: step >= 2 })); | |
| if (step >= 1) fig.append(shareTable(inst, sol, { delta: step >= 2, amounts: step >= 3 })); | |
| if (step >= 4) fig.append(journal(sol.entries, { title: "Journal entry" })); | |
| } else { | |
| const sol = LP.solve(inst); | |
| if (step <= 2) fig.append(lpSvg(inst, { stage: step + 1, highlight: step < 2 ? step : null, labels: true })); | |
| else if (step === 3) fig.append(lpSvg(inst, { stage: 9 }), zTable(inst)); | |
| else { | |
| const svgBox = el("div"); fig.append(svgBox, zTable(inst)); | |
| const target = sol.best ? sol.best.z.val() : 0; | |
| const start = inst.obj.sense === "max" ? 0 : target * 2.2; | |
| if (!animate || reduced()) { svgBox.append(lpSvg(inst, { stage: 9, iso: true, isoK: target })); return; } | |
| const t0 = performance.now(); const dur = 1400; | |
| const tick = (t) => { const u = Math.min(1, (t - t0) / dur); const e = 1 - Math.pow(1 - u, 3); svgBox.replaceChildren(lpSvg(inst, { stage: 9, iso: u >= 1, isoK: start + (target - start) * e })); if (u < 1) anim = requestAnimationFrame(tick); else svgBox.replaceChildren(lpSvg(inst, { stage: 9, iso: true, isoK: target })); }; | |
| anim = requestAnimationFrame(tick); | |
| } | |
| } | |
| } | |
| buildControls(); draw(); | |
| return root; | |
| } | |
| export function sampleFor(cls, scenario, seed = 1) { | |
| const { rng } = CORE; const r = rng(seed); return (cls === "goodwill" ? GW : cls === "reval" ? RV : cls === "optics" ? OP : cls === "market" ? MK : LP).sample(r, scenario); | |
| } | |
| import * as CORE from "./core.js"; | |
| export const CSS = RR.CSS + RO.CSS + RM.CSS + ` | |
| .vz{display:grid;gap:10px;margin:8px 0 12px;padding:12px;border:1px solid var(--rule);border-radius:var(--radius);background:var(--sheet)} | |
| .vz-cap{font-family:var(--font-display);font-size:.88rem;color:var(--ink-soft)} | |
| .vz-after{margin-top:6px;color:var(--accent);font-weight:600} | |
| .vz-svg{width:100%;height:auto;max-width:620px;display:block} | |
| .vz-lab,.vz-tick{fill:var(--ink-soft);font:12px var(--font-data)} | |
| .vz-seg{stroke:var(--sheet);stroke-width:1} | |
| .vz-seg.gain{stroke:var(--good);stroke-width:3} | |
| .vz-seg.sac{stroke:var(--warn);stroke-width:3;stroke-dasharray:5 3} | |
| .vz-seg-t{fill:#fff;font:600 12px var(--font-body);pointer-events:none} | |
| .vz-table{border-collapse:collapse;width:100%;max-width:620px;font-size:.92rem} | |
| .vz-table th,.vz-table td{border-bottom:1px solid var(--rule);padding:5px 8px;text-align:left} | |
| .vz-table th{font-family:var(--font-display);font-weight:600;color:var(--ink-soft)} | |
| .vz-table .num{text-align:right;font-family:var(--font-data);white-space:nowrap} | |
| .vz-table .gain{color:var(--good);font-weight:600}.vz-table .sac{color:var(--warn);font-weight:600} | |
| .vz-table tr.best td{background:var(--good-wash);font-weight:600} | |
| .vz-journal td.to{padding-left:28px}.vz-journal tr.narr td{color:var(--ink-soft);font-style:italic;font-size:.85rem} | |
| .vz-journal.wrong{outline:2px dashed var(--bad);outline-offset:2px} | |
| .vz-dot{display:inline-block;width:10px;height:10px;border-radius:3px;margin-right:6px;vertical-align:-1px} | |
| .vz-grid{stroke:var(--grid);stroke-width:1}.vz-axis{stroke:var(--ink);stroke-width:1.5} | |
| .vz-region{fill:var(--accent-wash);stroke:var(--accent);stroke-width:1.5;opacity:.95}.vz-region.partial{opacity:.6} | |
| .vz-con{stroke-width:2.2}.vz-con.c0{stroke:#4c78c8}.vz-con.c1{stroke:#d0892e}.vz-con.c2{stroke:#a65fb8}.vz-con.hl{stroke-width:4} | |
| .vz-con-t{font:600 12px var(--font-data)}.vz-con-t.c0{fill:#4c78c8}.vz-con-t.c1{fill:#d0892e}.vz-con-t.c2{fill:#a65fb8} | |
| .vz-lp .vz-tick,.vz-lp .vz-lab,.vz-lp .vz-con-t,.vz-lp .vz-corner-t,.vz-lp .vz-iso-t{font-size:15px} | |
| @media (max-width:520px){.vz-lp .vz-tick,.vz-lp .vz-lab,.vz-lp .vz-con-t,.vz-lp .vz-corner-t,.vz-lp .vz-iso-t{font-size:19px}.vz-seg-t{font-size:15px}.vz{padding:8px}} | |
| .vz-iso{stroke:var(--good);stroke-width:2.5;stroke-dasharray:7 4}.vz-iso-t{fill:var(--good);font:600 12px var(--font-data)} | |
| .vz-corner{fill:var(--ink)}.vz-corner.best{fill:var(--good)}.vz-corner-t{fill:var(--ink);font:12px var(--font-data);paint-order:stroke;stroke:var(--sheet);stroke-width:3px} | |
| .vz-steps{display:flex;align-items:center;gap:10px;flex-wrap:wrap} | |
| .vz-dots{display:flex;gap:6px;list-style:none;margin:0;padding:0} | |
| .vz-dots button{width:12px;height:12px;border-radius:50%;border:1px solid var(--accent);background:transparent;padding:0;cursor:pointer} | |
| .vz-dots li.on button{background:var(--accent)}.vz-dots li.done button{background:var(--accent-wash)} | |
| .vz-say{margin:0;min-height:3em} | |
| .vz-controls{display:grid;gap:8px;padding-top:8px;border-top:1px solid var(--rule)} | |
| .vz-ctl{display:flex;align-items:center;gap:8px;flex-wrap:wrap}.vz-ctl-l{font-family:var(--font-display);font-size:.88rem;min-width:9em;color:var(--ink-soft)} | |
| .vz-ctl input[type=range]{flex:1;min-width:140px;max-width:320px} | |
| .vz-step{display:inline-flex;align-items:center;gap:4px;margin-right:10px} | |
| .vz-num{font-family:var(--font-data);min-width:2ch;text-align:center} | |
| `; | |