diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 19cca3363..43b9857b6 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1226,7 +1226,7 @@ body.window .field.solo { grid-column: auto; } .led-style .opt { display: inline-flex; align-items: center; padding: 0 6px; } .led-style .opt svg { display: block; } .led-chart g.ln { fill: none; stroke-linecap: round; stroke-linejoin: round; transition: opacity .12s; } -.led-chart g.ln .line { stroke-width: 1.6; } +.led-chart g.ln .line { stroke-width: 1.8; } .led-chart g.ln .line.all { stroke-width: 1.2; stroke-dasharray: 3 3; } .led-chart g.ln .band { stroke: none; fill-opacity: .14; } .led-chart .led-day { cursor: pointer; outline: none; } diff --git a/internal/gui/assets/plugins/usage/page.js b/internal/gui/assets/plugins/usage/page.js index bcc182bbd..0db559494 100644 --- a/internal/gui/assets/plugins/usage/page.js +++ b/internal/gui/assets/plugins/usage/page.js @@ -1169,6 +1169,30 @@ function ledStyle(host) { // model's line is at its own speed, broken where it had none, over the // point's own speed dashed. The tooltip, the hover, a day's pick and the // ranking's emphasis are drawLedColumns' own, over the same slots. +// A smooth line through points [x, y], as d3's curveMonotoneX draws it +// (Fritsch–Carlson): it bends between points but never past one, so a +// quiet hour isn't drawn dipping under zero or a peak over its value. +// "from" starts it ("M", or "L" to go on from where a path is). +function ledCurve(ps, from = "M") { + const n = ps.length, f = (v) => v.toFixed(1); + if (!n) return ""; + let d = from + f(ps[0][0]) + " " + f(ps[0][1]); + if (n === 1) return d; + const s = [], m = []; + for (let i = 0; i < n - 1; i++) s.push((ps[i + 1][1] - ps[i][1]) / (ps[i + 1][0] - ps[i][0])); + for (let i = 1; i < n - 1; i++) { + const h0 = ps[i][0] - ps[i - 1][0], h1 = ps[i + 1][0] - ps[i][0], p = (s[i - 1] * h1 + s[i] * h0) / (h0 + h1); + m[i] = s[i - 1] * s[i] <= 0 ? 0 : (Math.sign(s[i - 1]) + Math.sign(s[i])) * Math.min(Math.abs(s[i - 1]), Math.abs(s[i]), Math.abs(p) / 2); + } + m[0] = n > 2 ? (3 * s[0] - m[1]) / 2 : s[0]; + m[n - 1] = n > 2 ? (3 * s[n - 2] - m[n - 2]) / 2 : s[0]; + for (let i = 0; i < n - 1; i++) { + const [xa, ya] = ps[i], [xb, yb] = ps[i + 1], dx = (xb - xa) / 3; + d += "C" + f(xa + dx) + " " + f(ya + dx * m[i]) + " " + f(xb - dx) + " " + f(yb - dx * m[i + 1]) + " " + f(xb) + " " + f(yb); + } + return d; +} + function ledLines(g, Y, x0, slot, segs) { const l = ledger, pts = l?.series || [], metric = ledMetric, split = ledSplit; for (const r of segs) r.remove(); @@ -1177,7 +1201,7 @@ function ledLines(g, Y, x0, slot, segs) { const speed = metric === "speed"; const { top } = ledCategories(l.chartBy ? { by: l.chartBy } : l, split, metric); const X = (i) => x0 + slot * i, dot = slot >= 12 ? 4.5 : 3; - const xy = (i, v) => X(i).toFixed(1) + " " + Y(v).toFixed(1); + const at = (i, v) => [X(i), Y(v)]; // stacked, each one goes under those before it, so a band never covers // the line under it let head = null; @@ -1185,17 +1209,20 @@ function ledLines(g, Y, x0, slot, segs) { // one series: its line, the dots at its points and the band under it const series = (key, color, vals, base, dashed) => { const s = sv("g", { class: "col ln", "data-k": key, "data-day": l.day || "", "data-color": color }, { stroke: color }); - let line = "", dots = "", up = "", down = "", run = false; - vals.forEach((v, i) => { - if (v == null) return void (run = false); - line += (run ? "L" : "M") + xy(i, v); - if (!dashed && (v > (base?.[i] || 0) || speed)) dots += "M" + xy(i, v) + "h0"; - run = true; - }); + // each unbroken run is one smooth curve; a point alone in its run has + // no line to sit on, so it is a dot + let line = "", dots = "", run = []; + const end = () => { + if (run.length === 1 && !dashed) dots += "M" + run[0][0].toFixed(1) + " " + run[0][1].toFixed(1) + "h0"; + line += ledCurve(run); + run = []; + }; + vals.forEach((v, i) => { if (v == null) end(); else run.push(at(i, v)); }); + end(); if (!line) return; if (base) { - vals.forEach((v, i) => { up += (i ? "L" : "M") + xy(i, v); }); - for (let i = vals.length - 1; i >= 0; i--) down += "L" + xy(i, base[i]); + const up = ledCurve(vals.map((v, i) => at(i, v))); + const down = ledCurve(base.map((v, i) => at(i, v)).reverse(), "L"); s.append(sv("path", { d: up + down + "Z", class: "band" }, { fill: color })); } s.append(sv("path", { d: line, class: "line" + (dashed ? " all" : "") })); diff --git a/internal/gui/tests/usage-chart-line.test.cjs b/internal/gui/tests/usage-chart-line.test.cjs index 9f6fa5f21..ab6e06ab7 100644 --- a/internal/gui/tests/usage-chart-line.test.cjs +++ b/internal/gui/tests/usage-chart-line.test.cjs @@ -98,7 +98,8 @@ const drawn = (p) => p.evaluate(() => { // the top of a stacked line chart, at the hour every model has a part in: // the highest line ends where the column's top did, the total's height const topAt = (p, h) => p.evaluate((h) => { - const at = [...document.querySelectorAll("#ledChart g.ln .line")].map((x) => x.getAttribute("d").split(/[ML]/).filter(Boolean).map((s) => s.split(" ").map(Number))[h]); + // the points a line passes through: each command's last x y + const at = [...document.querySelectorAll("#ledChart g.ln .line")].map((x) => x.getAttribute("d").split(/[MLC]/).filter(Boolean).map((s) => s.split(" ").map(Number).slice(-2))[h]); return at.reduce((a, b) => (b[1] < a[1] ? b : a)); }, h); // the top of the column whose middle is at x @@ -196,7 +197,16 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // a model's line breaks where it had no speed: luna starts at 10:00 const luna = await p.locator('#ledChart g.ln[data-k="gpt-6-luna"] .line').getAttribute("d"); assert.equal(luna.split("M").length - 1, 1, "one run: " + luna); - assert.equal(luna.split(/[ML]/).filter(Boolean).length, 14, "10:00 to 23:00"); + assert.equal(luna.split(/[MLC]/).filter(Boolean).length, 14, "10:00 to 23:00"); + assert.equal(luna.split("C").length - 1, 13, "drawn as curves between its points: " + luna); + // the curves bend between points but never past them: no line + // reaches over its highest point or under its lowest + const past = await p.evaluate(() => [...document.querySelectorAll("#ledChart g.ln .line")].map((x) => { + const ys = x.getAttribute("d").split(/[MLC]/).filter(Boolean).map((s) => +s.split(" ").at(-1)); + const b = x.getBBox(); + return [b.y - Math.min(...ys), Math.max(...ys) - (b.y + b.height)]; + }).filter(([a, b]) => a < -0.5 || b < -0.5)); + assert.deepEqual(past, [], "a curve overshoots its points"); // the tooltip over the lines, and the ranking's emphasis await p.locator("#ledMetric .opt").nth(0).click();