blockcharts 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +287 -2
- package/package.json +87 -4
- package/release/bc.d.ts +764 -0
- package/release/blocks/guide.axis.js +3 -0
- package/release/blocks/guide.grid.js +3 -0
- package/release/blocks/guide.kpi.js +3 -0
- package/release/blocks/guide.legend.js +3 -0
- package/release/blocks/interaction.animate.js +3 -0
- package/release/blocks/interaction.brush.js +3 -0
- package/release/blocks/interaction.crosshair.js +3 -0
- package/release/blocks/interaction.legend-filter.js +3 -0
- package/release/blocks/interaction.tooltip.js +3 -0
- package/release/blocks/interaction.zoom-controls.js +3 -0
- package/release/blocks/interaction.zoom.js +3 -0
- package/release/blocks/lib.columns.js +3 -0
- package/release/blocks/lib.curve.js +3 -0
- package/release/blocks/lib.pointer.js +3 -0
- package/release/blocks/lib.stats.js +3 -0
- package/release/blocks/lib.ticks.js +3 -0
- package/release/blocks/lib.view.js +3 -0
- package/release/blocks/mark.arc.js +3 -0
- package/release/blocks/mark.area.js +3 -0
- package/release/blocks/mark.boxplot.js +3 -0
- package/release/blocks/mark.candlestick.js +3 -0
- package/release/blocks/mark.line.js +3 -0
- package/release/blocks/mark.point.js +3 -0
- package/release/blocks/mark.polygon.js +3 -0
- package/release/blocks/mark.rect.js +3 -0
- package/release/blocks/mark.rule.js +3 -0
- package/release/blocks/mark.sankey.js +3 -0
- package/release/blocks/mark.text.js +3 -0
- package/release/blocks/mark.violin.js +3 -0
- package/release/blocks/renderer.canvas.js +3 -0
- package/release/blocks/renderer.svg.js +3 -0
- package/release/blocks/scale.band.js +3 -0
- package/release/blocks/scale.color.js +3 -0
- package/release/blocks/scale.linear.js +3 -0
- package/release/blocks/scale.log.js +3 -0
- package/release/blocks/scale.sequential.js +3 -0
- package/release/blocks/scale.size.js +3 -0
- package/release/blocks/scale.time.js +3 -0
- package/release/blocks/transform.aggregate.js +3 -0
- package/release/blocks/transform.bin.js +3 -0
- package/release/blocks/transform.calculate.js +3 -0
- package/release/blocks/transform.calendar.js +3 -0
- package/release/blocks/transform.contour.js +3 -0
- package/release/blocks/transform.decimate.js +3 -0
- package/release/blocks/transform.density.js +3 -0
- package/release/blocks/transform.filter.js +3 -0
- package/release/blocks/transform.fold.js +3 -0
- package/release/blocks/transform.funnel.js +3 -0
- package/release/blocks/transform.hexbin.js +3 -0
- package/release/blocks/transform.highlight.js +3 -0
- package/release/blocks/transform.marimekko.js +3 -0
- package/release/blocks/transform.quartiles.js +3 -0
- package/release/blocks/transform.sankey.js +3 -0
- package/release/blocks/transform.sort.js +3 -0
- package/release/blocks/transform.stack.js +3 -0
- package/release/blocks/transform.treemap.js +3 -0
- package/release/blocks/transform.waterfall.js +3 -0
- package/release/blocks/transform.window.js +3 -0
- package/release/core.js +5 -0
- package/release/kits/basic.d.mts +3 -0
- package/release/kits/basic.d.ts +3 -0
- package/release/kits/basic.js +64 -0
- package/release/kits/basic.mjs +64 -0
- package/release/kits/composition.d.mts +3 -0
- package/release/kits/composition.d.ts +3 -0
- package/release/kits/composition.js +106 -0
- package/release/kits/composition.mjs +106 -0
- package/release/kits/dashboard.d.mts +3 -0
- package/release/kits/dashboard.d.ts +3 -0
- package/release/kits/dashboard.js +100 -0
- package/release/kits/dashboard.mjs +100 -0
- package/release/kits/full.d.mts +3 -0
- package/release/kits/full.d.ts +3 -0
- package/release/kits/full.js +181 -0
- package/release/kits/full.mjs +181 -0
- package/release/kits/timeseries.d.mts +3 -0
- package/release/kits/timeseries.d.ts +3 -0
- package/release/kits/timeseries.js +94 -0
- package/release/kits/timeseries.mjs +94 -0
- package/release/loader.js +3 -0
- package/release/manifest.json +1038 -0
package/release/bc.d.ts
ADDED
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* blockcharts — contracts between the core and blocks.
|
|
3
|
+
*
|
|
4
|
+
* Ambient on purpose: no import/export anywhere. Every block file is a plain
|
|
5
|
+
* classic script (works from file://, inlines into <script>), and tsc emits it
|
|
6
|
+
* as-is. A block is written as
|
|
7
|
+
*
|
|
8
|
+
* (function () {
|
|
9
|
+
* BC.define({ role: 'mark', type: 'rect', version: 1, requires: [...], ... });
|
|
10
|
+
* })();
|
|
11
|
+
*
|
|
12
|
+
* Page shape (what an agent writes):
|
|
13
|
+
*
|
|
14
|
+
* <script type="application/json" id="bc-data">{ "sales": [ {...}, ... ] }</script>
|
|
15
|
+
* <script type="application/json" data-bc-chart>{ "data": "sales", ... }</script>
|
|
16
|
+
*
|
|
17
|
+
* The chart is mounted where its <script data-bc-chart> sits (or into #spec.id).
|
|
18
|
+
*/
|
|
19
|
+
declare namespace BC {
|
|
20
|
+
// ───────────────────────── data lake ─────────────────────────
|
|
21
|
+
|
|
22
|
+
type Row = Record<string, unknown>;
|
|
23
|
+
/** Numeric fields load as Float64Array, not boxed numbers — fewer GC pauses on
|
|
24
|
+
* scans/domain inference, and the same shape the future Canvas renderer needs. */
|
|
25
|
+
type Column = ArrayLike<unknown> | Float64Array;
|
|
26
|
+
|
|
27
|
+
/** Normalized, columnar. Rows are converted on load (numeric fields → Float64Array). */
|
|
28
|
+
interface Table {
|
|
29
|
+
readonly length: number;
|
|
30
|
+
readonly columns: Readonly<Record<string, Column>>;
|
|
31
|
+
/** NaN in this table means the row has no such field, not that a value is missing: a tooltip leaves such a line out. */
|
|
32
|
+
readonly sparse?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A dataset stored as text: the UTF-8 JSON of rows or { columns }, base64-encoded, optionally gzipped first.
|
|
36
|
+
* Decoded asynchronously (gzip needs DecompressionStream); charts wait for their dataset, see Runtime.ready. */
|
|
37
|
+
interface EncodedDataset {
|
|
38
|
+
encoding: 'base64' | 'gzip+base64';
|
|
39
|
+
data: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One numeric column stored as raw little-endian values (base64, optionally gzipped first): 4 or 8 bytes per value
|
|
43
|
+
* instead of ~10 characters of JSON. Used as a value in `columns`; read as Float64 (NaN = missing for float types). */
|
|
44
|
+
interface BinaryColumn {
|
|
45
|
+
dtype: 'float32' | 'float64' | 'int8' | 'int16' | 'int32' | 'uint8' | 'uint16' | 'uint32';
|
|
46
|
+
encoding: 'base64' | 'gzip+base64';
|
|
47
|
+
data: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type DatasetInput = Row[] | { columns: Record<string, unknown[] | BinaryColumn> } | EncodedDataset;
|
|
51
|
+
|
|
52
|
+
/** Content of <script id="bc-data">. Datasets are shared by name between charts. */
|
|
53
|
+
type DataLake = Record<string, DatasetInput>;
|
|
54
|
+
|
|
55
|
+
// ───────────────────────── spec (what the agent writes) ─────────────────────────
|
|
56
|
+
|
|
57
|
+
/** Common shape of every block reference in a spec: `type` selects `<role>.<type>`. */
|
|
58
|
+
interface BlockSpec {
|
|
59
|
+
type: string;
|
|
60
|
+
[param: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ChartSpec {
|
|
64
|
+
id?: string;
|
|
65
|
+
/** Key in the data lake. */
|
|
66
|
+
data: string;
|
|
67
|
+
/** Applied in order to the dataset before scales are built (stack, bin, ...). */
|
|
68
|
+
transforms?: TransformSpec[];
|
|
69
|
+
/** [width, height] px. The chart is laid out at its container's width and this height (see `responsive`); the width
|
|
70
|
+
* is used where no container's width is known (a server, the composer's check). Default [640, 400]. */
|
|
71
|
+
size?: [number, number];
|
|
72
|
+
/** px; a number is every side. Default 16 on every side, and the guides take what they need on top of it. */
|
|
73
|
+
padding?: Partial<Insets> | number;
|
|
74
|
+
title?: string;
|
|
75
|
+
/** What a reader who cannot see the chart hears it called (its aria-label): what it shows. Default: the title; the
|
|
76
|
+
* composer gives a chart without either the title of its card. What its guides say (a KPI's figure and change)
|
|
77
|
+
* follows it. */
|
|
78
|
+
alt?: string;
|
|
79
|
+
/** Declared once per chart, referenced by name from marks and guides. */
|
|
80
|
+
scales?: Record<string, ScaleSpec>;
|
|
81
|
+
guides?: GuideSpec[];
|
|
82
|
+
marks: MarkSpec[];
|
|
83
|
+
interaction?: InteractionSpec[];
|
|
84
|
+
/** Initial view: scale name → visible domain (zoomed state). Same thing `setView` changes at runtime. */
|
|
85
|
+
view?: Record<string, unknown[]>;
|
|
86
|
+
/** The chart is laid out at its container's width, narrower (a phone) or wider (a desk card) than `size`, so text keeps
|
|
87
|
+
* its size instead of shrinking or growing with the drawing; the height stays; again whenever that width changes.
|
|
88
|
+
* Never narrower than 200 px. Default true; false keeps `size` and scales the drawing to the container. */
|
|
89
|
+
responsive?: boolean;
|
|
90
|
+
/** Default "svg". */
|
|
91
|
+
renderer?: string;
|
|
92
|
+
/** The language numbers and dates are written in where a format names no `locale`. Default "en-US". */
|
|
93
|
+
locale?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface TransformSpec extends BlockSpec {}
|
|
97
|
+
|
|
98
|
+
interface ScaleSpec extends BlockSpec {
|
|
99
|
+
/** Extra column contributing to the domain; marks' channels contribute automatically. */
|
|
100
|
+
field?: string;
|
|
101
|
+
/** Explicit domain overrides inference. */
|
|
102
|
+
domain?: unknown[];
|
|
103
|
+
/** "width" | "height" resolve to the plot area (defaults for scales named x / y);
|
|
104
|
+
* otherwise an explicit range (numbers, or colors for color scales). */
|
|
105
|
+
range?: 'width' | 'height' | unknown[];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface GuideSpec extends BlockSpec {
|
|
109
|
+
/** Name of the scale the guide is bound to. */
|
|
110
|
+
scale?: string;
|
|
111
|
+
/** Which side of the plot the guide occupies. Default: derived from the
|
|
112
|
+
* bound scale's resolved range ("height" → "left", "width" → "bottom").
|
|
113
|
+
* Set explicitly for a second guide on the same side (e.g. a secondary
|
|
114
|
+
* y-axis: `{"type":"axis","scale":"y2","position":"right"}`). */
|
|
115
|
+
position?: 'top' | 'right' | 'bottom' | 'left';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface MarkSpec extends BlockSpec {
|
|
119
|
+
/** Channels: "date" = field on the scale named like the channel (x → scale "x");
|
|
120
|
+
* { field, scale } is explicit; { value } is a constant. */
|
|
121
|
+
[channel: string]: unknown;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface InteractionSpec extends BlockSpec {}
|
|
125
|
+
|
|
126
|
+
/** What interaction.tooltip hands to a custom `content` function. */
|
|
127
|
+
interface TooltipDatum {
|
|
128
|
+
/** Row of the table under the pointer. */
|
|
129
|
+
row: number;
|
|
130
|
+
/** Every column of that row, not only the ones the mark reads. */
|
|
131
|
+
values: Record<string, unknown>;
|
|
132
|
+
/** Every row the shape under the pointer stands for (see Pick.rows); just `row` for most shapes. */
|
|
133
|
+
rows: number[];
|
|
134
|
+
/** The default content: a label and a formatted value per line, only what all of `rows` have in common. */
|
|
135
|
+
lines: [string, string][];
|
|
136
|
+
mark: MarkSpec;
|
|
137
|
+
markIndex: number;
|
|
138
|
+
/** Anchor of the marker, in display-list px. */
|
|
139
|
+
x: number;
|
|
140
|
+
y: number;
|
|
141
|
+
table: Table;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** What a tooltip `content` function may return. A string is inserted as text. `{ html }` is inserted as markup
|
|
145
|
+
* (the caller is responsible for escaping data in it). A Node is inserted as is. An array inserts each item.
|
|
146
|
+
* null / false hides the tooltip; undefined keeps the default content. */
|
|
147
|
+
type TooltipContent = string | number | { html: string } | Node | null | false | undefined | readonly TooltipContent[];
|
|
148
|
+
|
|
149
|
+
type ChannelSpec = string | { field?: string; scale?: string; value?: number | string };
|
|
150
|
+
|
|
151
|
+
// ───────────────────────── geometry ─────────────────────────
|
|
152
|
+
|
|
153
|
+
interface Rect {
|
|
154
|
+
x: number;
|
|
155
|
+
y: number;
|
|
156
|
+
w: number;
|
|
157
|
+
h: number;
|
|
158
|
+
}
|
|
159
|
+
interface Insets {
|
|
160
|
+
top: number;
|
|
161
|
+
right: number;
|
|
162
|
+
bottom: number;
|
|
163
|
+
left: number;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ───────────────────────── scales ─────────────────────────
|
|
167
|
+
|
|
168
|
+
interface Tick {
|
|
169
|
+
value: unknown;
|
|
170
|
+
/** Position in the scale's range: px for positional scales, a fraction 0..1 of the domain for a continuous color scale. */
|
|
171
|
+
pos: number;
|
|
172
|
+
label: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** What a scale makes of a value: a place along the plot, a color, or the size of a circle. */
|
|
176
|
+
type ScaleOutput = 'position' | 'color' | 'size';
|
|
177
|
+
/** What a scale takes: numbers, dates, or categories (each distinct value a place or a color of its own). */
|
|
178
|
+
type ScaleInput = 'number' | 'date' | 'category';
|
|
179
|
+
|
|
180
|
+
/** The scales a channel can be drawn through, by what they declare (ScaleDef.output and ScaleDef.input), never by name:
|
|
181
|
+
* a scale block of a new name fits every mark whose channels take what it declares. */
|
|
182
|
+
interface ScaleNeed {
|
|
183
|
+
output: ScaleOutput;
|
|
184
|
+
/** The inputs it takes; omitted = any. */
|
|
185
|
+
input?: readonly ScaleInput[];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** A scale on a chart: the mapping its block makes, with what the block declares (ScaleDef.output, ScaleDef.input),
|
|
189
|
+
* set by the core when it creates it. Marks, guides and interactions read these, never the scale's name. */
|
|
190
|
+
interface Scale<D = any, R = any> extends ScaleMapping<D, R> {
|
|
191
|
+
readonly output: ScaleOutput;
|
|
192
|
+
readonly input: ScaleInput;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A callable data → range mapping, as a scale block makes it. `any` because the domain is per-kind. */
|
|
196
|
+
interface ScaleMapping<D = any, R = any> {
|
|
197
|
+
(value: D): R;
|
|
198
|
+
/** Current (view) domain — what ticks and mapping use. Equals baseDomain() until zoomed. */
|
|
199
|
+
domain(): D[];
|
|
200
|
+
/** Domain inferred from data / spec, before any view is applied. */
|
|
201
|
+
baseDomain(): D[];
|
|
202
|
+
/** Set the view domain; null resets to baseDomain(). Never changes the range. */
|
|
203
|
+
setDomain(domain: D[] | null): void;
|
|
204
|
+
range(): R[];
|
|
205
|
+
/** Called by the core after layout settles (guides may shrink the plot). */
|
|
206
|
+
setRange(range: R[]): void;
|
|
207
|
+
invert?(px: number): D;
|
|
208
|
+
ticks?(approxCount?: number): Tick[];
|
|
209
|
+
/** band / point scales only */
|
|
210
|
+
readonly bandwidth?: number;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Passed to ScaleDef.create. */
|
|
214
|
+
interface ScaleCtx {
|
|
215
|
+
readonly name: string;
|
|
216
|
+
/** Columns of every channel bound to this scale (plus spec.field). Domain inference input. */
|
|
217
|
+
readonly sources: readonly Column[];
|
|
218
|
+
/** Range already resolved from spec.range and the provisional plot rect. */
|
|
219
|
+
readonly range: readonly unknown[];
|
|
220
|
+
/** Categorical color by index from the theme palette (same as ChartCtx.color). */
|
|
221
|
+
color(index: number): string;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ───────────────────────── display list ─────────────────────────
|
|
225
|
+
|
|
226
|
+
/** Which table row of which mark a primitive represents — interaction uses it for hit-testing. */
|
|
227
|
+
interface DatumRef {
|
|
228
|
+
mark: number;
|
|
229
|
+
row: number;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
interface Style {
|
|
233
|
+
fill?: string; // any CSS color, CSS variables allowed (theming)
|
|
234
|
+
stroke?: string;
|
|
235
|
+
strokeWidth?: number;
|
|
236
|
+
strokeDash?: number[];
|
|
237
|
+
opacity?: number;
|
|
238
|
+
fillOpacity?: number;
|
|
239
|
+
strokeLinejoin?: 'round' | 'miter' | 'bevel';
|
|
240
|
+
strokeLinecap?: 'round' | 'butt' | 'square';
|
|
241
|
+
/** "stroke": the stroke is painted under the fill. A text stroked in the card color this way has a halo, and stays
|
|
242
|
+
* readable where it crosses a mark. */
|
|
243
|
+
paintOrder?: 'stroke';
|
|
244
|
+
/** The fill fades out downwards: full `fillOpacity` at the top of the plot, transparent at its bottom (a vertical
|
|
245
|
+
* gradient over the plot rectangle, the same for every shape, so areas stacked on one chart fade alike). */
|
|
246
|
+
fade?: boolean;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
interface PrimBase {
|
|
250
|
+
style?: Style;
|
|
251
|
+
/** CSS class hook for user styling / printing. */
|
|
252
|
+
cls?: string;
|
|
253
|
+
ref?: DatumRef;
|
|
254
|
+
/** Native tooltip / accessibility label. */
|
|
255
|
+
title?: string;
|
|
256
|
+
/** Extra attributes, written as `data-<key>` (a legend item tells an interaction which value it stands for). */
|
|
257
|
+
data?: Record<string, string>;
|
|
258
|
+
/** A switch a key can press, on or off (a legend item an interaction toggles): a renderer with elements makes it a
|
|
259
|
+
* button that takes the focus, keeps the focus on it from one redraw to the next, and names the chart a group of
|
|
260
|
+
* controls instead of an image (whose content a screen reader would not read). Its name is its text or `title`. */
|
|
261
|
+
pressed?: boolean;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** `d` is a standard SVG path string (Path2D understands it too, so a Canvas renderer can reuse it). */
|
|
265
|
+
interface PathPrim extends PrimBase {
|
|
266
|
+
type: 'path';
|
|
267
|
+
d: string;
|
|
268
|
+
}
|
|
269
|
+
interface RectPrim extends PrimBase {
|
|
270
|
+
type: 'rect';
|
|
271
|
+
x: number;
|
|
272
|
+
y: number;
|
|
273
|
+
w: number;
|
|
274
|
+
h: number;
|
|
275
|
+
r?: number;
|
|
276
|
+
}
|
|
277
|
+
interface CirclePrim extends PrimBase {
|
|
278
|
+
type: 'circle';
|
|
279
|
+
cx: number;
|
|
280
|
+
cy: number;
|
|
281
|
+
r: number;
|
|
282
|
+
}
|
|
283
|
+
interface TextPrim extends PrimBase {
|
|
284
|
+
type: 'text';
|
|
285
|
+
x: number;
|
|
286
|
+
y: number;
|
|
287
|
+
text: string;
|
|
288
|
+
size?: number;
|
|
289
|
+
weight?: number | string;
|
|
290
|
+
anchor?: 'start' | 'middle' | 'end';
|
|
291
|
+
baseline?: 'auto' | 'middle' | 'hanging';
|
|
292
|
+
/** degrees around (x, y) */
|
|
293
|
+
rotate?: number;
|
|
294
|
+
}
|
|
295
|
+
interface GroupPrim extends PrimBase {
|
|
296
|
+
type: 'g';
|
|
297
|
+
children: Prim[];
|
|
298
|
+
translate?: [number, number];
|
|
299
|
+
clip?: Rect;
|
|
300
|
+
}
|
|
301
|
+
type Prim = PathPrim | RectPrim | CirclePrim | TextPrim | GroupPrim;
|
|
302
|
+
|
|
303
|
+
/** Draw order, bottom to top. */
|
|
304
|
+
type LayerName = 'grid' | 'marks' | 'axes' | 'overlay';
|
|
305
|
+
type Emission = Partial<Record<LayerName, Prim[]>>;
|
|
306
|
+
|
|
307
|
+
interface Layer {
|
|
308
|
+
name: LayerName;
|
|
309
|
+
prims: Prim[];
|
|
310
|
+
clip?: Rect;
|
|
311
|
+
}
|
|
312
|
+
interface DisplayList {
|
|
313
|
+
size: { w: number; h: number };
|
|
314
|
+
plot: Rect;
|
|
315
|
+
layers: Layer[];
|
|
316
|
+
/** What the chart is called for a reader who cannot see it (ChartSpec.alt): a renderer gives its element this name. */
|
|
317
|
+
label?: string;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ───────────────────────── contexts ─────────────────────────
|
|
321
|
+
|
|
322
|
+
/** Resolved data of one mark channel. */
|
|
323
|
+
interface ChannelData {
|
|
324
|
+
readonly field?: string;
|
|
325
|
+
readonly scale?: Scale;
|
|
326
|
+
/** Column values (or a constant expanded to table length). */
|
|
327
|
+
readonly raw: Column;
|
|
328
|
+
/** `scale(raw[i])`; a constant goes through the scale of a position, and is itself on a channel without a scale. Marks read this. */
|
|
329
|
+
readonly mapped: ArrayLike<any>;
|
|
330
|
+
/** Half the band of a band scale, 0 for any other: `mapped[i] + half` is the middle of the row's band. */
|
|
331
|
+
readonly half: number;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
interface ChartCtx {
|
|
335
|
+
readonly spec: ChartSpec;
|
|
336
|
+
/** BC.formatter in the language of the chart (its `locale`), also when called later, from an event: a card on hover
|
|
337
|
+
* is written like the labels. BC.formatter itself has the chart's language only while the chart is built and drawn. */
|
|
338
|
+
format(spec: string | FormatSpec, kind?: 'number' | 'date'): (value: unknown) => string;
|
|
339
|
+
readonly table: Table;
|
|
340
|
+
/** The table before the transforms that depend on the view (transform.decimate and what follows it): every row,
|
|
341
|
+
* whatever the zoom. A figure about the data as a whole (a KPI) reads this; marks draw `table`. */
|
|
342
|
+
readonly baseTable: Table;
|
|
343
|
+
readonly size: { w: number; h: number };
|
|
344
|
+
readonly plot: Rect;
|
|
345
|
+
readonly scales: Readonly<Record<string, Scale>>;
|
|
346
|
+
/** Resolve and cache a channel of a mark. Throws a Diagnostic-worthy error if required and missing. */
|
|
347
|
+
channel(mark: MarkSpec, name: string): ChannelData;
|
|
348
|
+
/** Which plot axis a positional scale runs along (from its resolved `range`: "width" → horizontal,
|
|
349
|
+
* "height" → vertical); null for non-positional scales (color, size). Guides use it to pick a default side. */
|
|
350
|
+
orientation(scale: string): 'horizontal' | 'vertical' | null;
|
|
351
|
+
/** Ticks the chart uses for a scale, computed once per frame and shared by every guide, so an axis and
|
|
352
|
+
* a grid line up. `approxCount` defaults to plot size / 40px (vertical) or / 80px (horizontal). */
|
|
353
|
+
ticks(scale: string, approxCount?: number): Tick[];
|
|
354
|
+
/** Rows of the table a mark has to draw. Marks loop over `from..to` instead of `0..table.length`. While a scale is
|
|
355
|
+
* zoomed and the column bound to it is sorted (either way), only the rows inside the plot are returned, with one
|
|
356
|
+
* neighbour on each side and 12 px of margin; otherwise the whole table. Mapped channel values outside the range
|
|
357
|
+
* are NaN. */
|
|
358
|
+
rows(mark: MarkSpec): { from: number; to: number };
|
|
359
|
+
/** Rows of a mark split into series — by its `group` param or by the field of its `color` channel; no
|
|
360
|
+
* such field = one series — each in x order (rows unsorted by `xs` are sorted). Lines and areas share it. The result
|
|
361
|
+
* is kept until the next redraw for the same `xs` (a pointer move asks again on every frame), so it is read-only. */
|
|
362
|
+
series(mark: MarkSpec, xs: ArrayLike<number>): { key: unknown; rows: number[] }[];
|
|
363
|
+
/** Categorical color by index, from the theme (`--bc-c<N>`) with a built-in fallback palette. */
|
|
364
|
+
color(index: number): string;
|
|
365
|
+
/** Text measurement for layout, generous: text laid out by it is never clipped. In a browser never narrower than the
|
|
366
|
+
* text in the chart's font, with room on each side for ink that reaches past its advance (the tail of an r). */
|
|
367
|
+
measure(text: string, size?: number): { w: number; h: number };
|
|
368
|
+
/** The width of a label in the chart's own font, in a browser: measured in the font family its host draws in, at
|
|
369
|
+
* this size and weight, every figure as the widest (figures are drawn tabular). Where no font can be measured (the
|
|
370
|
+
* composer, a test), as a sans-serif such as Inter draws it, a little over. For room that shows when it is
|
|
371
|
+
* overestimated (between value labels, under slanted ones). `weight` as in a text primitive. */
|
|
372
|
+
textWidth(text: string, size?: number, weight?: unknown): number;
|
|
373
|
+
/** Whether a text has characters a full em wide (Chinese, Japanese, Korean): such text can break between any two. */
|
|
374
|
+
wide(text: string): boolean;
|
|
375
|
+
/** The px an overlay (InteractionDef.corner) takes from the left and the right end of the chart's top or bottom edge. */
|
|
376
|
+
edgeTaken(edge: 'top' | 'bottom'): { left: number; right: number };
|
|
377
|
+
/** An interaction on the chart reads it along x, at any height (InteractionDef.alongX, a crosshair): a mark's pick
|
|
378
|
+
* then answers for the x of the pointer above or below its shape too. */
|
|
379
|
+
readonly alongX: boolean;
|
|
380
|
+
/** Whether an interaction switches the categories of this scale from its legend in the chart (InteractionDef.toggles):
|
|
381
|
+
* the legend then makes its items switches (Prim.pressed). */
|
|
382
|
+
toggled(scale: string): boolean;
|
|
383
|
+
/** From a guide's render: words a reader who cannot see the chart needs, said after its name (a KPI's figure and
|
|
384
|
+
* change; see ChartSpec.alt). Collected afresh at every draw. */
|
|
385
|
+
say(text: string): void;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Available to interactions once the chart is in the DOM. */
|
|
389
|
+
interface LiveCtx extends ChartCtx {
|
|
390
|
+
readonly host: HTMLElement;
|
|
391
|
+
readonly root: Element;
|
|
392
|
+
/** The current display list (a new one after every redraw). */
|
|
393
|
+
readonly list: DisplayList;
|
|
394
|
+
/** Change the visible domain of a scale (null = back to the full domain). Calls in one frame are
|
|
395
|
+
* merged; only marks, guides and rendering rerun — the layout stays frozen. */
|
|
396
|
+
setView(scale: string, domain: unknown[] | null): void;
|
|
397
|
+
/** The requested visible domain of every scale (applied at the next frame); the full domain if not zoomed. */
|
|
398
|
+
getView(): Record<string, unknown[]>;
|
|
399
|
+
/** The domain `scale` would have if it were inferred from only the rows visible in the requested view of
|
|
400
|
+
* `follow` (same nice / zero / padding rules as the full domain). null = there is no view to follow or no
|
|
401
|
+
* row is visible: leave the scale alone. Feed it to setView to auto-fit y to an x zoom. */
|
|
402
|
+
fit(scale: string, follow: string): unknown[] | null;
|
|
403
|
+
/** Which datum an event target belongs to (reads DatumRef from the DOM; only meaningful with the SVG renderer). */
|
|
404
|
+
hit(ev: Event): DatumRef | null;
|
|
405
|
+
/** The `data` of the primitive under a pointer event (a legend item says which value it stands for), or null.
|
|
406
|
+
* Works with every renderer: the DOM ones read the element, the canvas one tests the display list. */
|
|
407
|
+
dataAt(ev: MouseEvent): Record<string, string> | null;
|
|
408
|
+
/** Register teardown; the core runs it on destroy/update. */
|
|
409
|
+
cleanup(fn: () => void): void;
|
|
410
|
+
/** Make `host` a positioning context (`position: relative`, unless it already is one) for an absolutely-positioned
|
|
411
|
+
* overlay (a tooltip marker, a drag box, a button bar). Calls stack: `host` only goes back to how it was once
|
|
412
|
+
* every claim on it has called the returned release function. Interactions must not set `host.style.position`
|
|
413
|
+
* themselves — more than one may need it at once, and a plain "save it, restore it" fights the others. */
|
|
414
|
+
claimPosition(): () => void;
|
|
415
|
+
/** Claim the drags that `when` says yes to (a brush: those with its modifier held), so that another interaction
|
|
416
|
+
* (the pan of a zoom) leaves them alone. A claim lasts as long as the chart is built as it is now: a rebuild
|
|
417
|
+
* attaches the interactions again, and they claim again. */
|
|
418
|
+
claimDrag(when: (ev: PointerEvent) => boolean): void;
|
|
419
|
+
/** Whether an interaction has claimed this drag (claimDrag). One that drags by default asks before it starts. */
|
|
420
|
+
dragClaimed(ev: PointerEvent): boolean;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ───────────────────────── blocks ─────────────────────────
|
|
424
|
+
|
|
425
|
+
type Role = 'lib' | 'scale' | 'transform' | 'mark' | 'guide' | 'interaction' | 'renderer';
|
|
426
|
+
/** `<role>.<type>`, e.g. "scale.linear", "mark.rect". Derived, never written by hand. */
|
|
427
|
+
type BlockName = `${Role}.${string}`;
|
|
428
|
+
|
|
429
|
+
interface ParamDef {
|
|
430
|
+
/** "color": a CSS color that loads nothing (hex, rgb()/hsl(), a name, var(--x)); never url(…). */
|
|
431
|
+
/** "format": a preset name or Intl options (BC.formatter); "formats": an object of field names and formats; "fields": a
|
|
432
|
+
* list of field names, each checked against the data like a "field". */
|
|
433
|
+
kind: 'string' | 'number' | 'boolean' | 'enum' | 'field' | 'fields' | 'scale' | 'list' | 'color' | 'format' | 'formats' | 'any';
|
|
434
|
+
required?: boolean;
|
|
435
|
+
default?: unknown;
|
|
436
|
+
/** for kind "enum" */
|
|
437
|
+
values?: readonly string[];
|
|
438
|
+
/** for kind "field" of a transform: what it reads from the field. A field with none of them is an error when the
|
|
439
|
+
* chart is built, and one with a few values it cannot read a warning, naming them. */
|
|
440
|
+
reads?: 'number' | 'date';
|
|
441
|
+
doc?: string;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
interface ChannelDef {
|
|
445
|
+
required?: boolean;
|
|
446
|
+
/** The scales it can be drawn through, by what they declare. Omitted = any. */
|
|
447
|
+
accepts?: ScaleNeed;
|
|
448
|
+
/** Channel whose scale this one uses by default (e.g. `y2` of a rect shares the scale of `y`), instead of a scale
|
|
449
|
+
* named like itself. An explicit { field, scale } still wins. */
|
|
450
|
+
sharesScale?: string;
|
|
451
|
+
/** The channel is a plain field (a label, a slice size) read as it is: no scale, `mapped` equals `raw`. Naming a scale
|
|
452
|
+
* for it is a validation error. */
|
|
453
|
+
unscaled?: boolean;
|
|
454
|
+
default?: unknown;
|
|
455
|
+
doc?: string;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
interface BlockBase {
|
|
459
|
+
/** Second part of the name. */
|
|
460
|
+
type: string;
|
|
461
|
+
/** Integer; bump on any behavior change. Same name + same version = idempotent no-op. */
|
|
462
|
+
version: number;
|
|
463
|
+
/** Full names of blocks this one needs at runtime. Single source of truth for the manifest. */
|
|
464
|
+
requires?: readonly BlockName[];
|
|
465
|
+
/** Validated by the core, exported to the manifest for the agent. */
|
|
466
|
+
params?: Readonly<Record<string, ParamDef>>;
|
|
467
|
+
doc?: string;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
interface ScaleDef extends BlockBase {
|
|
471
|
+
role: 'scale';
|
|
472
|
+
/** What it makes of a value and what it takes: the channels that accept it, the guides that show it and the
|
|
473
|
+
* interactions that work on it go by these. */
|
|
474
|
+
output: ScaleOutput;
|
|
475
|
+
input: ScaleInput;
|
|
476
|
+
/** The mapping; the core adds `output` and `input` from the declaration. */
|
|
477
|
+
create(spec: ScaleSpec, ctx: ScaleCtx): ScaleMapping;
|
|
478
|
+
/** true: a domain it infers comes from fields alone, and a constant a channel writes (a rule at one category) adds
|
|
479
|
+
* nothing to it. A band takes its categories from the data; a number scale stretches to show a constant. */
|
|
480
|
+
fieldsOnly?: boolean;
|
|
481
|
+
/** true: equal differences of value are equal distances on screen (linear, time; a log scale is not). A mark that
|
|
482
|
+
* widens a domain by a share of a step between values (half a bar) does it only on such a scale. */
|
|
483
|
+
even?: boolean;
|
|
484
|
+
/** How charts that share this scale (the panels of a facet) put their domains together: "union" takes every value any
|
|
485
|
+
* of them has, in the order first seen (categories); "extent" from the least to the greatest (numbers, dates). Without
|
|
486
|
+
* it each panel keeps its own. A scale that declares a `nice` param is not rounded again: each panel already was. */
|
|
487
|
+
merge?: 'union' | 'extent';
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** What a view-dependent transform may look at: where the plot is and the scales as they are drawn now (zoomed). */
|
|
491
|
+
interface TransformView {
|
|
492
|
+
readonly plot: Rect;
|
|
493
|
+
readonly scales: Readonly<Record<string, Scale>>;
|
|
494
|
+
/** Report something worth knowing that is not an error (data left as it is because it is unsorted, ...). It becomes a
|
|
495
|
+
* warning in the chart's diagnostics, once, however often the transform reruns. */
|
|
496
|
+
warn(message: string): void;
|
|
497
|
+
/** true on the run that gives the scales their domains: the plot is laid out, nothing is zoomed, and the scales are
|
|
498
|
+
* about to take their domains from what this run writes, so theirs now are only a guess (from the run without a
|
|
499
|
+
* view). A transform that bins for the plot's shape bins for `plot` then, not for the scales. */
|
|
500
|
+
readonly fitting?: boolean;
|
|
501
|
+
/** On the run with `fitting`: what else a scale is to hold besides the values the marks read from it (room the
|
|
502
|
+
* transform keeps around what it wrote: hexagons regular on a plot of any shape), as MarkDef.reach for a mark. */
|
|
503
|
+
reach?(scale: string, values: unknown[]): void;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
interface TransformDef extends BlockBase {
|
|
507
|
+
role: 'transform';
|
|
508
|
+
/** true = the result depends on what is visible (level of detail, bins that refine when zooming). The transform,
|
|
509
|
+
* and every transform after it, runs again at every redraw with `view`; the ones before it run once. Scale
|
|
510
|
+
* domains are inferred from the full data. A function can do without a view: it runs once without one for a first
|
|
511
|
+
* guess at the domains, and again with `fitting` once the plot is laid out; `true` cannot (decimate). */
|
|
512
|
+
viewDependent?: boolean | ((spec: TransformSpec) => boolean);
|
|
513
|
+
apply(table: Table, spec: TransformSpec, view?: TransformView): Table;
|
|
514
|
+
/** Names of the columns `apply` adds, so validation still knows every field after the transform ran. */
|
|
515
|
+
outputs?(spec: TransformSpec): string[];
|
|
516
|
+
/** What a reader calls the columns it adds ("Q1", "Count"), by their names: a tooltip shows a field under this name
|
|
517
|
+
* unless its own `labels` names it. */
|
|
518
|
+
labels?(spec: TransformSpec): Record<string, string>;
|
|
519
|
+
/** The result is a new table, not the old one plus columns (group-by, binning): after it only the `outputs` exist, and
|
|
520
|
+
* validation flags a reference to a column the transform dropped. */
|
|
521
|
+
replacesColumns?: boolean;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** A datum a mark found near a pointer position (logical px, same space as the display list). */
|
|
525
|
+
interface Pick {
|
|
526
|
+
row: number;
|
|
527
|
+
/** Anchor for a marker / tooltip. */
|
|
528
|
+
x: number;
|
|
529
|
+
y: number;
|
|
530
|
+
/** 0 = the pointer is on the shape, larger = farther. Lets an interaction choose between marks. */
|
|
531
|
+
dist: number;
|
|
532
|
+
/** The shape's own bounding box (logical px), when it has one (rect marks: bar, heatmap). An interaction can
|
|
533
|
+
* highlight this instead of a dot at `x`/`y` — a dot on a bar's edge reads as a rendering glitch, not a marker. */
|
|
534
|
+
box?: { x: number; y: number; w: number; h: number };
|
|
535
|
+
/** For a shape that stands for several rows, not for one (a Sankey node): what a tooltip shows instead of `row`'s fields.
|
|
536
|
+
* `row` still has to be a row of the table. A custom `content` function is not called for it. */
|
|
537
|
+
card?: { title: string; color?: string; lines: [string, string][] };
|
|
538
|
+
/** Shapes a tooltip lights up while this is hovered, as SVG paths in chart coordinates (the flows of a Sankey node). */
|
|
539
|
+
flows?: { d: string; fill: string }[];
|
|
540
|
+
/** Every row the shape stands for, `row` among them, when the mark draws several rows as one: a line through a total
|
|
541
|
+
* that a transform repeated on every row of its quarter, dots on one spot. A tooltip shows what they have in common. */
|
|
542
|
+
rows?: number[];
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** What a mark can learn about its channels before the scales exist (see MarkDef.reach). */
|
|
546
|
+
interface MarkReachAsk {
|
|
547
|
+
/** What the scale a channel of the mark is on declares; undefined for a channel without a scale. */
|
|
548
|
+
scale(channel: string): { input: ScaleInput; even: boolean } | undefined;
|
|
549
|
+
/** The column a channel reads; undefined for a constant or a channel not given. */
|
|
550
|
+
column(channel: string): Column | undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
interface MarkDef extends BlockBase {
|
|
554
|
+
role: 'mark';
|
|
555
|
+
channels: Readonly<Record<string, ChannelDef>>;
|
|
556
|
+
/** Pure: spec + context → primitives for the "marks" layer. `index` is the mark's position in spec.marks.
|
|
557
|
+
* Line/area emit ONE PathPrim per series (single pass building one `d` string), never one primitive
|
|
558
|
+
* per point — that's what keeps large series cheap for the SVG renderer (see uPlot's single-Path2D-per-series). */
|
|
559
|
+
render(spec: MarkSpec, ctx: ChartCtx, index: number): Prim[];
|
|
560
|
+
/** Primitives drawn above the marks and outside the plot's clip: labels that stand past the plot's edge (the name at
|
|
561
|
+
* the end of a line), which a zoom would otherwise cut off. */
|
|
562
|
+
labels?(spec: MarkSpec, ctx: ChartCtx, index: number): Prim[];
|
|
563
|
+
/** Values the scale of `channel` has to hold besides the data, because the mark draws past its first and last value:
|
|
564
|
+
* a bar standing at a date is half a bar wide on each side. Asked before any scale exists, from the spec and the data;
|
|
565
|
+
* null when the mark draws within its values. */
|
|
566
|
+
reach?(spec: MarkSpec, channel: string, ask: MarkReachAsk): unknown[] | null;
|
|
567
|
+
/** Hit-testing lives with the mark that knows its shapes. Return the closest datum within a mark-specific
|
|
568
|
+
* reach of (x, y), or null. Interactions (tooltip) call it on every mark and keep the smallest `dist`. */
|
|
569
|
+
pick?(spec: MarkSpec, ctx: ChartCtx, index: number, x: number, y: number): Pick | null;
|
|
570
|
+
// What the mark offers to interactions, which read these rather than naming marks:
|
|
571
|
+
/** It draws each series as one shape through its rows in x order (a line, an area): a crosshair snaps to it. */
|
|
572
|
+
series?: boolean;
|
|
573
|
+
/** How interaction.animate brings it in: bars grow from their baseline ("grow"), series are drawn from left to
|
|
574
|
+
* right ("wipe"), slices open from the center ("pop"). Without one it fades in. */
|
|
575
|
+
enter?: 'grow' | 'wipe' | 'pop';
|
|
576
|
+
/** The color it draws `row` in, as its render does: the dot a tooltip shows by the heading and its marker. '' when
|
|
577
|
+
* the row has no color (a category the scale hides). A mark without it (a label in the text color) shows none. */
|
|
578
|
+
color?(spec: MarkSpec, ctx: ChartCtx, index: number, row: number): string;
|
|
579
|
+
/** It draws as well at any width: shapes with no text beside them (a mark that writes labels says so only when it
|
|
580
|
+
* writes none). A chart whose marks and guides all say so (a tile: a figure over a sparkline) is laid out at its
|
|
581
|
+
* container's width down to 100 px; any other chart scales down as a drawing below 200 px. */
|
|
582
|
+
narrow?: boolean | ((spec: MarkSpec) => boolean);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** What a guide needs around the plot. The side keys are space the guide occupies and add up with other guides
|
|
586
|
+
* (they stack). `overhang` is different: how far past the plot edge something the guide draws sticks out — the end
|
|
587
|
+
* label of a horizontal axis is centered on the plot edge and needs half its width. Overhangs of all guides combine
|
|
588
|
+
* by taking the largest, and only widen the margin if the padding and the stacked guides do not already cover them. */
|
|
589
|
+
interface GuideMeasure extends Partial<Insets> {
|
|
590
|
+
overhang?: Partial<Insets>;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
interface GuideDef extends BlockBase {
|
|
594
|
+
role: 'guide';
|
|
595
|
+
/** Space the guide needs around the plot (axis labels), keyed by its own side
|
|
596
|
+
* (see GuideSpec.position). Called before the plot rect is final. */
|
|
597
|
+
measure?(spec: GuideSpec, ctx: ChartCtx): GuideMeasure;
|
|
598
|
+
/** `offset` is the inset already claimed on this guide's side by earlier
|
|
599
|
+
* entries in `spec.guides` (array order) — e.g. a second y-axis with
|
|
600
|
+
* `position: "right"` uses it to draw outside the first one instead of
|
|
601
|
+
* on top of it. Zero on all sides when this is the only guide there. */
|
|
602
|
+
render(spec: GuideSpec, ctx: ChartCtx, offset: Insets): Emission;
|
|
603
|
+
/** For a guide that writes the categories of a band scale (an axis): how many of its labels fit along `length` px,
|
|
604
|
+
* the way it writes them. ChartCtx.ticks shows every category that fits for the guides of that scale. */
|
|
605
|
+
fits?(spec: GuideSpec, ctx: ChartCtx, length: number): number;
|
|
606
|
+
/** It shows its scale and nothing of the panel it is in (a legend). On a facet whose panels share that scale, and the
|
|
607
|
+
* scale is of categories (merged by "union"), the page draws it once above them, as a row of swatches. */
|
|
608
|
+
shownOnce?: boolean;
|
|
609
|
+
/** It draws as well at any width (MarkDef.narrow): lines with no text (a grid), or text it fits to the width itself
|
|
610
|
+
* (a KPI). */
|
|
611
|
+
narrow?: boolean | ((spec: GuideSpec) => boolean);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
interface InteractionDef extends BlockBase {
|
|
615
|
+
role: 'interaction';
|
|
616
|
+
/** For an overlay that sits in a corner of the chart: which one, and how far along its edge from the end of the chart.
|
|
617
|
+
* Read at layout, so what is drawn along that edge can keep clear of it. */
|
|
618
|
+
corner?(spec: InteractionSpec, ctx: ChartCtx): { edge: 'top' | 'bottom'; side: 'left' | 'right'; width: number } | null;
|
|
619
|
+
/** It reads the chart along x at any height (a crosshair): ChartCtx.alongX is true while it is on the chart. */
|
|
620
|
+
alongX?: boolean;
|
|
621
|
+
/** It can follow a legend outside the chart (legend-filter): a page that draws one legend for several charts, as the
|
|
622
|
+
* composer does above a facet, gives it `legend`, the id of that element, and `scale`, the scale the legend shows. */
|
|
623
|
+
outerLegend?: boolean;
|
|
624
|
+
/** The scale whose categories it switches from the legend in the chart (legend-filter), or undefined: ChartCtx.toggled. */
|
|
625
|
+
toggles?(spec: InteractionSpec, ctx: ChartCtx): string | undefined;
|
|
626
|
+
attach(spec: InteractionSpec, live: LiveCtx): void;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
interface Rendered {
|
|
630
|
+
/** Stays the same element across updates: interactions attach listeners to it once. */
|
|
631
|
+
root: Element;
|
|
632
|
+
/** Redraw in place with a new display list (same size, possibly different primitives). */
|
|
633
|
+
update(list: DisplayList): void;
|
|
634
|
+
/** For renderers without elements to hit-test (canvas): the `data` of the topmost primitive under the pointer.
|
|
635
|
+
* Left out, the core reads `data-*` attributes of the event target instead. */
|
|
636
|
+
dataAt?(ev: MouseEvent): Record<string, string> | null;
|
|
637
|
+
destroy(): void;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
interface RendererDef extends BlockBase {
|
|
641
|
+
role: 'renderer';
|
|
642
|
+
render(list: DisplayList, host: HTMLElement): Rendered;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/** Code that other blocks share (a curve, say). A spec never names it; a block that uses it lists it in `requires`
|
|
646
|
+
* and reaches it through the core: (BC.get('lib.curve') as LibDef).exports. */
|
|
647
|
+
interface LibDef extends BlockBase {
|
|
648
|
+
role: 'lib';
|
|
649
|
+
exports: Readonly<Record<string, unknown>>;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
type BlockDef = LibDef | ScaleDef | TransformDef | MarkDef | GuideDef | InteractionDef | RendererDef;
|
|
653
|
+
|
|
654
|
+
/** How to write a number or a date: a preset name ("percent", "compact", "integer", "year", "month", "day", "date",
|
|
655
|
+
* "datetime"), or an object of Intl.NumberFormat / Intl.DateTimeFormat options plus a few of our own. JSON-friendly on purpose. */
|
|
656
|
+
interface FormatSpec {
|
|
657
|
+
/** BCP 47 locale, default "en-US" (fixed, so a report reads the same for every reader). */
|
|
658
|
+
locale?: string;
|
|
659
|
+
/** "number" or "date"; guessed from the options when omitted. */
|
|
660
|
+
kind?: 'number' | 'date';
|
|
661
|
+
prefix?: string;
|
|
662
|
+
suffix?: string;
|
|
663
|
+
/** e.g. { style: "currency", currency: "USD", maximumFractionDigits: 0 }, { month: "short", year: "2-digit" } */
|
|
664
|
+
[intlOption: string]: unknown;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// ───────────────────────── runtime (core.js) ─────────────────────────
|
|
668
|
+
|
|
669
|
+
interface Diagnostic {
|
|
670
|
+
level: 'error' | 'warn';
|
|
671
|
+
/** JSON-path-ish location in the spec, e.g. "marks[1].y". */
|
|
672
|
+
path: string;
|
|
673
|
+
message: string;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
interface ChartHandle {
|
|
677
|
+
readonly host: HTMLElement;
|
|
678
|
+
readonly spec: ChartSpec;
|
|
679
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
680
|
+
/** Resolves once the chart has been built for the first time (after its dataset finished decoding). */
|
|
681
|
+
readonly ready: Promise<void>;
|
|
682
|
+
/** Rebuild with a new spec. The current zoom is kept for every scale that still exists with the same type
|
|
683
|
+
* (except where the new spec sets its own `view`); `resetView: true` starts from the spec instead. */
|
|
684
|
+
update(spec: ChartSpec, options?: { resetView?: boolean }): void;
|
|
685
|
+
/** External control of the visible domain, same mechanism the zoom interaction uses. Called while the dataset
|
|
686
|
+
* is still decoding, it is kept and the chart is built with it, over the spec's own `view`. */
|
|
687
|
+
setView(scale: string, domain: unknown[] | null): void;
|
|
688
|
+
/** The visible domain of every scale; while the dataset is still decoding, only the views set so far. */
|
|
689
|
+
getView(): Record<string, unknown[]>;
|
|
690
|
+
destroy(): void;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
interface Runtime {
|
|
694
|
+
readonly version: string;
|
|
695
|
+
|
|
696
|
+
/** Idempotent: same name+version is ignored, different version warns and keeps the first. */
|
|
697
|
+
define(def: BlockDef): void;
|
|
698
|
+
has(name: BlockName): boolean;
|
|
699
|
+
get(name: BlockName): BlockDef | undefined;
|
|
700
|
+
blocks(): BlockName[];
|
|
701
|
+
/** The palette color of an index: what a color scale gives its n-th category. */
|
|
702
|
+
color(index: number): string;
|
|
703
|
+
/** The colors of the palette, light, as `color(index)` falls back on them where a page sets no `--bc-c<N>`. A page
|
|
704
|
+
* in another theme (dark) sets the variables; the composer writes these for the light one. */
|
|
705
|
+
readonly palette: readonly string[];
|
|
706
|
+
|
|
707
|
+
/** Add/replace a dataset in the data lake (the page's #bc-data is loaded on mount). Rows and columns are
|
|
708
|
+
* available at once; an EncodedDataset is decoded in the background (a failure is reported by the charts
|
|
709
|
+
* that use it, and by console.error). A malformed plain dataset throws. */
|
|
710
|
+
data(name: string, input: DatasetInput): void;
|
|
711
|
+
/** Resolves when every dataset that was being decoded has settled. Never rejects. */
|
|
712
|
+
ready(): Promise<void>;
|
|
713
|
+
/** Register a function that specs can refer to by name (e.g. tooltip `content: "myTip"`), because JSON
|
|
714
|
+
* cannot carry code. Last registration wins; specs look the name up when they use it. */
|
|
715
|
+
defineFn(name: string, fn: (...args: any[]) => unknown): void;
|
|
716
|
+
/** A function that writes values as the spec says (numbers, or dates in UTC). Missing values give "". Built once per
|
|
717
|
+
* spec and cached. An invalid spec throws an Error that says what is wrong. `kind` fixes number or date. */
|
|
718
|
+
formatter(spec: string | FormatSpec, kind?: 'number' | 'date'): (value: unknown) => string;
|
|
719
|
+
/** The one way blockcharts reads a time: epoch milliseconds, a Date, or an ISO string, as epoch ms; NaN when it is not
|
|
720
|
+
* one (a number past the range of dates is not a time). A string without an offset ("2024-03-01", "2024-03-01T09:00",
|
|
721
|
+
* "2024-03-01 09:00") is UTC, like everything else, so every reader sees the same hours whatever their time zone. */
|
|
722
|
+
time(value: unknown): number;
|
|
723
|
+
getFn(name: string): ((...args: any[]) => unknown) | undefined;
|
|
724
|
+
|
|
725
|
+
/** Direct block names a spec uses (e.g. ["scale.time","mark.line","guide.axis","renderer.svg"]).
|
|
726
|
+
* Pure function of the spec; the same mapping a composer applies with the manifest closures. */
|
|
727
|
+
needs(spec: ChartSpec): BlockName[];
|
|
728
|
+
/** Missing blocks, unknown params, dangling scale/field references. Never throws. */
|
|
729
|
+
validate(spec: ChartSpec): Diagnostic[];
|
|
730
|
+
/** Whether every mark and guide of the spec draws as well at any width (MarkDef.narrow, GuideDef.narrow): such a chart
|
|
731
|
+
* is laid out at its container's width down to 100 px, and a page may put it two to a row on a phone (a KPI tile).
|
|
732
|
+
* False when it uses a block that is not defined. */
|
|
733
|
+
narrow(spec: ChartSpec): boolean;
|
|
734
|
+
/** A dataset as a table, for the composer. Not on a composed page. */
|
|
735
|
+
table?(name: string): Table | undefined;
|
|
736
|
+
|
|
737
|
+
chart(host: HTMLElement, spec: ChartSpec): ChartHandle;
|
|
738
|
+
/** Load #bc-data, find every <script data-bc-chart>, mount. Runs automatically on DOMContentLoaded, unless
|
|
739
|
+
* `globalThis.BC_CONFIG = { autoMount: false }` was set before the core loaded. */
|
|
740
|
+
mount(root?: ParentNode): ChartHandle[];
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// ───────────────────────── build output ─────────────────────────
|
|
744
|
+
|
|
745
|
+
/** manifest.json, generated by the build from the registry — nobody edits it. */
|
|
746
|
+
interface ManifestEntry {
|
|
747
|
+
file: string;
|
|
748
|
+
bytes: number;
|
|
749
|
+
version: number;
|
|
750
|
+
requires: BlockName[];
|
|
751
|
+
/** Transitive requires including the block itself; a composer unions these across a page. */
|
|
752
|
+
closure: BlockName[];
|
|
753
|
+
params?: Record<string, ParamDef>;
|
|
754
|
+
channels?: Record<string, ChannelDef>;
|
|
755
|
+
doc?: string;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
interface Manifest {
|
|
759
|
+
core: { file: string; bytes: number; version: string };
|
|
760
|
+
blocks: Record<BlockName, ManifestEntry>;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
declare const BC: BC.Runtime;
|