@oh-my-pi/pi-tui 18.8.7 → 18.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -86
- package/dist/types/charts/chart-plan.d.ts +11 -5
- package/dist/types/charts/chart-svg.d.ts +11 -7
- package/dist/types/charts/table-data.d.ts +6 -1
- package/dist/types/chat/execution-shared.d.ts +6 -5
- package/dist/types/keybinding-matchers.d.ts +3 -0
- package/dist/types/kitty-graphics.d.ts +2 -2
- package/dist/types/multiplexers/cmux.d.ts +16 -0
- package/dist/types/multiplexers/herdr.d.ts +26 -0
- package/dist/types/multiplexers/notify.d.ts +13 -0
- package/dist/types/multiplexers/orca.d.ts +12 -0
- package/dist/types/multiplexers/screen.d.ts +9 -0
- package/dist/types/multiplexers/tmux.d.ts +29 -0
- package/dist/types/multiplexers/types.d.ts +100 -0
- package/dist/types/multiplexers/wmux.d.ts +12 -0
- package/dist/types/multiplexers/zellij.d.ts +12 -0
- package/dist/types/overlays/annotation-overlay.d.ts +12 -0
- package/dist/types/overlays/annotation-types.d.ts +2 -0
- package/dist/types/overlays/hook-selector.d.ts +9 -4
- package/dist/types/overlays/model-browser.d.ts +2 -2
- package/dist/types/overlays/model-hub.d.ts +21 -4
- package/dist/types/overlays/session-observer-registry.d.ts +2 -0
- package/dist/types/overlays/usage-dashboard.d.ts +3 -1
- package/dist/types/status-line/types.d.ts +3 -3
- package/dist/types/terminal-capabilities.d.ts +24 -32
- package/dist/types/terminal-multiplexer.d.ts +127 -14
- package/dist/types/terminal.d.ts +5 -4
- package/dist/types/theme/symbols.d.ts +1 -1
- package/dist/types/tmux.d.ts +1 -4
- package/dist/types/tools/eval.d.ts +1 -1
- package/dist/types/tools/native-view.d.ts +47 -0
- package/dist/types/tools/renderer.d.ts +15 -2
- package/dist/types/tui.d.ts +20 -0
- package/dist/types/vim.d.ts +19 -5
- package/package.json +9 -9
- package/src/charts/chart-plan.ts +152 -12
- package/src/charts/chart-svg.ts +479 -126
- package/src/charts/table-data.ts +43 -8
- package/src/chat/execution-shared.ts +42 -25
- package/src/chat/svg-figure.ts +45 -8
- package/src/chat/tool-execution.ts +9 -2
- package/src/components/editor.ts +182 -5
- package/src/keybinding-matchers.ts +6 -2
- package/src/kitty-graphics.ts +4 -4
- package/src/multiplexers/cmux.ts +29 -0
- package/src/multiplexers/herdr.ts +64 -0
- package/src/multiplexers/notify.ts +24 -0
- package/src/multiplexers/orca.ts +16 -0
- package/src/multiplexers/screen.ts +15 -0
- package/src/multiplexers/tmux.ts +40 -0
- package/src/multiplexers/types.ts +110 -0
- package/src/multiplexers/wmux.ts +16 -0
- package/src/multiplexers/zellij.ts +24 -0
- package/src/overlays/agent-hub.ts +7 -3
- package/src/overlays/annotation-overlay.ts +87 -6
- package/src/overlays/annotation-types.ts +2 -0
- package/src/overlays/hook-selector.ts +12 -5
- package/src/overlays/model-browser.ts +3 -3
- package/src/overlays/model-hub.ts +107 -27
- package/src/overlays/session-observer-registry.ts +10 -2
- package/src/overlays/usage-dashboard.ts +70 -23
- package/src/prompt/composer.ts +1 -0
- package/src/status-line/component.ts +12 -8
- package/src/status-line/segments.ts +10 -11
- package/src/status-line/types.ts +3 -3
- package/src/terminal-capabilities.ts +65 -181
- package/src/terminal-multiplexer.ts +112 -33
- package/src/terminal.ts +34 -28
- package/src/theme/session-color.ts +1 -7
- package/src/theme/symbols.ts +5 -0
- package/src/theme/theme.ts +7 -4
- package/src/tmux.ts +4 -11
- package/src/tools/bash.ts +82 -31
- package/src/tools/eval.ts +149 -123
- package/src/tools/native-view.ts +102 -1
- package/src/tools/renderer.ts +15 -2
- package/src/tui.ts +86 -12
- package/src/vim.ts +333 -18
package/src/charts/chart-svg.ts
CHANGED
|
@@ -6,10 +6,12 @@
|
|
|
6
6
|
* so the host resolves them against the live theme (`prepareSvg`) for both
|
|
7
7
|
* the rasterized terminal image and the native TSP blob.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* and
|
|
12
|
-
*
|
|
9
|
+
* Styled after Apple's charts and Tern's UI: a UI sans (Geist where Tern
|
|
10
|
+
* provides it), muted labels, hairline gridlines that recede behind the
|
|
11
|
+
* marks, bars and cells with rounded corners, smooth lines over a soft area
|
|
12
|
+
* fill, dot legends. 13-unit labels land near the terminal's own glyph size
|
|
13
|
+
* at 16 user units per row; bolded table cells draw at full strength while
|
|
14
|
+
* their neighbours recede.
|
|
13
15
|
*/
|
|
14
16
|
import type { ChartPoint, ChartSeries, ChartSpec } from "./chart-plan";
|
|
15
17
|
import type { Dimension } from "./table-data";
|
|
@@ -18,16 +20,29 @@ const WIDTH = 720;
|
|
|
18
20
|
const PAD = 12;
|
|
19
21
|
const LABEL_SIZE = 13;
|
|
20
22
|
const TICK_SIZE = 11;
|
|
21
|
-
/**
|
|
22
|
-
const ADVANCE = 0.
|
|
23
|
-
const FONT =
|
|
23
|
+
/** Mean advance per em of the UI sans, for character budgets; {@link measure} estimates real widths. */
|
|
24
|
+
const ADVANCE = 0.55;
|
|
25
|
+
const FONT =
|
|
26
|
+
"Geist, -apple-system, system-ui, 'SF Pro Text', 'Helvetica Neue', Inter, 'Segoe UI', Roboto, 'Noto Sans', 'DejaVu Sans', Arial, sans-serif";
|
|
24
27
|
/** Series colors in draw order. */
|
|
25
|
-
const SERIES = ["
|
|
28
|
+
const SERIES = ["c1", "c2", "c3", "c4", "c5", "c6"] as const;
|
|
29
|
+
/** Corner radius of bars, tracks and cells. */
|
|
30
|
+
const RADIUS = 3;
|
|
31
|
+
/** Opacity of gridlines over the border color: hairlines behind the marks. */
|
|
32
|
+
const GRID_OPACITY = 0.5;
|
|
33
|
+
/** Ratio between line series' peaks from which they stop sharing an axis. */
|
|
34
|
+
const SPLIT_SPREAD = 8;
|
|
35
|
+
/** Most categories whose every sample draws as a dot on its line. */
|
|
36
|
+
const MAX_DOTS = 16;
|
|
26
37
|
/** Spread (max/min of positive values) from which an axis turns logarithmic. */
|
|
27
38
|
const LOG_SPREAD = 100;
|
|
28
39
|
const MAX_LABEL_CHARS = 28;
|
|
29
40
|
/** Opacity of marks beside an emphasized one. */
|
|
30
41
|
const RECEDE = 0.45;
|
|
42
|
+
/** Most panels across one band of small multiples drawn as panels. */
|
|
43
|
+
const PANELS_ACROSS = 3;
|
|
44
|
+
/** Factors a change axis rounds its ends out to; past the last, whole powers of ten. */
|
|
45
|
+
const FACTOR_STOPS = [1.25, 1.5, 2, 3, 5, 10];
|
|
31
46
|
|
|
32
47
|
/** A drawn layer: its SVG elements and the height they span from `top`. */
|
|
33
48
|
interface Layer {
|
|
@@ -75,7 +90,8 @@ export function renderChartSvg(spec: ChartSpec): ChartSvg {
|
|
|
75
90
|
export function chartAlt(spec: ChartSpec): string {
|
|
76
91
|
const names = spec.series.map(entry => entry.name).join(", ");
|
|
77
92
|
const kind = spec.kind === "multiples" ? "small multiples" : spec.kind;
|
|
78
|
-
|
|
93
|
+
const versus = spec.baseline ? ` vs ${spec.baseline}` : "";
|
|
94
|
+
return `${kind} chart of ${names}${versus}${spec.axis ? ` by ${spec.axis}` : ""}`;
|
|
79
95
|
}
|
|
80
96
|
|
|
81
97
|
function drawChart(spec: ChartSpec): Layer {
|
|
@@ -85,7 +101,12 @@ function drawChart(spec: ChartSpec): Layer {
|
|
|
85
101
|
case "heatmap":
|
|
86
102
|
return drawGrid(spec, "heat");
|
|
87
103
|
case "multiples":
|
|
88
|
-
|
|
104
|
+
// Many panels of a few colorable categories wrap into bands; otherwise one column per panel.
|
|
105
|
+
return spec.series.length > PANELS_ACROSS && spec.categories.length <= SERIES.length
|
|
106
|
+
? drawPanels(spec)
|
|
107
|
+
: drawGrid(spec, "bars");
|
|
108
|
+
case "change":
|
|
109
|
+
return drawChange(spec);
|
|
89
110
|
case "share":
|
|
90
111
|
return drawShare(spec);
|
|
91
112
|
case "scatter":
|
|
@@ -95,13 +116,18 @@ function drawChart(spec: ChartSpec): Layer {
|
|
|
95
116
|
}
|
|
96
117
|
}
|
|
97
118
|
|
|
98
|
-
// ── Horizontal bars: bar, grouped, paired, diverging
|
|
119
|
+
// ── Horizontal bars: bar, grouped, paired, diverging, progress ───────────────
|
|
99
120
|
|
|
100
121
|
function drawBars(spec: ChartSpec): Layer {
|
|
101
122
|
const { kind, categories, series } = spec;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
);
|
|
123
|
+
// Progress fills a 0–100% track per bar; grouped and multi-track progress stack thin bars per category.
|
|
124
|
+
const tracks = kind === "progress";
|
|
125
|
+
const grouped = kind === "grouped" || (tracks && series.length > 1);
|
|
126
|
+
const values = tracks
|
|
127
|
+
? [0, 100]
|
|
128
|
+
: series.flatMap(entry =>
|
|
129
|
+
entry.points.flatMap(point => (point ? [point.value, point.upper ?? point.value] : [])),
|
|
130
|
+
);
|
|
105
131
|
const labelWidth = labelColumnWidth(categories);
|
|
106
132
|
const valueText = (at: number) =>
|
|
107
133
|
kind === "paired"
|
|
@@ -129,7 +155,7 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
129
155
|
parts.push(legend.body);
|
|
130
156
|
top += legend.height + 6;
|
|
131
157
|
}
|
|
132
|
-
const groupRow =
|
|
158
|
+
const groupRow = grouped ? series.length * 11 + 8 : 22;
|
|
133
159
|
const gridTop = top;
|
|
134
160
|
const gridBottom = top + categories.length * groupRow;
|
|
135
161
|
parts.push(drawValueAxis(scale, series[0]!, gridTop, gridBottom));
|
|
@@ -139,10 +165,10 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
139
165
|
categories.forEach((name, at) => {
|
|
140
166
|
const rowTop = top + at * groupRow;
|
|
141
167
|
const middle = rowTop + groupRow / 2;
|
|
142
|
-
parts.push(
|
|
168
|
+
parts.push(categoryLabel(name, middle));
|
|
143
169
|
if (kind === "paired") {
|
|
144
170
|
const [a, b] = [series[0]!.points[at], series[1]!.points[at]];
|
|
145
|
-
if (a && b) parts.push(line(scale.at(a.value), middle, scale.at(b.value), middle, "border",
|
|
171
|
+
if (a && b) parts.push(line(scale.at(a.value), middle, scale.at(b.value), middle, "border", 4, 0.6));
|
|
146
172
|
if (a) parts.push(dot(scale.at(a.value), middle, 4.5, seriesColor(kind, 0)));
|
|
147
173
|
if (b)
|
|
148
174
|
parts.push(dot(scale.at(b.value), middle, 4.5, seriesColor(kind, 1), a?.emphasis || b.emphasis ? 1 : 0.9));
|
|
@@ -154,12 +180,13 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
154
180
|
);
|
|
155
181
|
return;
|
|
156
182
|
}
|
|
157
|
-
const barHeight =
|
|
183
|
+
const barHeight = grouped ? 9 : 14;
|
|
158
184
|
series.forEach((entry, index) => {
|
|
159
185
|
const point = entry.points[at];
|
|
160
|
-
const y =
|
|
186
|
+
const y = grouped ? rowTop + 4 + index * 11 : middle - barHeight / 2;
|
|
161
187
|
const fill = kind === "diverging" && point && point.value < 0 ? "c2" : seriesColor(kind, index);
|
|
162
188
|
const opacity = emphasized && !point?.emphasis ? RECEDE : 1;
|
|
189
|
+
if (tracks) parts.push(rect(left, y, scale.at(100) - left, barHeight, "border", 0.35));
|
|
163
190
|
if (!point) {
|
|
164
191
|
const note = entry.notes[at];
|
|
165
192
|
if (note)
|
|
@@ -168,14 +195,16 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
168
195
|
}
|
|
169
196
|
const end = scale.at(point.value);
|
|
170
197
|
if (scale.log) {
|
|
171
|
-
parts.push(
|
|
198
|
+
parts.push(rule(left, y + barHeight / 2, end, y + barHeight / 2));
|
|
172
199
|
parts.push(dot(end, y + barHeight / 2, barHeight / 2.8, fill, opacity));
|
|
173
200
|
} else {
|
|
174
201
|
parts.push(rect(Math.min(zero, end), y, Math.abs(end - zero), barHeight, fill, opacity));
|
|
175
202
|
}
|
|
176
203
|
if (point.upper !== undefined) parts.push(whisker(end, scale.at(point.upper), y + barHeight / 2, fill));
|
|
177
|
-
if (
|
|
178
|
-
const labelX =
|
|
204
|
+
if (grouped) {
|
|
205
|
+
const labelX = tracks
|
|
206
|
+
? scale.at(100) + 4
|
|
207
|
+
: Math.max(end, point.upper === undefined ? end : scale.at(point.upper)) + 4;
|
|
179
208
|
parts.push(text(labelX, y + barHeight / 2, clip(point.text, 12), { size: 10, fill: "muted" }));
|
|
180
209
|
}
|
|
181
210
|
});
|
|
@@ -185,7 +214,7 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
185
214
|
? Math.max(scale.at(point.value), point.upper === undefined ? 0 : scale.at(point.upper))
|
|
186
215
|
: left;
|
|
187
216
|
// Clear the dot a log axis draws instead of a bar.
|
|
188
|
-
const x = kind === "diverging" ? right + 8 : Math.max(end, zero) + (scale.log ? 11 : 6);
|
|
217
|
+
const x = kind === "diverging" || tracks ? right + 8 : Math.max(end, zero) + (scale.log ? 11 : 6);
|
|
189
218
|
parts.push(
|
|
190
219
|
text(x, middle, valueText(at), {
|
|
191
220
|
size: TICK_SIZE,
|
|
@@ -199,9 +228,9 @@ function drawBars(spec: ChartSpec): Layer {
|
|
|
199
228
|
return { height: gridBottom + 20, body: parts.join("") };
|
|
200
229
|
}
|
|
201
230
|
|
|
202
|
-
/** Series color: dumbbells pair a muted "before" with the
|
|
231
|
+
/** Series color: dumbbells pair a muted "before" with the lead series color "after". */
|
|
203
232
|
function seriesColor(kind: ChartSpec["kind"], index: number): string {
|
|
204
|
-
if (kind === "paired") return index === 0 ? "muted" :
|
|
233
|
+
if (kind === "paired") return index === 0 ? "muted" : SERIES[0];
|
|
205
234
|
return SERIES[index % SERIES.length]!;
|
|
206
235
|
}
|
|
207
236
|
|
|
@@ -227,7 +256,7 @@ function drawGrid(spec: ChartSpec, style: "heat" | "bars"): Layer {
|
|
|
227
256
|
const shared = intensity(all);
|
|
228
257
|
categories.forEach((name, row) => {
|
|
229
258
|
const y = top + row * rowHeight;
|
|
230
|
-
parts.push(
|
|
259
|
+
parts.push(categoryLabel(name, y + rowHeight / 2));
|
|
231
260
|
});
|
|
232
261
|
series.forEach((entry, index) => {
|
|
233
262
|
const x = left + index * cellWidth;
|
|
@@ -248,7 +277,7 @@ function drawGrid(spec: ChartSpec, style: "heat" | "bars"): Layer {
|
|
|
248
277
|
if (style === "heat") {
|
|
249
278
|
// Capped so the foreground-colored value stays legible on the strongest cell.
|
|
250
279
|
const level = shared(point.value);
|
|
251
|
-
parts.push(rect(x + 1, y + 1, cellWidth - 2, rowHeight - 2,
|
|
280
|
+
parts.push(rect(x + 1, y + 1, cellWidth - 2, rowHeight - 2, SERIES[0], 0.08 + 0.57 * level));
|
|
252
281
|
parts.push(
|
|
253
282
|
text(x + cellWidth / 2, middle, clip(point.text, headerChars), {
|
|
254
283
|
size: TICK_SIZE,
|
|
@@ -273,12 +302,62 @@ function drawGrid(spec: ChartSpec, style: "heat" | "bars"): Layer {
|
|
|
273
302
|
if (style === "bars") {
|
|
274
303
|
for (let index = 1; index < series.length; index++) {
|
|
275
304
|
const x = left + index * cellWidth - 3;
|
|
276
|
-
parts.push(
|
|
305
|
+
parts.push(rule(x, top, x, top + categories.length * rowHeight));
|
|
277
306
|
}
|
|
278
307
|
}
|
|
279
308
|
return { height: top + categories.length * rowHeight + 4, body: parts.join("") };
|
|
280
309
|
}
|
|
281
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Small multiples as bands of panels, one per series, each scaling its own
|
|
313
|
+
* bars; the few categories are told apart by color through a legend. Fits a
|
|
314
|
+
* table whose rows are metrics: each metric gets a panel comparing the columns.
|
|
315
|
+
*/
|
|
316
|
+
function drawPanels(spec: ChartSpec): Layer {
|
|
317
|
+
const { categories, series } = spec;
|
|
318
|
+
const legend = drawLegend(
|
|
319
|
+
categories.map((name, at) => [name, SERIES[at % SERIES.length]!]),
|
|
320
|
+
PAD,
|
|
321
|
+
WIDTH - PAD,
|
|
322
|
+
);
|
|
323
|
+
const parts = [legend.body];
|
|
324
|
+
const bands = Math.ceil(series.length / PANELS_ACROSS);
|
|
325
|
+
const across = Math.ceil(series.length / bands);
|
|
326
|
+
const gap = 18;
|
|
327
|
+
const panelWidth = (WIDTH - 2 * PAD - gap * (across - 1)) / across;
|
|
328
|
+
const panelHeight = 20 + categories.length * 14 + 12;
|
|
329
|
+
const top = legend.height + 10;
|
|
330
|
+
const titleChars = Math.floor(panelWidth / (TICK_SIZE * ADVANCE));
|
|
331
|
+
series.forEach((entry, index) => {
|
|
332
|
+
const x = PAD + (index % across) * (panelWidth + gap);
|
|
333
|
+
const y = top + Math.floor(index / across) * panelHeight;
|
|
334
|
+
parts.push(text(x, y + 6, clip(entry.name, titleChars), { size: TICK_SIZE, fill: "fg", weight: 600 }));
|
|
335
|
+
const scale = intensity(entry.points.flatMap(point => (point ? [point.value] : [])));
|
|
336
|
+
const textWidth = Math.max(0, ...entry.points.map((_, at) => measure(pointText(entry, at), 10)));
|
|
337
|
+
const barSpace = Math.max(0, panelWidth - textWidth - 6);
|
|
338
|
+
const emphasized = entry.points.some(point => point?.emphasis);
|
|
339
|
+
entry.points.forEach((point, at) => {
|
|
340
|
+
const middle = y + 27 + at * 14;
|
|
341
|
+
if (!point) {
|
|
342
|
+
const note = entry.notes[at];
|
|
343
|
+
if (note) parts.push(text(x, middle, clip(note, titleChars), { size: 10, fill: "warning" }));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const width = Math.max(1, scale(point.value) * barSpace);
|
|
347
|
+
const color = SERIES[at % SERIES.length]!;
|
|
348
|
+
parts.push(rect(x, middle - 5, width, 10, color, emphasized && !point.emphasis ? RECEDE : 1));
|
|
349
|
+
parts.push(
|
|
350
|
+
text(x + width + 4, middle, pointText(entry, at), {
|
|
351
|
+
size: 10,
|
|
352
|
+
fill: point.emphasis ? "fg" : "muted",
|
|
353
|
+
weight: point.emphasis ? 600 : undefined,
|
|
354
|
+
}),
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
return { height: top + bands * panelHeight - 8, body: parts.join("") };
|
|
359
|
+
}
|
|
360
|
+
|
|
282
361
|
/**
|
|
283
362
|
* Map values onto 0–1 for bar length or fill strength: from zero for
|
|
284
363
|
* non-negative values (log beyond {@link LOG_SPREAD}), min–max otherwise.
|
|
@@ -296,6 +375,121 @@ function intensity(values: readonly number[]): (value: number) => number {
|
|
|
296
375
|
return value => (value - min) / (max - min);
|
|
297
376
|
}
|
|
298
377
|
|
|
378
|
+
// ── Change against a baseline ───────────────────────────────────────────────
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Factors on a log axis through 1× (the baseline): each metric's bar runs from
|
|
382
|
+
* 1× to its factor, so halving and doubling draw equally long whatever the
|
|
383
|
+
* metric's unit. One series prints the factor and both figures beside its bar.
|
|
384
|
+
*/
|
|
385
|
+
function drawChange(spec: ChartSpec): Layer {
|
|
386
|
+
const { categories, series } = spec;
|
|
387
|
+
const single = series.length === 1;
|
|
388
|
+
const points = series.flatMap(entry => entry.points.filter(point => point !== null));
|
|
389
|
+
const logs = points.flatMap(point => (point.value > 0 ? [Math.log10(point.value)] : []));
|
|
390
|
+
let low = -roundFactor(-Math.min(0, ...logs));
|
|
391
|
+
let high = roundFactor(Math.max(0, ...logs));
|
|
392
|
+
// A drop to zero has no factor; its bar runs to the axis end.
|
|
393
|
+
if (points.some(point => point.value === 0)) low = Math.min(low, -Math.log10(2));
|
|
394
|
+
if (low === high) high = Math.log10(FACTOR_STOPS[0]!);
|
|
395
|
+
|
|
396
|
+
const parts: string[] = [];
|
|
397
|
+
let top = 0;
|
|
398
|
+
if (!single) {
|
|
399
|
+
const legend = drawLegend(
|
|
400
|
+
[
|
|
401
|
+
...series.map((entry, at): [string, string] => [entry.name, SERIES[at % SERIES.length]!]),
|
|
402
|
+
[spec.baseline ?? "baseline", "muted"],
|
|
403
|
+
],
|
|
404
|
+
PAD,
|
|
405
|
+
WIDTH - PAD,
|
|
406
|
+
);
|
|
407
|
+
parts.push(legend.body);
|
|
408
|
+
top += legend.height + 6;
|
|
409
|
+
}
|
|
410
|
+
const factors = categories.map((_, at) => (single ? factorText(series[0]!.points[at]) : ""));
|
|
411
|
+
const factorWidth = Math.max(0, ...factors.map(label => measure(label, TICK_SIZE)));
|
|
412
|
+
const figures = categories.map((_, at) => (single ? (series[0]!.points[at]?.text ?? "") : ""));
|
|
413
|
+
const figureWidth = Math.max(0, ...figures.map(label => measure(label, TICK_SIZE)));
|
|
414
|
+
const valueWidth = single
|
|
415
|
+
? Math.min(280, factorWidth + 10 + figureWidth) + 8
|
|
416
|
+
: Math.max(0, ...points.map(point => measure(factorText(point), 10))) + 8;
|
|
417
|
+
const left = PAD + labelColumnWidth(categories) + 10;
|
|
418
|
+
const right = WIDTH - PAD - valueWidth;
|
|
419
|
+
const x = (log: number) => left + ((log - low) / (high - low)) * (right - left);
|
|
420
|
+
const rowHeight = single ? 22 : series.length * 11 + 8;
|
|
421
|
+
const barHeight = single ? 14 : 9;
|
|
422
|
+
const gridBottom = top + categories.length * rowHeight;
|
|
423
|
+
|
|
424
|
+
// 1× always; the ends and whole decades between wherever their labels stay clear of the rest.
|
|
425
|
+
const decade = (right - left) / (high - low);
|
|
426
|
+
const ticks = [0];
|
|
427
|
+
for (const end of [low, high]) if (Math.abs(end) * decade >= 40) ticks.push(end);
|
|
428
|
+
for (let power = Math.ceil(low); power <= Math.floor(high); power++) {
|
|
429
|
+
if (ticks.every(tick => Math.abs(power - tick) * decade >= 40)) ticks.push(power);
|
|
430
|
+
}
|
|
431
|
+
for (const tick of new Set(ticks)) {
|
|
432
|
+
parts.push(rule(x(tick), top, x(tick), gridBottom));
|
|
433
|
+
const label = tick === 0 ? "1×" : `${tick < 0 ? "÷" : "×"}${compactFactor(10 ** Math.abs(tick))}`;
|
|
434
|
+
parts.push(text(x(tick), gridBottom + 12, label, { size: TICK_SIZE, fill: "muted", anchor: "middle" }));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const emphasized = points.some(point => point.emphasis);
|
|
438
|
+
// The epsilon keeps a figure that exactly fits from clipping on float error.
|
|
439
|
+
const figureChars = Math.floor((valueWidth - 18 - factorWidth) / (TICK_SIZE * ADVANCE) + 1e-6);
|
|
440
|
+
categories.forEach((name, row) => {
|
|
441
|
+
const rowTop = top + row * rowHeight;
|
|
442
|
+
const middle = rowTop + rowHeight / 2;
|
|
443
|
+
parts.push(categoryLabel(name, middle));
|
|
444
|
+
series.forEach((entry, index) => {
|
|
445
|
+
const point = entry.points[row];
|
|
446
|
+
if (!point) return;
|
|
447
|
+
const y = single ? middle - barHeight / 2 : rowTop + 4 + index * 11;
|
|
448
|
+
const end = x(point.value > 0 ? Math.log10(point.value) : low);
|
|
449
|
+
const color = SERIES[single ? 0 : index % SERIES.length]!;
|
|
450
|
+
const opacity = emphasized && !point.emphasis ? RECEDE : 1;
|
|
451
|
+
parts.push(rect(Math.min(x(0), end), y, Math.max(1, Math.abs(end - x(0))), barHeight, color, opacity));
|
|
452
|
+
if (!single) parts.push(text(right + 8, y + barHeight / 2, factorText(point), { size: 10, fill: "muted" }));
|
|
453
|
+
});
|
|
454
|
+
if (single) {
|
|
455
|
+
const strong = series[0]!.points[row]?.emphasis ? 600 : undefined;
|
|
456
|
+
parts.push(text(right + 8, middle, factors[row]!, { size: TICK_SIZE, fill: "fg", weight: strong }));
|
|
457
|
+
parts.push(
|
|
458
|
+
text(right + 18 + factorWidth, middle, clip(figures[row]!, figureChars), {
|
|
459
|
+
size: TICK_SIZE,
|
|
460
|
+
fill: "muted",
|
|
461
|
+
}),
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
parts.push(line(x(0), top - 2, x(0), gridBottom + 2, "muted", 1.5));
|
|
466
|
+
return { height: gridBottom + 20, body: parts.join("") };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** `log10` of the first {@link FACTOR_STOPS} entry (else power of ten) at or past `10^log`; 0 stays 0. */
|
|
470
|
+
function roundFactor(log: number): number {
|
|
471
|
+
if (log <= 0) return 0;
|
|
472
|
+
const stop = FACTOR_STOPS.find(factor => Math.log10(factor) >= log - 1e-9);
|
|
473
|
+
return stop === undefined ? Math.ceil(log - 1e-9) : Math.log10(stop);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** A factor as people say it: `48× less`, `2.1× more`, or a percent within 2× (`−12%`, `+27%`). */
|
|
477
|
+
function factorText(point: ChartPoint | null | undefined): string {
|
|
478
|
+
if (!point) return "";
|
|
479
|
+
const factor = point.value;
|
|
480
|
+
if (factor === 0) return "−100%";
|
|
481
|
+
if (factor >= 2) return `${compactFactor(factor)}× more`;
|
|
482
|
+
if (factor <= 0.5) return `${compactFactor(1 / factor)}× less`;
|
|
483
|
+
const percent = Math.round((factor - 1) * 100);
|
|
484
|
+
return percent === 0 ? "same" : `${percent > 0 ? "+" : "−"}${Math.abs(percent)}%`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** `48`, `2.1`, `1.25`, `35k`: whole above 10, one decimal above 2, two below. */
|
|
488
|
+
function compactFactor(factor: number): string {
|
|
489
|
+
if (factor >= 10) return compact(Math.round(factor));
|
|
490
|
+
return Number(factor.toFixed(factor >= 2 ? 1 : 2)).toString();
|
|
491
|
+
}
|
|
492
|
+
|
|
299
493
|
// ── Share of a whole ─────────────────────────────────────────────────────────
|
|
300
494
|
|
|
301
495
|
function drawShare(spec: ChartSpec): Layer {
|
|
@@ -307,8 +501,8 @@ function drawShare(spec: ChartSpec): Layer {
|
|
|
307
501
|
entry.points.forEach((point, at) => {
|
|
308
502
|
if (!point || point.value <= 0) return;
|
|
309
503
|
const width = (point.value / total) * barWidth;
|
|
310
|
-
|
|
311
|
-
|
|
504
|
+
// Segments part with a sliver of the surface behind them.
|
|
505
|
+
parts.push(rect(x, 1, Math.max(1, width - 2), 18, SERIES[at % SERIES.length]!, 1));
|
|
312
506
|
x += width;
|
|
313
507
|
});
|
|
314
508
|
const columns = spec.categories.length > 5 ? 2 : 1;
|
|
@@ -320,7 +514,7 @@ function drawShare(spec: ChartSpec): Layer {
|
|
|
320
514
|
const lx = PAD + column * columnWidth;
|
|
321
515
|
const ly = 34 + (at % perColumn) * 20;
|
|
322
516
|
const point = entry.points[at];
|
|
323
|
-
parts.push(
|
|
517
|
+
parts.push(dot(lx + 5, ly, 4.5, SERIES[at % SERIES.length]!));
|
|
324
518
|
parts.push(text(lx + 16, ly, clip(name, chars), { fill: "fg" }));
|
|
325
519
|
parts.push(
|
|
326
520
|
text(lx + columnWidth - 12, ly, point?.text ?? "", {
|
|
@@ -336,56 +530,126 @@ function drawShare(spec: ChartSpec): Layer {
|
|
|
336
530
|
|
|
337
531
|
// ── Line over an ordered axis ────────────────────────────────────────────────
|
|
338
532
|
|
|
533
|
+
/** A line's value label at its end, nudged clear of its neighbours by {@link spreadLabels}. */
|
|
534
|
+
interface EndLabel {
|
|
535
|
+
readonly x: number;
|
|
536
|
+
y: number;
|
|
537
|
+
readonly text: string;
|
|
538
|
+
readonly color: string;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Lines across the categories (or the numeric x), smoothed without
|
|
543
|
+
* overshooting a sample. Series whose peaks differ {@link SPLIT_SPREAD}× or
|
|
544
|
+
* more stack in panels on their own axes, so one large series never flattens
|
|
545
|
+
* the rest; the panels share the x labels under the last. A lone line draws
|
|
546
|
+
* over a soft area fill, and every line ends in its value.
|
|
547
|
+
*/
|
|
339
548
|
function drawLine(spec: ChartSpec): Layer {
|
|
340
549
|
const { categories, series, x } = spec;
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
series.map((entry, at) => [entry.name, SERIES[at % SERIES.length]!]),
|
|
346
|
-
PAD,
|
|
347
|
-
WIDTH - PAD,
|
|
348
|
-
);
|
|
349
|
-
parts.push(legend.body);
|
|
350
|
-
top += legend.height + 6;
|
|
351
|
-
}
|
|
352
|
-
const values = series.flatMap(entry => entry.points.flatMap(point => (point ? [point.value] : [])));
|
|
353
|
-
const lastLabels = series.flatMap(entry => entry.points.map(point => point?.text ?? ""));
|
|
550
|
+
const panels = magnitudePanels(series);
|
|
551
|
+
const values = panels.map(panel =>
|
|
552
|
+
panel.flatMap(index => series[index]!.points.flatMap(point => (point ? [point.value] : []))),
|
|
553
|
+
);
|
|
354
554
|
const tickWidth = Math.max(
|
|
355
|
-
...
|
|
356
|
-
|
|
357
|
-
|
|
555
|
+
...values.flatMap(panelValues => {
|
|
556
|
+
const scale = valueScale(panelValues, 0, 1, { zero: false });
|
|
557
|
+
const format = tickFormat(scale, series[0]!);
|
|
558
|
+
return scale.ticks.map(tick => measure(format(tick), TICK_SIZE));
|
|
559
|
+
}),
|
|
560
|
+
);
|
|
561
|
+
const position = (at: number) => (x ? x.values[at]! : at);
|
|
562
|
+
const order = categories.map((_, at) => at).sort((a, b) => position(a) - position(b));
|
|
563
|
+
// The rightmost sample of each line carries its value label.
|
|
564
|
+
const lasts = series.map(entry => order.findLast(at => entry.points[at]) ?? -1);
|
|
565
|
+
const endWidth = Math.max(
|
|
566
|
+
0,
|
|
567
|
+
...lasts.map((last, index) => measure(series[index]!.points[last]?.text ?? "", TICK_SIZE)),
|
|
358
568
|
);
|
|
359
569
|
const left = PAD + tickWidth + 8;
|
|
360
|
-
const right = WIDTH - PAD - Math.min(120,
|
|
361
|
-
const plotTop = top + 6;
|
|
362
|
-
const plotBottom = plotTop + 180;
|
|
363
|
-
const yScale = valueScale(values, plotBottom, plotTop, { zero: false });
|
|
570
|
+
const right = WIDTH - PAD - Math.min(120, endWidth + 10);
|
|
364
571
|
const xScale = x
|
|
365
572
|
? valueScale(x.values, left, right, { zero: false })
|
|
366
573
|
: {
|
|
367
574
|
at: (index: number) =>
|
|
368
575
|
left + (categories.length === 1 ? 0 : (index / (categories.length - 1)) * (right - left)),
|
|
369
|
-
ticks: [],
|
|
370
|
-
log: false,
|
|
371
576
|
};
|
|
372
|
-
const xAt = (
|
|
577
|
+
const xAt = (at: number) => xScale.at(position(at));
|
|
578
|
+
const plotHeight = panels.length === 1 ? 180 : Math.max(72, Math.round(200 / panels.length));
|
|
579
|
+
const dotted = categories.length <= MAX_DOTS;
|
|
580
|
+
|
|
581
|
+
const parts: string[] = [];
|
|
582
|
+
let top = 0;
|
|
583
|
+
let plotBottom = 0;
|
|
584
|
+
panels.forEach((panel, panelIndex) => {
|
|
585
|
+
// Several lines need names, and so does a panel of one beside others.
|
|
586
|
+
if (series.length > 1) {
|
|
587
|
+
const legend = drawLegend(
|
|
588
|
+
panel.map(index => [series[index]!.name, SERIES[index % SERIES.length]!]),
|
|
589
|
+
PAD,
|
|
590
|
+
WIDTH - PAD,
|
|
591
|
+
);
|
|
592
|
+
parts.push(`<g transform="translate(0 ${num(top)})">${legend.body}</g>`);
|
|
593
|
+
top += legend.height + 4;
|
|
594
|
+
}
|
|
595
|
+
const plotTop = top + 6;
|
|
596
|
+
plotBottom = plotTop + plotHeight;
|
|
597
|
+
const scale = valueScale(values[panelIndex]!, plotBottom, plotTop, { zero: false });
|
|
598
|
+
const format = tickFormat(scale, series[0]!);
|
|
599
|
+
// Short panels label every other gridline.
|
|
600
|
+
const labelEvery = plotHeight / Math.max(1, scale.ticks.length - 1) < 20 ? 2 : 1;
|
|
601
|
+
scale.ticks.forEach((tick, at) => {
|
|
602
|
+
const y = scale.at(tick);
|
|
603
|
+
parts.push(rule(left, y, right, y));
|
|
604
|
+
if (at % labelEvery === 0)
|
|
605
|
+
parts.push(text(left - 8, y, format(tick), { size: TICK_SIZE, fill: "muted", anchor: "end" }));
|
|
606
|
+
});
|
|
607
|
+
const ends: EndLabel[] = [];
|
|
608
|
+
for (const index of panel) {
|
|
609
|
+
const entry = series[index]!;
|
|
610
|
+
const color = SERIES[index % SERIES.length]!;
|
|
611
|
+
// Runs of consecutive samples; a gap in the table breaks the line.
|
|
612
|
+
const runs: [number, number][][] = [[]];
|
|
613
|
+
for (const at of order) {
|
|
614
|
+
const point = entry.points[at];
|
|
615
|
+
if (point) runs.at(-1)!.push([xAt(at), scale.at(point.value)]);
|
|
616
|
+
else if (runs.at(-1)!.length > 0) runs.push([]);
|
|
617
|
+
}
|
|
618
|
+
if (panel.length === 1) {
|
|
619
|
+
// The fade's id names its color, so two charts inlined in one page agree on it.
|
|
620
|
+
parts.push(
|
|
621
|
+
`<defs><linearGradient id="fade-${color}" x1="0" y1="0" x2="0" y2="1">`,
|
|
622
|
+
`<stop offset="0" stop-color="${token(color)}" stop-opacity="0.26"/>`,
|
|
623
|
+
`<stop offset="1" stop-color="${token(color)}" stop-opacity="0"/>`,
|
|
624
|
+
"</linearGradient></defs>",
|
|
625
|
+
);
|
|
626
|
+
for (const run of runs) {
|
|
627
|
+
if (run.length < 2) continue;
|
|
628
|
+
const floor = `L${num(run.at(-1)![0])} ${num(plotBottom)}L${num(run[0]![0])} ${num(plotBottom)}Z`;
|
|
629
|
+
parts.push(`<path d="${smoothPath(run)}${floor}" fill="url(#fade-${color})"/>`);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
for (const run of runs) {
|
|
633
|
+
if (run.length < 2) continue;
|
|
634
|
+
parts.push(
|
|
635
|
+
`<path d="${smoothPath(run)}" fill="none" stroke="${token(color)}" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round"/>`,
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
const last = lasts[index]!;
|
|
639
|
+
entry.points.forEach((point, at) => {
|
|
640
|
+
if (!point || !(dotted || point.emphasis || at === last)) return;
|
|
641
|
+
parts.push(dot(xAt(at), scale.at(point.value), point.emphasis ? 4.5 : at === last ? 3.5 : 2.5, color));
|
|
642
|
+
});
|
|
643
|
+
const end = entry.points[last];
|
|
644
|
+
if (end) ends.push({ x: xAt(last) + 8, y: scale.at(end.value), text: end.text, color });
|
|
645
|
+
}
|
|
646
|
+
parts.push(spreadLabels(ends, plotTop - 4, plotBottom + 4));
|
|
647
|
+
top = plotBottom + 22;
|
|
648
|
+
});
|
|
373
649
|
|
|
374
|
-
for (const tick of yScale.ticks) {
|
|
375
|
-
const y = yScale.at(tick);
|
|
376
|
-
parts.push(line(left, y, right, y, "border", 1));
|
|
377
|
-
parts.push(
|
|
378
|
-
text(left - 6, y, formatValue(tick, series[0]!.dim, series[0]!.unit), {
|
|
379
|
-
size: TICK_SIZE,
|
|
380
|
-
fill: "muted",
|
|
381
|
-
anchor: "end",
|
|
382
|
-
}),
|
|
383
|
-
);
|
|
384
|
-
}
|
|
385
650
|
// Category labels left to right, skipping any that would touch the last one drawn.
|
|
386
651
|
const slot = (right - left) / Math.max(1, categories.length - 1);
|
|
387
652
|
const labelChars = Math.max(3, Math.floor(Math.max(slot, 60) / (TICK_SIZE * ADVANCE)) - 1);
|
|
388
|
-
const order = categories.map((_, at) => at).sort((a, b) => xAt(a) - xAt(b));
|
|
389
653
|
let drawnEnd = Number.NEGATIVE_INFINITY;
|
|
390
654
|
for (const at of order) {
|
|
391
655
|
const label = clip(categories[at]!, labelChars);
|
|
@@ -395,33 +659,78 @@ function drawLine(spec: ChartSpec): Layer {
|
|
|
395
659
|
parts.push(text(center, plotBottom + 14, label, { size: TICK_SIZE, fill: "muted", anchor: "middle" }));
|
|
396
660
|
drawnEnd = center + width / 2;
|
|
397
661
|
}
|
|
398
|
-
series.forEach((entry, index) => {
|
|
399
|
-
const color = SERIES[index % SERIES.length]!;
|
|
400
|
-
let path = "";
|
|
401
|
-
let pen = false;
|
|
402
|
-
entry.points.forEach((point, at) => {
|
|
403
|
-
if (!point) {
|
|
404
|
-
pen = false;
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
path += `${pen ? "L" : "M"}${num(xAt(at))} ${num(yScale.at(point.value))}`;
|
|
408
|
-
pen = true;
|
|
409
|
-
});
|
|
410
|
-
parts.push(`<path d="${path}" fill="none" stroke="${token(color)}" stroke-width="2" stroke-linejoin="round"/>`);
|
|
411
|
-
entry.points.forEach((point, at) => {
|
|
412
|
-
if (point) parts.push(dot(xAt(at), yScale.at(point.value), point.emphasis ? 4.5 : 3, color));
|
|
413
|
-
});
|
|
414
|
-
// The rightmost point carries the series' value label.
|
|
415
|
-
let lastIndex = -1;
|
|
416
|
-
entry.points.forEach((point, at) => {
|
|
417
|
-
if (point && (lastIndex < 0 || xAt(at) > xAt(lastIndex))) lastIndex = at;
|
|
418
|
-
});
|
|
419
|
-
const last = entry.points[lastIndex];
|
|
420
|
-
if (last) parts.push(text(xAt(lastIndex) + 8, yScale.at(last.value), last.text, { size: TICK_SIZE, fill: "fg" }));
|
|
421
|
-
});
|
|
422
662
|
return { height: plotBottom + 24, body: parts.join("") };
|
|
423
663
|
}
|
|
424
664
|
|
|
665
|
+
/**
|
|
666
|
+
* Series indices grouped into panels whose peaks stay within
|
|
667
|
+
* {@link SPLIT_SPREAD}× of the panel's largest, panels and their series in
|
|
668
|
+
* table order. An all-zero series joins the smallest panel.
|
|
669
|
+
*/
|
|
670
|
+
function magnitudePanels(series: readonly ChartSeries[]): number[][] {
|
|
671
|
+
const peaks = series.map(entry => Math.max(0, ...entry.points.map(point => (point ? Math.abs(point.value) : 0))));
|
|
672
|
+
const panels: number[][] = [];
|
|
673
|
+
let panelPeak = 0;
|
|
674
|
+
for (const index of series.map((_, at) => at).sort((a, b) => peaks[b]! - peaks[a]!)) {
|
|
675
|
+
const peak = peaks[index]!;
|
|
676
|
+
if (panels.length > 0 && (peak === 0 || panelPeak < peak * SPLIT_SPREAD)) {
|
|
677
|
+
panels.at(-1)!.push(index);
|
|
678
|
+
} else {
|
|
679
|
+
panels.push([index]);
|
|
680
|
+
panelPeak = peak;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
for (const panel of panels) panel.sort((a, b) => a - b);
|
|
684
|
+
return panels.sort((a, b) => a[0]! - b[0]!);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* A path through `points` (x ascending) as a monotone cubic (Steffen's
|
|
689
|
+
* method): smooth, yet never overshooting a sample the way a Catmull-Rom
|
|
690
|
+
* curve would between a peak and a flat stretch.
|
|
691
|
+
*/
|
|
692
|
+
function smoothPath(points: readonly (readonly [number, number])[]): string {
|
|
693
|
+
const n = points.length;
|
|
694
|
+
let path = `M${num(points[0]![0])} ${num(points[0]![1])}`;
|
|
695
|
+
if (n < 3) return n === 2 ? `${path}L${num(points[1]![0])} ${num(points[1]![1])}` : path;
|
|
696
|
+
const secants: number[] = [];
|
|
697
|
+
for (let at = 0; at < n - 1; at++) {
|
|
698
|
+
const h = points[at + 1]![0] - points[at]![0];
|
|
699
|
+
secants.push(h === 0 ? 0 : (points[at + 1]![1] - points[at]![1]) / h);
|
|
700
|
+
}
|
|
701
|
+
const tangents: number[] = [0];
|
|
702
|
+
for (let at = 1; at < n - 1; at++) {
|
|
703
|
+
const h0 = points[at]![0] - points[at - 1]![0];
|
|
704
|
+
const h1 = points[at + 1]![0] - points[at]![0];
|
|
705
|
+
const [s0, s1] = [secants[at - 1]!, secants[at]!];
|
|
706
|
+
const weighted = (s0 * h1 + s1 * h0) / (h0 + h1 || 1);
|
|
707
|
+
tangents.push((Math.sign(s0) + Math.sign(s1)) * Math.min(Math.abs(s0), Math.abs(s1), 0.5 * Math.abs(weighted)));
|
|
708
|
+
}
|
|
709
|
+
tangents[0] = (3 * secants[0]! - tangents[1]!) / 2;
|
|
710
|
+
tangents.push((3 * secants[n - 2]! - tangents[n - 2]!) / 2);
|
|
711
|
+
for (let at = 0; at < n - 1; at++) {
|
|
712
|
+
const [x0, y0] = points[at]!;
|
|
713
|
+
const [x1, y1] = points[at + 1]!;
|
|
714
|
+
const third = (x1 - x0) / 3;
|
|
715
|
+
path += `C${num(x0 + third)} ${num(y0 + third * tangents[at]!)} ${num(x1 - third)} ${num(y1 - third * tangents[at + 1]!)} ${num(x1)} ${num(y1)}`;
|
|
716
|
+
}
|
|
717
|
+
return path;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/** End labels in their lines' colors, nudged apart so none overlap, kept between `top` and `bottom` where they fit. */
|
|
721
|
+
function spreadLabels(labels: EndLabel[], top: number, bottom: number): string {
|
|
722
|
+
const gap = TICK_SIZE + 2;
|
|
723
|
+
labels.sort((a, b) => a.y - b.y);
|
|
724
|
+
for (let at = 1; at < labels.length; at++) labels[at]!.y = Math.max(labels[at]!.y, labels[at - 1]!.y + gap);
|
|
725
|
+
for (let at = labels.length - 1; at >= 0; at--) {
|
|
726
|
+
const limit = at === labels.length - 1 ? bottom : labels[at + 1]!.y - gap;
|
|
727
|
+
labels[at]!.y = Math.max(top, Math.min(labels[at]!.y, limit));
|
|
728
|
+
}
|
|
729
|
+
return labels
|
|
730
|
+
.map(label => text(label.x, label.y, label.text, { size: TICK_SIZE, fill: label.color, weight: 600 }))
|
|
731
|
+
.join("");
|
|
732
|
+
}
|
|
733
|
+
|
|
425
734
|
// ── Scatter ──────────────────────────────────────────────────────────────────
|
|
426
735
|
|
|
427
736
|
function drawScatter(spec: ChartSpec): Layer {
|
|
@@ -429,8 +738,9 @@ function drawScatter(spec: ChartSpec): Layer {
|
|
|
429
738
|
const xs = spec.x!;
|
|
430
739
|
const ys = entry.points.map(point => point?.value ?? Number.NaN);
|
|
431
740
|
const finite = (values: readonly number[]) => values.filter(Number.isFinite);
|
|
432
|
-
const
|
|
433
|
-
const
|
|
741
|
+
const yAxis = valueScale(finite(ys), 0, 1, { zero: false });
|
|
742
|
+
const yFormat = tickFormat(yAxis, entry);
|
|
743
|
+
const left = PAD + Math.max(...yAxis.ticks.map(tick => measure(yFormat(tick), TICK_SIZE))) + 8;
|
|
434
744
|
const right = WIDTH - PAD - 80;
|
|
435
745
|
const top = 18;
|
|
436
746
|
const bottom = top + 200;
|
|
@@ -438,18 +748,19 @@ function drawScatter(spec: ChartSpec): Layer {
|
|
|
438
748
|
const yScale = valueScale(finite(ys), bottom, top, { zero: false });
|
|
439
749
|
const parts: string[] = [text(left, 6, entry.name, { size: TICK_SIZE, fill: "muted" })];
|
|
440
750
|
for (const tick of yScale.ticks) {
|
|
441
|
-
parts.push(
|
|
751
|
+
parts.push(rule(left, yScale.at(tick), right, yScale.at(tick)));
|
|
442
752
|
parts.push(
|
|
443
|
-
text(left -
|
|
753
|
+
text(left - 8, yScale.at(tick), yFormat(tick), {
|
|
444
754
|
size: TICK_SIZE,
|
|
445
755
|
fill: "muted",
|
|
446
756
|
anchor: "end",
|
|
447
757
|
}),
|
|
448
758
|
);
|
|
449
759
|
}
|
|
760
|
+
const xFormat = tickFormat(xScale, xs);
|
|
450
761
|
for (const tick of xScale.ticks) {
|
|
451
762
|
parts.push(
|
|
452
|
-
text(xScale.at(tick), bottom + 14,
|
|
763
|
+
text(xScale.at(tick), bottom + 14, xFormat(tick), {
|
|
453
764
|
size: TICK_SIZE,
|
|
454
765
|
fill: "muted",
|
|
455
766
|
anchor: "middle",
|
|
@@ -463,7 +774,7 @@ function drawScatter(spec: ChartSpec): Layer {
|
|
|
463
774
|
if (!point || !Number.isFinite(xv)) return;
|
|
464
775
|
const px = xScale.at(xv);
|
|
465
776
|
const py = yScale.at(point.value);
|
|
466
|
-
parts.push(dot(px, py, point.emphasis ? 5 : 3.5,
|
|
777
|
+
parts.push(dot(px, py, point.emphasis ? 5 : 3.5, SERIES[0], point.emphasis ? 1 : 0.85));
|
|
467
778
|
if (labelled) parts.push(text(px + 7, py, clip(spec.categories[at]!, 14), { size: 10, fill: "muted" }));
|
|
468
779
|
});
|
|
469
780
|
return { height: bottom + 36, body: parts.join("") };
|
|
@@ -524,11 +835,12 @@ function niceStep(raw: number): number {
|
|
|
524
835
|
/** Gridlines and tick labels under a horizontal value axis. */
|
|
525
836
|
function drawValueAxis(scale: Scale, series: ChartSeries, top: number, bottom: number): string {
|
|
526
837
|
const parts: string[] = [];
|
|
838
|
+
const format = tickFormat(scale, series);
|
|
527
839
|
for (const tick of scale.ticks) {
|
|
528
840
|
const x = scale.at(tick);
|
|
529
|
-
parts.push(
|
|
841
|
+
parts.push(rule(x, top, x, bottom));
|
|
530
842
|
parts.push(
|
|
531
|
-
text(x, bottom + 12,
|
|
843
|
+
text(x, bottom + 12, format(tick), {
|
|
532
844
|
size: TICK_SIZE,
|
|
533
845
|
fill: "muted",
|
|
534
846
|
anchor: "middle",
|
|
@@ -538,29 +850,48 @@ function drawValueAxis(scale: Scale, series: ChartSeries, top: number, bottom: n
|
|
|
538
850
|
return parts.join("");
|
|
539
851
|
}
|
|
540
852
|
|
|
541
|
-
/**
|
|
853
|
+
/** Dot-and-name legend wrapping across `left`…`right`. */
|
|
542
854
|
function drawLegend(items: readonly (readonly [string, string])[], left: number, right: number): Layer {
|
|
543
855
|
const parts: string[] = [];
|
|
544
856
|
let x = left;
|
|
545
857
|
let y = 6;
|
|
546
858
|
for (const [name, color] of items) {
|
|
547
|
-
const label = clip(name,
|
|
548
|
-
const width =
|
|
859
|
+
const label = clip(name, 32);
|
|
860
|
+
const width = 12 + measure(label, TICK_SIZE) + 18;
|
|
549
861
|
if (x + width > right && x > left) {
|
|
550
862
|
x = left;
|
|
551
863
|
y += 18;
|
|
552
864
|
}
|
|
553
|
-
parts.push(
|
|
554
|
-
parts.push(text(x +
|
|
865
|
+
parts.push(dot(x + 4, y, 4, color));
|
|
866
|
+
parts.push(text(x + 12, y, label, { size: TICK_SIZE, fill: "muted" }));
|
|
555
867
|
x += width;
|
|
556
868
|
}
|
|
557
869
|
return { height: y + 8, body: parts.join("") };
|
|
558
870
|
}
|
|
559
871
|
|
|
872
|
+
/**
|
|
873
|
+
* A category's name at the start of the label column. Starting there, a name
|
|
874
|
+
* set wider than {@link measure} estimates (another font, a larger size)
|
|
875
|
+
* runs toward the plot instead of off the canvas's left edge.
|
|
876
|
+
*/
|
|
877
|
+
function categoryLabel(name: string, y: number): string {
|
|
878
|
+
return text(PAD, y, clip(name, MAX_LABEL_CHARS), { fill: "fg" });
|
|
879
|
+
}
|
|
880
|
+
|
|
560
881
|
/** Width of the category label column. */
|
|
561
882
|
function labelColumnWidth(categories: readonly string[]): number {
|
|
562
|
-
const
|
|
563
|
-
return
|
|
883
|
+
const widest = Math.max(...categories.map(name => measure(clip(name, MAX_LABEL_CHARS), LABEL_SIZE)));
|
|
884
|
+
return Math.max(4 * LABEL_SIZE * ADVANCE, widest);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* Labels for a linear axis' ticks in one unit, so it reads `0, 5k, 10k`
|
|
889
|
+
* rather than `0, 5000, 10k`; a log axis spans units, each tick in its own.
|
|
890
|
+
*/
|
|
891
|
+
function tickFormat(scale: Scale, axis: { readonly dim: Dimension; readonly unit: string }): (tick: number) => string {
|
|
892
|
+
if (scale.log) return tick => formatValue(tick, axis.dim, axis.unit);
|
|
893
|
+
const magnitude = Math.max(0, ...scale.ticks.map(Math.abs));
|
|
894
|
+
return tick => formatValue(tick, axis.dim, axis.unit, magnitude);
|
|
564
895
|
}
|
|
565
896
|
|
|
566
897
|
/** A point's written figure, or its value formatted when the table wrote none. */
|
|
@@ -571,19 +902,20 @@ function pointText(series: ChartSeries, at: number): string {
|
|
|
571
902
|
}
|
|
572
903
|
|
|
573
904
|
/**
|
|
574
|
-
* A value in base units as an axis label: durations and sizes in the unit
|
|
575
|
-
*
|
|
905
|
+
* A value in base units as an axis label: durations and sizes in the unit,
|
|
906
|
+
* counts compacted under the suffix (`12k`, `3.4M`), that `magnitude` reads
|
|
907
|
+
* best in — the value's own size by default, an axis' largest tick to keep
|
|
908
|
+
* its labels in one unit.
|
|
576
909
|
*/
|
|
577
|
-
export function formatValue(value: number, dim: Dimension, unit: string): string {
|
|
910
|
+
export function formatValue(value: number, dim: Dimension, unit: string, magnitude = Math.abs(value)): string {
|
|
578
911
|
switch (dim) {
|
|
579
912
|
case "percent":
|
|
580
|
-
return `${compact(value)}%`;
|
|
913
|
+
return `${compact(value, magnitude)}%`;
|
|
581
914
|
case "ratio":
|
|
582
|
-
return `${compact(value)}×`;
|
|
915
|
+
return `${compact(value, magnitude)}×`;
|
|
583
916
|
case "currency":
|
|
584
|
-
return `${value < 0 ? "-" : ""}${unit}${compact(Math.abs(value))}`;
|
|
917
|
+
return `${value < 0 ? "-" : ""}${unit}${compact(Math.abs(value), magnitude)}`;
|
|
585
918
|
case "duration": {
|
|
586
|
-
const magnitude = Math.abs(value);
|
|
587
919
|
const [divisor, suffix] =
|
|
588
920
|
magnitude === 0
|
|
589
921
|
? [1, "s"]
|
|
@@ -604,22 +936,18 @@ export function formatValue(value: number, dim: Dimension, unit: string): string
|
|
|
604
936
|
}
|
|
605
937
|
case "bytes": {
|
|
606
938
|
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
607
|
-
let scaled = value;
|
|
608
939
|
let index = 0;
|
|
609
|
-
while (
|
|
610
|
-
|
|
611
|
-
index++;
|
|
612
|
-
}
|
|
613
|
-
return `${compact(scaled)}${units[index]}`;
|
|
940
|
+
while (magnitude >= 1000 ** (index + 1) && index < units.length - 1) index++;
|
|
941
|
+
return `${compact(value / 1000 ** index)}${units[index]}`;
|
|
614
942
|
}
|
|
615
943
|
default:
|
|
616
|
-
return compact(value);
|
|
944
|
+
return compact(value, magnitude);
|
|
617
945
|
}
|
|
618
946
|
}
|
|
619
947
|
|
|
620
|
-
/** `1234567` → `1.2M`, `0.0123` → `0.012
|
|
621
|
-
function compact(value: number): string {
|
|
622
|
-
|
|
948
|
+
/** `1234567` → `1.2M`, `0.0123` → `0.012`; the suffix follows `magnitude` (`5000` → `5k` beside `10k`). */
|
|
949
|
+
function compact(value: number, magnitude = Math.abs(value)): string {
|
|
950
|
+
if (value === 0) return "0";
|
|
623
951
|
if (magnitude >= 1e9) return `${trim(value / 1e9)}B`;
|
|
624
952
|
if (magnitude >= 1e6) return `${trim(value / 1e6)}M`;
|
|
625
953
|
if (magnitude >= 1e4) return `${trim(value / 1e3)}k`;
|
|
@@ -632,8 +960,22 @@ function trim(value: number): string {
|
|
|
632
960
|
return Number(value.toFixed(digits)).toString();
|
|
633
961
|
}
|
|
634
962
|
|
|
963
|
+
/** Narrow and wide glyphs of a UI sans (Geist, SF Pro, Helvetica Neue), for {@link measure}. */
|
|
964
|
+
const NARROW = "iljtfrI.,:;'!|()[] ";
|
|
965
|
+
const WIDE = "mwMW%@—";
|
|
966
|
+
|
|
967
|
+
/** Estimated width of `content` set in the chart's sans at `size`. */
|
|
635
968
|
function measure(content: string, size: number): number {
|
|
636
|
-
|
|
969
|
+
let em = 0;
|
|
970
|
+
for (const char of content) {
|
|
971
|
+
if (NARROW.includes(char)) em += 0.3;
|
|
972
|
+
else if (WIDE.includes(char)) em += 0.85;
|
|
973
|
+
else if (char >= "0" && char <= "9") em += 0.58;
|
|
974
|
+
else if (char >= "A" && char <= "Z") em += 0.66;
|
|
975
|
+
else if (char.codePointAt(0)! >= 0x1100) em += 1;
|
|
976
|
+
else em += 0.53;
|
|
977
|
+
}
|
|
978
|
+
return em * size;
|
|
637
979
|
}
|
|
638
980
|
|
|
639
981
|
function clip(content: string, chars: number): string {
|
|
@@ -673,13 +1015,24 @@ function text(x: number, y: number, content: string, style: TextStyle): string {
|
|
|
673
1015
|
return `<text ${attrs.join(" ")}>${escaped}</text>`;
|
|
674
1016
|
}
|
|
675
1017
|
|
|
1018
|
+
/** A rectangle with corners rounded by {@link RADIUS}, less for one too small to hold it. */
|
|
676
1019
|
function rect(x: number, y: number, width: number, height: number, fill: string, opacity: number): string {
|
|
1020
|
+
const w = Math.max(0, width);
|
|
1021
|
+
const radius = Math.min(RADIUS, w / 2, height / 2);
|
|
1022
|
+
const rounded = radius >= 0.5 ? ` rx="${num(radius)}"` : "";
|
|
677
1023
|
const alpha = opacity < 1 ? ` fill-opacity="${Number(opacity.toFixed(2))}"` : "";
|
|
678
|
-
return `<rect x="${num(x)}" y="${num(y)}" width="${num(
|
|
1024
|
+
return `<rect x="${num(x)}" y="${num(y)}" width="${num(w)}" height="${num(height)}"${rounded} fill="${token(fill)}"${alpha}/>`;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/** A round-capped line. */
|
|
1028
|
+
function line(x1: number, y1: number, x2: number, y2: number, stroke: string, width: number, opacity = 1): string {
|
|
1029
|
+
const alpha = opacity < 1 ? ` stroke-opacity="${Number(opacity.toFixed(2))}"` : "";
|
|
1030
|
+
return `<line x1="${num(x1)}" y1="${num(y1)}" x2="${num(x2)}" y2="${num(y2)}" stroke="${token(stroke)}" stroke-width="${width}" stroke-linecap="round"${alpha}/>`;
|
|
679
1031
|
}
|
|
680
1032
|
|
|
681
|
-
|
|
682
|
-
|
|
1033
|
+
/** A gridline: a hairline that recedes behind the marks. */
|
|
1034
|
+
function rule(x1: number, y1: number, x2: number, y2: number): string {
|
|
1035
|
+
return line(x1, y1, x2, y2, "border", 1, GRID_OPACITY);
|
|
683
1036
|
}
|
|
684
1037
|
|
|
685
1038
|
function dot(x: number, y: number, radius: number, fill: string, opacity = 1): string {
|