swiftplot 0.5.1 → 0.6.0-rc.0
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/README.md +8 -7
- package/dist/chart.d.ts +2 -1
- package/dist/chart.js +129 -29
- package/dist/charts/build.d.ts +2 -2
- package/dist/charts/build.js +24 -1
- package/dist/charts/candlestick.js +3 -0
- package/dist/charts/controller.d.ts +1 -1
- package/dist/charts/controller.js +14 -3
- package/dist/charts/defaults.d.ts +5 -1
- package/dist/charts/defaults.js +56 -20
- package/dist/charts/distribution.js +20 -5
- package/dist/charts/parts.js +1 -1
- package/dist/charts/types.d.ts +32 -7
- package/dist/charts/xy.d.ts +1 -0
- package/dist/charts/xy.js +81 -17
- package/dist/color.d.ts +7 -3
- package/dist/color.js +11 -7
- package/dist/features/animate.js +7 -7
- package/dist/features/brush.js +19 -14
- package/dist/features/legend.js +14 -14
- package/dist/features/pointer.d.ts +14 -1
- package/dist/features/pointer.js +53 -2
- package/dist/features/tooltip.d.ts +2 -2
- package/dist/features/tooltip.js +58 -18
- package/dist/features/zoom.d.ts +4 -0
- package/dist/features/zoom.js +30 -22
- package/dist/gl.d.ts +1 -1
- package/dist/gl.js +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/marks/arcs.d.ts +5 -0
- package/dist/marks/arcs.js +21 -14
- package/dist/scale.d.ts +4 -2
- package/dist/scale.js +30 -9
- package/dist/scales/band.js +2 -0
- package/dist/scales/bare.d.ts +2 -1
- package/dist/scales/bare.js +3 -3
- package/dist/scales/log.js +3 -1
- package/dist/scales/time.js +2 -1
- package/dist/schema.json +1 -0
- package/dist/text.js +1 -0
- package/dist/transforms.d.ts +6 -0
- package/dist/transforms.js +13 -0
- package/dist/types.d.ts +21 -3
- package/package.json +4 -3
package/dist/charts/types.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export type YAxisProps = AxisProps & {
|
|
|
21
21
|
format?: (v: number) => string;
|
|
22
22
|
/** Which series the automatic extent covers: those shown, all of them, or those inside the x window. @default 'shown' */
|
|
23
23
|
fit?: Axis['fit'];
|
|
24
|
-
/** 'nice' widens the extent to whole steps, 'data' keeps it exact. @default 'nice' */
|
|
24
|
+
/** 'nice' widens the extent to whole steps, 'data' keeps it exact; `[0, 'nice']` fixes one end and fits the other. @default 'nice' */
|
|
25
25
|
domain?: Axis['domain'];
|
|
26
26
|
};
|
|
27
27
|
export type XAxisProps = AxisProps & {
|
|
@@ -31,7 +31,7 @@ export type XAxisProps = AxisProps & {
|
|
|
31
31
|
format?: (v: number) => string;
|
|
32
32
|
/** On numbers and dates, which series the automatic extent covers: those shown or all of them; text and bars ignore it. @default 'shown' */
|
|
33
33
|
fit?: Axis['fit'];
|
|
34
|
-
/** On numbers 'nice' (the default) widens to whole steps
|
|
34
|
+
/** On numbers 'nice' (the default) widens to whole steps, 'data' keeps it exact and `[0, 'nice']` fixes one end; dates fit the data unless given `[from, to]` in ms; text and bars ignore it, but reject one open end. */
|
|
35
35
|
domain?: Axis['domain'];
|
|
36
36
|
/** Reads dates without a zone, and labels the axis, in UTC; `false` uses local time, except on bars, which are always UTC. @default true */
|
|
37
37
|
utc?: boolean;
|
|
@@ -52,6 +52,10 @@ export type Axes = {
|
|
|
52
52
|
/** Called with the window shown once zoom or pan has moved it; never for update(). */
|
|
53
53
|
onViewChange?: Options['onViewChange'];
|
|
54
54
|
};
|
|
55
|
+
type Aligned = {
|
|
56
|
+
/** 'zero' puts zero at the same height on y and y2, widening each to take it in; a number given in `domain` is never moved, and both axes must be linear. */
|
|
57
|
+
align?: 'zero';
|
|
58
|
+
};
|
|
55
59
|
type ScaledYAxisProps = YAxisProps & {
|
|
56
60
|
/** 'log' spaces equal ratios equally; a value at or below zero is then a gap, and `step` does not apply. @default 'linear' */
|
|
57
61
|
scale?: 'linear' | 'log';
|
|
@@ -60,7 +64,7 @@ export type ScaledAxes = {
|
|
|
60
64
|
/** Settings for the y axis. */
|
|
61
65
|
yAxis?: ScaledYAxisProps;
|
|
62
66
|
/** Settings for the right-hand axis of `y2`, which is drawn only with it. */
|
|
63
|
-
y2Axis?: ScaledYAxisProps;
|
|
67
|
+
y2Axis?: ScaledYAxisProps & Aligned;
|
|
64
68
|
};
|
|
65
69
|
/** A date, as a Date, in ms or as an ISO string, a number, or a category: whichever the x axis shows. */
|
|
66
70
|
export type XValue = number | string | Date;
|
|
@@ -110,14 +114,18 @@ export type Overlays = {
|
|
|
110
114
|
};
|
|
111
115
|
export type SecondAxis = {
|
|
112
116
|
/** Settings for the right-hand axis of `y2`, which is drawn only with it. */
|
|
113
|
-
y2Axis?: YAxisProps;
|
|
117
|
+
y2Axis?: YAxisProps & Aligned;
|
|
114
118
|
};
|
|
115
119
|
export type Base = {
|
|
116
120
|
/** Bump after changing the data in place, so that it is converted again. */
|
|
117
121
|
dataVersion?: number;
|
|
118
122
|
/** Series colours in order; `palette`, or `paletteDark` on the dark theme, by default. */
|
|
119
123
|
colors?: readonly string[];
|
|
120
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* 'auto' follows the CSS `color-scheme` of the chart's element, then a `dark` or `light` marker on `<body>` or `<html>`
|
|
126
|
+
* (a class, `data-theme` and the like), then the system when that `color-scheme` allows both light and dark, else light.
|
|
127
|
+
* A change to those two elements' attributes is followed, and re-reads CSS variables, whatever the theme. @default 'auto'
|
|
128
|
+
*/
|
|
121
129
|
theme?: 'auto' | 'light' | 'dark' | Theme;
|
|
122
130
|
/** The hover tooltip, with each value in the format of its axis, else two decimals at most. @default true */
|
|
123
131
|
tooltip?: boolean;
|
|
@@ -135,6 +143,8 @@ export type Base = {
|
|
|
135
143
|
margin?: Options['margin'];
|
|
136
144
|
/** What assistive technology reads for the chart; by default the axis labels and the series names. */
|
|
137
145
|
description?: string;
|
|
146
|
+
/** Text written in the middle of the frame while there is no data to draw, such as 'No data'. */
|
|
147
|
+
empty?: string;
|
|
138
148
|
/** The series not drawn, by name; kept by the chart unless given. */
|
|
139
149
|
hidden?: string[];
|
|
140
150
|
/** Called with the hidden series once the legend has changed them; never for update(). */
|
|
@@ -237,16 +247,22 @@ export type ErrorBarsOn<F> = ErrorBars<F> & {
|
|
|
237
247
|
export type LineProps<D> = Props<D> & ScaledAxes & Overlays & Curve & {
|
|
238
248
|
/** Shades the area under each line down to the bottom of the plot: `true` for a faint fade of its colour, or a gradient. @default false */
|
|
239
249
|
fill?: boolean | Gradient;
|
|
250
|
+
/** Draws each series as its running total in x order, a missing value adding zero, and takes no errorBars; the tooltip gives the total and then the row's own value. @default false */
|
|
251
|
+
cumulative?: boolean;
|
|
240
252
|
/** An upright bar from low to high at each x, under the line and in its colour; takes one y, with series or without. */
|
|
241
253
|
errorBars?: ErrorBars<NonNullable<Exclude<Props<D>['y'], readonly unknown[]>>>;
|
|
242
254
|
};
|
|
243
255
|
export type AreaProps<D> = Props<D> & Overlays & Curve & {
|
|
244
256
|
/** Stacks the series of each y axis, a missing value counting as zero; unstacked areas overlap, half transparent. @default false */
|
|
245
257
|
stack?: boolean;
|
|
258
|
+
/** Draws each series as its running total in x order, a missing value adding zero, before stack lays them on each other; the tooltip gives the total and then the row's own value. @default false */
|
|
259
|
+
cumulative?: boolean;
|
|
246
260
|
};
|
|
247
261
|
export type BarProps<D> = Props<D> & Overlays & {
|
|
248
262
|
/** Stacks the series, not with `y2`; unstacked bars stand side by side. @default false */
|
|
249
263
|
stack?: boolean;
|
|
264
|
+
/** Draws each series as its running total in the order of its categories, a missing value adding zero, before stack lays them on each other, and takes no errorBars; the tooltip gives the total and then the row's own value. @default false */
|
|
265
|
+
cumulative?: boolean;
|
|
250
266
|
/** An upright bar from low to high over each bar, in the text colour; takes one y, and no stacked series. */
|
|
251
267
|
errorBars?: ErrorBars<NonNullable<Exclude<Props<D>['y'], readonly unknown[]>>>;
|
|
252
268
|
};
|
|
@@ -311,6 +327,11 @@ export type PieProps<D> = PartProps<D> & {
|
|
|
311
327
|
text?: boolean;
|
|
312
328
|
/** Each slice starts at this opacity, from 0 to 1, and reaches its full colour at its end; solid when left out. */
|
|
313
329
|
sweep?: number;
|
|
330
|
+
/**
|
|
331
|
+
* Text in the middle of the donut, or a function of the total of the positive values. A share it
|
|
332
|
+
* would cover is dropped; with `inner` 0 it is drawn over the slices.
|
|
333
|
+
*/
|
|
334
|
+
center?: string | ((total: number) => string);
|
|
314
335
|
};
|
|
315
336
|
export type FunnelProps<D> = PartProps<D> & {
|
|
316
337
|
/** Writes each stage's name and its share of the first stage. @default true */
|
|
@@ -438,9 +459,11 @@ export type HistogramProps<D> = {
|
|
|
438
459
|
} & (0 extends 1 & D ? LooseHistogramProps : [D] extends [ArrayLike<number | null | undefined>] ? HistogramNumberProps : [D] extends [readonly (readonly unknown[])[]] ? LooseHistogramProps : [D] extends [readonly (infer R)[]] ? HistogramRowProps<R> : [D] extends [Record<string, ArrayLike<unknown>>] ? HistogramColumnProps<D> : LooseHistogramProps) & {
|
|
439
460
|
/** How many equal bins span the values, at most 1,000,000, or the bin edges in ascending order; Sturges' rule when left out. */
|
|
440
461
|
bins?: number | Col;
|
|
462
|
+
/** Each bar holds every value up to its bin's upper edge: `true` for the count, `'share'` for the share of all values counted, from 0 to 1. @default false */
|
|
463
|
+
cumulative?: boolean | 'share';
|
|
441
464
|
/** The bars' colour, as the first of a list. */
|
|
442
465
|
colors?: readonly string[];
|
|
443
|
-
/** The y axis of the counts, titled `count` unless given a label. */
|
|
466
|
+
/** The y axis of the counts, titled `count`, `cumulative count` or `cumulative share` unless given a label. */
|
|
444
467
|
yAxis?: YAxisProps;
|
|
445
468
|
};
|
|
446
469
|
type BoxPlotBase = Base & {
|
|
@@ -543,12 +566,14 @@ type ComboBase = Axes & ScaledAxes & Overlays & Base & {
|
|
|
543
566
|
colors?: readonly string[];
|
|
544
567
|
/** Stacks the bars, not with both `bars` and `y2Bars`; unstacked bars stand side by side. @default false */
|
|
545
568
|
stack?: boolean;
|
|
569
|
+
/** Draws each series as its running total in x order, or in the order of the categories for a text x, a missing value adding zero, before stack lays the bars on each other; the tooltip gives the total and then the row's own value. @default false */
|
|
570
|
+
cumulative?: boolean;
|
|
546
571
|
/** Settings for the x axis: a category per row for text, else a scale of dates or numbers, on which the bars of one x take 0.6 of the smallest gap. */
|
|
547
572
|
xAxis?: XAxisProps;
|
|
548
573
|
/** Settings for the y axis; a log scale is for lines alone. */
|
|
549
574
|
yAxis?: ScaledYAxisProps;
|
|
550
575
|
/** Settings for the right-hand axis of `y2Lines` and `y2Bars`, drawn only with one of them; a log scale is for lines alone. */
|
|
551
|
-
y2Axis?: ScaledYAxisProps;
|
|
576
|
+
y2Axis?: ScaledYAxisProps & Aligned;
|
|
552
577
|
};
|
|
553
578
|
export type ComboRowProps<R> = ComboBase & {
|
|
554
579
|
/** Row objects; missing, null or empty draws an empty frame. */
|
package/dist/charts/xy.d.ts
CHANGED
package/dist/charts/xy.js
CHANGED
|
@@ -7,9 +7,10 @@ import { errorBars } from '../marks/errorBars.js';
|
|
|
7
7
|
import { line } from '../marks/line.js';
|
|
8
8
|
import { rule, zone } from '../marks/rule.js';
|
|
9
9
|
import { ticks } from '../marks/ticks.js';
|
|
10
|
+
import { closed } from '../scale.js';
|
|
10
11
|
import { band } from '../scales/band.js';
|
|
11
12
|
import { log } from '../scales/log.js';
|
|
12
|
-
import { bands } from '../transforms.js';
|
|
13
|
+
import { bands, cumulate } from '../transforms.js';
|
|
13
14
|
import { dates, defined, easyChart, featuresOf, install, linear, on, sizeOf } from './build.js';
|
|
14
15
|
import { bandTitle, colorsFor, DAY, formatNumber, namesOf, spacing, timeTitle } from './defaults.js';
|
|
15
16
|
import { assign, list } from './fields.js';
|
|
@@ -25,8 +26,8 @@ const columns = (s, cut) => ({
|
|
|
25
26
|
...(s.low && { low: cut(s.low) }),
|
|
26
27
|
...(s.high && { high: cut(s.high) }),
|
|
27
28
|
});
|
|
28
|
-
/** Line and area need ascending x; equal x keep their order. */
|
|
29
|
-
function sorted(ys) {
|
|
29
|
+
/** Line and area need ascending x; equal x keep their order. With keep, each moved series keeps its rows' order. */
|
|
30
|
+
function sorted(ys, keep) {
|
|
30
31
|
const orders = new Map();
|
|
31
32
|
const order = (x) => {
|
|
32
33
|
if (!orders.has(x))
|
|
@@ -40,7 +41,12 @@ function sorted(ys) {
|
|
|
40
41
|
return s;
|
|
41
42
|
if (!xs.has(s.x))
|
|
42
43
|
xs.set(s.x, Float64Array.from(o, i => s.x[i]));
|
|
43
|
-
return {
|
|
44
|
+
return {
|
|
45
|
+
...s,
|
|
46
|
+
x: xs.get(s.x),
|
|
47
|
+
...columns(s, c => Float64Array.from(o, i => c[i])),
|
|
48
|
+
...(keep && { order: o }),
|
|
49
|
+
};
|
|
44
50
|
});
|
|
45
51
|
}
|
|
46
52
|
/** Each series' values at the slot of its x in order, NaN where it has no row, the last row winning. */
|
|
@@ -90,6 +96,15 @@ function whole(s, rows, length) {
|
|
|
90
96
|
});
|
|
91
97
|
return { x, y };
|
|
92
98
|
}
|
|
99
|
+
/** A sorted series' y put back in the order of its rows. */
|
|
100
|
+
function inRows(raw, r) {
|
|
101
|
+
const { order } = r;
|
|
102
|
+
if (!order)
|
|
103
|
+
return { x: raw.x, y: r.y };
|
|
104
|
+
const y = new Float64Array(order.length);
|
|
105
|
+
order.forEach((row, i) => (y[row] = r.y[i]));
|
|
106
|
+
return { x: raw.x, y };
|
|
107
|
+
}
|
|
93
108
|
function overlays(p, name, theme, y, xAt, onBand) {
|
|
94
109
|
const neither = (what) => {
|
|
95
110
|
throw new Error(`${name}: ${what} takes x or y`);
|
|
@@ -172,6 +187,9 @@ export function xy(name, kind = 'line') {
|
|
|
172
187
|
const spread = kind === 'area' ? undefined : p.errorBars;
|
|
173
188
|
const assigned = memo('fields', [s, p.x, ...list(p.y), p.series, rField, utc, spread?.low, spread?.high, spread?.axis, ...list(p.y2)], () => assign(s, { ...p, errorBars: spread, xAxis: { utc } }, scatter ? 'scatter' : 'xy', name, kind === 'bar'));
|
|
174
189
|
const stacked = kind !== 'line' && !scatter && !!p.stack;
|
|
190
|
+
const cumulative = !scatter && !!p.cumulative;
|
|
191
|
+
if (spread && cumulative)
|
|
192
|
+
throw new Error(`${name}: errorBars do not apply to running totals. Leave out cumulative or errorBars.`);
|
|
175
193
|
if (spread && stacked && assigned.ys.length > 1)
|
|
176
194
|
throw new Error(`${name}: errorBars do not apply to stacked bars. Leave out stack or errorBars.`);
|
|
177
195
|
if (stacked && new Set(assigned.ys.filter((_, i) => barred(i)).map(r => r.yAxis)).size > 1)
|
|
@@ -181,7 +199,7 @@ export function xy(name, kind = 'line') {
|
|
|
181
199
|
: undefined;
|
|
182
200
|
const placed = bars?.ys ??
|
|
183
201
|
(scatter ? assigned.ys : undefined) ??
|
|
184
|
-
memo('sorted', [assigned, stacked], () => stacked && p.series !== undefined ? aligned(assigned.ys) : sorted(assigned.ys));
|
|
202
|
+
memo('sorted', [assigned, stacked, combo && cumulative], () => stacked && p.series !== undefined ? aligned(assigned.ys) : sorted(assigned.ys, combo && cumulative));
|
|
185
203
|
const { x: ax, y: ay } = assigned;
|
|
186
204
|
const room = p.barred?.includes(true) && !bars
|
|
187
205
|
? memo('spacing', [placed], () => spacing(placed[0]?.x ?? [], ax.kind === 'time' ? DAY : 1))
|
|
@@ -192,6 +210,8 @@ export function xy(name, kind = 'line') {
|
|
|
192
210
|
const label = xa.label ?? ax.label;
|
|
193
211
|
const categories = bars?.categories ?? ax.categories ?? [];
|
|
194
212
|
const banded = !!bars || ax.kind === 'text';
|
|
213
|
+
if (banded || ax.kind === 'time')
|
|
214
|
+
closed(xa.domain, `${name}: xAxis.domain`, banded ? 'categories' : 'dates');
|
|
195
215
|
const x = banded
|
|
196
216
|
? band({ label, categories })
|
|
197
217
|
: ax.kind === 'time'
|
|
@@ -211,16 +231,34 @@ export function xy(name, kind = 'line') {
|
|
|
211
231
|
if (role === 'yAxis' && ay.categories)
|
|
212
232
|
throw new Error(`${name}: yAxis.scale 'log' does not apply to a text y`);
|
|
213
233
|
const { domain } = a;
|
|
234
|
+
closed(domain, `${name}: ${role}.domain`, 'a log scale');
|
|
214
235
|
if (Array.isArray(domain) && !domain.every(v => v > 0 && v < Infinity))
|
|
215
236
|
throw new Error(`${name}: ${role}.domain must be above zero and finite on a log scale`);
|
|
216
237
|
return log(defined({ label: a.label ?? own, domain, format: a.format, fit: a.fit }));
|
|
217
238
|
};
|
|
218
239
|
const numeric = scaled('yAxis', ay.label);
|
|
240
|
+
if (ay.categories)
|
|
241
|
+
closed(ya.domain, `${name}: yAxis.domain`, 'categories');
|
|
219
242
|
const y = ay.categories
|
|
220
243
|
? band({ label: ya.label ?? ay.label, categories: ay.categories })
|
|
221
244
|
: numeric;
|
|
222
|
-
const
|
|
223
|
-
|
|
245
|
+
const align = p.y2Axis?.align;
|
|
246
|
+
if (align === 'zero' && assigned.y2) {
|
|
247
|
+
const off = p.y2Axis?.scale === 'log'
|
|
248
|
+
? 'a log scale'
|
|
249
|
+
: ya.scale === 'log'
|
|
250
|
+
? 'a log y'
|
|
251
|
+
: ay.categories
|
|
252
|
+
? 'a text y'
|
|
253
|
+
: undefined;
|
|
254
|
+
if (off)
|
|
255
|
+
throw new Error(`${name}: y2Axis.align 'zero' does not apply to ${off}`);
|
|
256
|
+
}
|
|
257
|
+
const y2 = assigned.y2 && { ...scaled('y2Axis', assigned.y2.label), ...(align && { align }) };
|
|
258
|
+
const totals = cumulative
|
|
259
|
+
? memo('cumulative', [placed], () => placed.map(r => ({ ...r, y: cumulate(r.y) })))
|
|
260
|
+
: placed;
|
|
261
|
+
const ys = memo('ys', [totals, !!y.col, !!y2?.col], () => totals.map(r => {
|
|
224
262
|
const a = r.yAxis ? y2 : y;
|
|
225
263
|
if (!a.col)
|
|
226
264
|
return r;
|
|
@@ -289,7 +327,9 @@ export function xy(name, kind = 'line') {
|
|
|
289
327
|
};
|
|
290
328
|
if (barred(i)) {
|
|
291
329
|
// Clicks and `selected` number a bar by its row in the data.
|
|
292
|
-
const own = combo
|
|
330
|
+
const own = combo
|
|
331
|
+
? whole(bars ? r : cumulative ? inRows(assigned.ys[i], r) : assigned.ys[i], assigned.rows, s.length)
|
|
332
|
+
: r;
|
|
293
333
|
const drawn = bar({
|
|
294
334
|
key: `series:${r.name}`,
|
|
295
335
|
label: r.name,
|
|
@@ -332,13 +372,18 @@ export function xy(name, kind = 'line') {
|
|
|
332
372
|
const titled = !bars && ax.kind === 'time' && p.tooltip !== false
|
|
333
373
|
? memo('timeTitle', [ys, utc], () => timeTitle([...new Set(ys.map(r => r.x))], utc))
|
|
334
374
|
: formatNumber;
|
|
375
|
+
const snapped = kind !== 'bar' && (combo || cumulative) && p.tooltip !== false
|
|
376
|
+
? memo('snapped', [ys], () => {
|
|
377
|
+
const xs = [...new Set(ys.map(r => r.x))];
|
|
378
|
+
return xs.length === 1
|
|
379
|
+
? xs[0]
|
|
380
|
+
: Float64Array.from(new Set(xs.flatMap(x => Array.from(x)))).sort();
|
|
381
|
+
})
|
|
382
|
+
: undefined;
|
|
335
383
|
const text = {
|
|
336
384
|
x: (v) => (!banded && xa.format ? xa.format : text.title)(v),
|
|
337
385
|
title: banded ? v => bandTitle(categories, v) : titled,
|
|
338
|
-
snap: v =>
|
|
339
|
-
const x = ys[0]?.x ?? [];
|
|
340
|
-
return x[nearest(x, v)] ?? v;
|
|
341
|
-
},
|
|
386
|
+
snap: v => (snapped ? (snapped[nearest(snapped, v)] ?? v) : v),
|
|
342
387
|
value: (v, axis, observed) => {
|
|
343
388
|
if (axis === 'y' && ay.categories)
|
|
344
389
|
return bandTitle(ay.categories, v);
|
|
@@ -357,7 +402,7 @@ export function xy(name, kind = 'line') {
|
|
|
357
402
|
}
|
|
358
403
|
return format(best ?? +v.toPrecision(13));
|
|
359
404
|
},
|
|
360
|
-
around: (x, axis) =>
|
|
405
|
+
around: (x, axis) => totals.flatMap(r => {
|
|
361
406
|
if ((r.yAxis ?? 'y') !== axis)
|
|
362
407
|
return [];
|
|
363
408
|
const after = upTo(r.x, x);
|
|
@@ -365,9 +410,24 @@ export function xy(name, kind = 'line') {
|
|
|
365
410
|
return rows.flatMap(i => r.y[i] ?? []);
|
|
366
411
|
}),
|
|
367
412
|
at: (label, row) => {
|
|
368
|
-
const own =
|
|
413
|
+
const own = totals.find(r => r.name === label)?.y[row ?? -1];
|
|
369
414
|
return own === undefined ? [] : [own];
|
|
370
415
|
},
|
|
416
|
+
...(cumulative && {
|
|
417
|
+
added: (label, row) => {
|
|
418
|
+
const r = placed.find(r => r.name === label);
|
|
419
|
+
const own = r?.y[row];
|
|
420
|
+
if (own === undefined || !Number.isFinite(own))
|
|
421
|
+
return '';
|
|
422
|
+
const format = (r.yAxis ? p.y2Axis : ya)?.format ?? formatNumber;
|
|
423
|
+
return ` (${own < 0 ? '' : '+'}${format(own)})`;
|
|
424
|
+
},
|
|
425
|
+
}),
|
|
426
|
+
row: (label, x) => {
|
|
427
|
+
const at = placed.find(r => r.name === label)?.x ?? [];
|
|
428
|
+
const after = upTo(at, x);
|
|
429
|
+
return at[after - 1] === x ? after - 1 : -1;
|
|
430
|
+
},
|
|
371
431
|
axis: label => ys.find(r => r.name === label)?.yAxis ?? 'y',
|
|
372
432
|
onBrush: p.onBrush,
|
|
373
433
|
};
|
|
@@ -396,13 +456,17 @@ export function xy(name, kind = 'line') {
|
|
|
396
456
|
: kind === 'bar'
|
|
397
457
|
? {
|
|
398
458
|
mode: 'nearest',
|
|
399
|
-
text: (label, _x, v, row) => `${ref.current.title(row)} · ${label}: ${own(label, v)}`,
|
|
459
|
+
text: (label, _x, v, row) => `${ref.current.title(row)} · ${label}: ${own(label, v)}${ref.current.added?.(label, row) ?? ''}`,
|
|
400
460
|
}
|
|
401
461
|
: {
|
|
402
462
|
mode: 'x',
|
|
403
|
-
|
|
463
|
+
snap: v => ref.current.snap(v),
|
|
404
464
|
title: v => ref.current.title((shownX = v)),
|
|
405
|
-
value: (v, axis
|
|
465
|
+
value: (v, axis, label) => {
|
|
466
|
+
const { value, around, added, row } = ref.current;
|
|
467
|
+
const shown = value(v, axis, () => around(shownX, axis));
|
|
468
|
+
return added ? shown + added(label, row(label, shownX)) : shown;
|
|
469
|
+
},
|
|
406
470
|
});
|
|
407
471
|
if (features.brush)
|
|
408
472
|
drawn.push(brush({
|
package/dist/color.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
/** The window an element is in, or this one for an element of a document that has none. */
|
|
2
|
+
export declare const windowOf: (el: Node) => Window & typeof globalThis;
|
|
3
|
+
/** Whether a colour depends on where it is drawn, which a canvas cannot tell. */
|
|
4
|
+
export declare const placed: (css: string) => boolean;
|
|
1
5
|
/**
|
|
2
|
-
* CSS variables in a colour, resolved where the chart stands: anything but a
|
|
3
|
-
* back as it is. A colour with no value becomes `muted`, with one warning.
|
|
4
|
-
* until forget() or reread().
|
|
6
|
+
* CSS variables and `light-dark()` in a colour, resolved where the chart stands: anything but a
|
|
7
|
+
* string naming one comes back as it is. A colour with no value becomes `muted`, with one warning.
|
|
8
|
+
* What was read is kept until forget() or reread().
|
|
5
9
|
*/
|
|
6
10
|
export declare function resolver(root: HTMLElement, muted: () => string): {
|
|
7
11
|
color: <T>(css: T) => T | string;
|
package/dist/color.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { light } from './theme.js';
|
|
2
|
+
/** The window an element is in, or this one for an element of a document that has none. */
|
|
3
|
+
export const windowOf = (el) => (el.ownerDocument?.defaultView ?? globalThis);
|
|
4
|
+
/** Whether a colour depends on where it is drawn, which a canvas cannot tell. */
|
|
5
|
+
export const placed = (css) => /var\(|light-dark\(/i.test(css);
|
|
2
6
|
/**
|
|
3
|
-
* CSS variables in a colour, resolved where the chart stands: anything but a
|
|
4
|
-
* back as it is. A colour with no value becomes `muted`, with one warning.
|
|
5
|
-
* until forget() or reread().
|
|
7
|
+
* CSS variables and `light-dark()` in a colour, resolved where the chart stands: anything but a
|
|
8
|
+
* string naming one comes back as it is. A colour with no value becomes `muted`, with one warning.
|
|
9
|
+
* What was read is kept until forget() or reread().
|
|
6
10
|
*/
|
|
7
11
|
export function resolver(root, muted) {
|
|
8
12
|
const known = new Map();
|
|
9
13
|
const warned = new Set();
|
|
10
14
|
const read = (css) => {
|
|
11
|
-
const probe =
|
|
15
|
+
const probe = root.ownerDocument.createElement('div');
|
|
12
16
|
// The root's own values, against a page's rules for its children and properties that do not inherit.
|
|
13
17
|
// Every word is taken, as CSS escapes can spell even a name's dashes.
|
|
14
18
|
const names = css.match(/(?:\\[\da-f]{1,6}(?:\r\n|\s)?|\\.|[\w-]|[^\0-\x7f])+/gi) ?? [];
|
|
@@ -18,13 +22,13 @@ export function resolver(root, muted) {
|
|
|
18
22
|
// value each keeps its own, so they differ.
|
|
19
23
|
probe.style.setProperty('flood-color', css, 'important');
|
|
20
24
|
probe.style.setProperty('lighting-color', css, 'important');
|
|
21
|
-
(root.isConnected ? root :
|
|
22
|
-
const { floodColor, lightingColor } = getComputedStyle(probe);
|
|
25
|
+
(root.isConnected ? root : root.ownerDocument.body).append(probe);
|
|
26
|
+
const { floodColor, lightingColor } = windowOf(root).getComputedStyle(probe);
|
|
23
27
|
probe.remove();
|
|
24
28
|
return floodColor === lightingColor ? floodColor : undefined;
|
|
25
29
|
};
|
|
26
30
|
const color = (css) => {
|
|
27
|
-
if (typeof css !== 'string' || !css
|
|
31
|
+
if (typeof css !== 'string' || !placed(css))
|
|
28
32
|
return css;
|
|
29
33
|
let found = known.get(css);
|
|
30
34
|
if (found !== undefined)
|
package/dist/features/animate.js
CHANGED
|
@@ -13,8 +13,8 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
13
13
|
if (!Number.isFinite(duration) || duration <= 0)
|
|
14
14
|
return () => ({ destroy() { } });
|
|
15
15
|
return chart => {
|
|
16
|
-
const { canvas } = chart;
|
|
17
|
-
const motion = matchMedia('(prefers-reduced-motion: reduce)');
|
|
16
|
+
const { canvas, win } = chart;
|
|
17
|
+
const motion = win.matchMedia('(prefers-reduced-motion: reduce)');
|
|
18
18
|
const ctx = canvas.getContext('2d');
|
|
19
19
|
const from = new OffscreenCanvas(0, 0);
|
|
20
20
|
const to = new OffscreenCanvas(0, 0);
|
|
@@ -29,7 +29,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
29
29
|
picture.getContext('2d').drawImage(canvas, 0, 0);
|
|
30
30
|
};
|
|
31
31
|
const stop = () => {
|
|
32
|
-
cancelAnimationFrame(frame);
|
|
32
|
+
win.cancelAnimationFrame(frame);
|
|
33
33
|
frame = 0;
|
|
34
34
|
entering = false;
|
|
35
35
|
shown = undefined;
|
|
@@ -37,7 +37,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
37
37
|
chart.morph(1);
|
|
38
38
|
};
|
|
39
39
|
const run = (step, drawn = false) => {
|
|
40
|
-
const start = performance.now();
|
|
40
|
+
const start = win.performance.now();
|
|
41
41
|
const show = (e) => {
|
|
42
42
|
step(e);
|
|
43
43
|
shown = () => step(e);
|
|
@@ -46,13 +46,13 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
46
46
|
const t = motion.matches ? 1 : Math.min(1, Math.max(0, (now - start) / duration));
|
|
47
47
|
show(1 - (1 - t) ** 3);
|
|
48
48
|
if (t < 1)
|
|
49
|
-
frame = requestAnimationFrame(tick);
|
|
49
|
+
frame = win.requestAnimationFrame(tick);
|
|
50
50
|
else
|
|
51
51
|
stop();
|
|
52
52
|
};
|
|
53
53
|
if (!drawn)
|
|
54
54
|
show(0);
|
|
55
|
-
frame = requestAnimationFrame(tick);
|
|
55
|
+
frame = win.requestAnimationFrame(tick);
|
|
56
56
|
};
|
|
57
57
|
// 'copy' replaces the canvas with what is drawn, so the pictures need no clearing first.
|
|
58
58
|
const blend = (mix) => (t) => {
|
|
@@ -89,7 +89,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
89
89
|
return shown();
|
|
90
90
|
}
|
|
91
91
|
const now = performance.now();
|
|
92
|
-
const still = why !== 'data' || frame !== 0 || now - last < duration ||
|
|
92
|
+
const still = why !== 'data' || frame !== 0 || now - last < duration || chart.doc.hidden || motion.matches;
|
|
93
93
|
stop();
|
|
94
94
|
if (why === 'data')
|
|
95
95
|
last = now;
|
package/dist/features/brush.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { locate, settle, SLOP_PX, unselect } from './pointer.js';
|
|
1
|
+
import { fittedWidth, locate, narrowest, reach, settle, SLOP_PX, unselect, widthOf } from './pointer.js';
|
|
2
2
|
export function brush(b = {}) {
|
|
3
3
|
const [bx, by] = ['x', 'y'].map(k => (b.axes ?? 'x').includes(k));
|
|
4
4
|
return chart => {
|
|
5
|
-
const { drag } = chart;
|
|
5
|
+
const { drag, doc } = chart;
|
|
6
6
|
drag.brush = true;
|
|
7
7
|
const shade = chart.layer('div', 'display:none;opacity:0.2');
|
|
8
8
|
let reported = false;
|
|
@@ -28,11 +28,11 @@ export function brush(b = {}) {
|
|
|
28
28
|
if (!over)
|
|
29
29
|
return;
|
|
30
30
|
from = { id: e.pointerId, x: px, y: py };
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
doc.addEventListener('pointermove', move, true);
|
|
32
|
+
doc.addEventListener('pointerup', up, true);
|
|
33
|
+
doc.addEventListener('pointercancel', cancel, true);
|
|
34
|
+
doc.addEventListener('keydown', key, true);
|
|
35
|
+
doc.addEventListener('selectstart', unselect, true);
|
|
36
36
|
clear();
|
|
37
37
|
};
|
|
38
38
|
const move = (e) => {
|
|
@@ -74,11 +74,11 @@ export function brush(b = {}) {
|
|
|
74
74
|
};
|
|
75
75
|
const end = () => {
|
|
76
76
|
from = undefined;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
doc.removeEventListener('pointermove', move, true);
|
|
78
|
+
doc.removeEventListener('pointerup', up, true);
|
|
79
|
+
doc.removeEventListener('pointercancel', cancel, true);
|
|
80
|
+
doc.removeEventListener('keydown', key, true);
|
|
81
|
+
doc.removeEventListener('selectstart', unselect, true);
|
|
82
82
|
if (drag.active)
|
|
83
83
|
settling = settle(chart);
|
|
84
84
|
};
|
|
@@ -109,7 +109,12 @@ export function brush(b = {}) {
|
|
|
109
109
|
if (!b.zoom || gone)
|
|
110
110
|
return;
|
|
111
111
|
clear();
|
|
112
|
-
|
|
112
|
+
const t = Math.min(narrowest.get(chart) ?? 0, widthOf(chart, view.x));
|
|
113
|
+
const c = (picked.x[0] + picked.x[1]) / 2;
|
|
114
|
+
const f = (h) => fittedWidth(chart, [c - h, c + h]);
|
|
115
|
+
const h = widthOf(chart, picked.x) < t && reach(f, t, 0, Math.abs(view.x[1] - view.x[0]), t / 2);
|
|
116
|
+
const x = h ? [c - h, c + h] : picked.x;
|
|
117
|
+
chart.propose({ ...(bx && { x }), ...(by && { y: picked.y }) });
|
|
113
118
|
};
|
|
114
119
|
chart.root.addEventListener('pointerdown', down);
|
|
115
120
|
return {
|
|
@@ -118,7 +123,7 @@ export function brush(b = {}) {
|
|
|
118
123
|
gone = true;
|
|
119
124
|
from = undefined;
|
|
120
125
|
end();
|
|
121
|
-
clearTimeout(settling);
|
|
126
|
+
chart.win.clearTimeout(settling);
|
|
122
127
|
drag.brush = drag.active = false;
|
|
123
128
|
chart.root.removeEventListener('pointerdown', down);
|
|
124
129
|
shade.remove();
|
package/dist/features/legend.js
CHANGED
|
@@ -17,12 +17,12 @@ function ends(lo, hi) {
|
|
|
17
17
|
*/
|
|
18
18
|
export function legend({ toggle = true, rows: cap } = {}) {
|
|
19
19
|
return chart => {
|
|
20
|
-
const { root } = chart;
|
|
20
|
+
const { root, doc } = chart;
|
|
21
21
|
const toggles = (e) => toggle && !e.part;
|
|
22
22
|
const off = (e) => e.id !== undefined && chart.hidden().includes(e.id);
|
|
23
23
|
const box = chart.layer('div', `color:${chart.theme().text}`);
|
|
24
24
|
const shown = new Map();
|
|
25
|
-
const cut = new ResizeObserver(seen => {
|
|
25
|
+
const cut = new chart.win.ResizeObserver(seen => {
|
|
26
26
|
for (const { target: t } of seen) {
|
|
27
27
|
if (t.scrollWidth > t.clientWidth)
|
|
28
28
|
t.setAttribute('title', t.textContent);
|
|
@@ -103,19 +103,19 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
103
103
|
const key = JSON.stringify([rows, unlisted, strips]);
|
|
104
104
|
if (key !== built) {
|
|
105
105
|
built = key;
|
|
106
|
-
const
|
|
107
|
-
const focused = [...shown].find(([item]) => item.contains(
|
|
106
|
+
const tree = root.getRootNode();
|
|
107
|
+
const focused = [...shown].find(([item]) => item.contains(tree.activeElement))?.[1].id;
|
|
108
108
|
shown.clear();
|
|
109
109
|
cut.disconnect();
|
|
110
110
|
box.replaceChildren(...rows.map((row, r) => {
|
|
111
|
-
const line =
|
|
111
|
+
const line = doc.createElement('div');
|
|
112
112
|
line.style.cssText = `display:flex;gap:${GAP}px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
113
113
|
const capped = r === rows.length - 1 && unlisted.length > 0;
|
|
114
114
|
for (const e of row) {
|
|
115
|
-
const item = line.appendChild(
|
|
115
|
+
const item = line.appendChild(doc.createElement('span'));
|
|
116
116
|
// Only the entries take the pointer: the rest of the strip stays the chart's.
|
|
117
117
|
item.style.cssText = `pointer-events:auto;${capped ? 'flex:0 1 auto;min-width:0' : 'flex:none;max-width:100%'};overflow:hidden;text-overflow:ellipsis`;
|
|
118
|
-
const swatch = item.appendChild(
|
|
118
|
+
const swatch = item.appendChild(doc.createElement('span'));
|
|
119
119
|
swatch.style.cssText =
|
|
120
120
|
'display:inline-block;width:10px;height:10px;margin-right:6px;border-radius:2px';
|
|
121
121
|
swatch.style.backgroundColor = e.color;
|
|
@@ -129,16 +129,16 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
129
129
|
shown.set(item, e);
|
|
130
130
|
}
|
|
131
131
|
if (capped) {
|
|
132
|
-
const item = line.appendChild(
|
|
132
|
+
const item = line.appendChild(doc.createElement('span'));
|
|
133
133
|
item.style.cssText = 'pointer-events:auto;flex:none';
|
|
134
134
|
item.textContent = more(unlisted.length);
|
|
135
135
|
item.title = unlisted.join(', ');
|
|
136
136
|
}
|
|
137
137
|
return line;
|
|
138
138
|
}), ...strips.map(s => {
|
|
139
|
-
const line =
|
|
139
|
+
const line = doc.createElement('div');
|
|
140
140
|
line.style.cssText = `display:flex;gap:6px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
141
|
-
const strip =
|
|
141
|
+
const strip = doc.createElement('span');
|
|
142
142
|
strip.style.cssText = 'display:inline-block;width:120px;height:10px;border-radius:2px';
|
|
143
143
|
strip.style.backgroundImage = `linear-gradient(to right,${s.colors.join()})`;
|
|
144
144
|
line.append(...(s.label ? [s.label, ' '] : []), s.ends[0], strip, s.ends[1]);
|
|
@@ -213,13 +213,13 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
213
213
|
host.addEventListener('pointermove', move, true);
|
|
214
214
|
root.addEventListener('pointerleave', lift);
|
|
215
215
|
root.addEventListener('pointercancel', leave);
|
|
216
|
-
|
|
216
|
+
doc.addEventListener('pointerdown', leave, true);
|
|
217
217
|
return {
|
|
218
218
|
// The entries may have moved under a pointer at rest, so what it is over is looked up again.
|
|
219
219
|
update() {
|
|
220
220
|
box.style.color = chart.theme().text;
|
|
221
|
-
const
|
|
222
|
-
if (at && !over(
|
|
221
|
+
const tree = root.isConnected && root.getRootNode();
|
|
222
|
+
if (at && !over(tree ? tree.elementFromPoint(...at) : null))
|
|
223
223
|
at = undefined;
|
|
224
224
|
},
|
|
225
225
|
letGo: () => held && leave(),
|
|
@@ -228,7 +228,7 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
228
228
|
host.removeEventListener('pointermove', move, true);
|
|
229
229
|
root.removeEventListener('pointerleave', lift);
|
|
230
230
|
root.removeEventListener('pointercancel', leave);
|
|
231
|
-
|
|
231
|
+
doc.removeEventListener('pointerdown', leave, true);
|
|
232
232
|
cut.disconnect();
|
|
233
233
|
box.remove();
|
|
234
234
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Internals } from '../types.js';
|
|
1
|
+
import type { Internals, Span } from '../types.js';
|
|
2
2
|
export declare const SLOP_PX = 4;
|
|
3
3
|
export declare const unselect: (e: Event) => void;
|
|
4
4
|
/** Clears `drag.active` after the click the browser sends for this release, which a drag must not count as one. */
|
|
@@ -44,3 +44,16 @@ export declare function locate(chart: Internals, e: MouseEvent): {
|
|
|
44
44
|
};
|
|
45
45
|
over: boolean;
|
|
46
46
|
};
|
|
47
|
+
export declare const widthOf: (chart: Internals, s: Span) => number;
|
|
48
|
+
/** The width in axis units of the x span s once the chart fits it inside its domain: a span pushed off a log edge narrows or widens. */
|
|
49
|
+
export declare const fittedWidth: (chart: Internals, s: Span) => number;
|
|
50
|
+
/** The narrowest x span, in axis units, that zoom() lets a gesture reach on a chart; a brush's zoom keeps to it too. */
|
|
51
|
+
export declare const narrowest: WeakMap<Internals, number>;
|
|
52
|
+
/** Where between lo and hi the increasing f reaches target, trying `guess` first: it is exact on a linear axis. */
|
|
53
|
+
export declare const reach: (f: (t: number) => number, target: number, lo: number, hi: number, guess: number) => number;
|
|
54
|
+
/**
|
|
55
|
+
* A zoom factor for the x span s about the value a, at the share `at` of the span, held so the span in axis units goes no
|
|
56
|
+
* further past `min` or `max` than it already is: a pinch that moves to where a log axis is wider or narrower can need
|
|
57
|
+
* a factor on the other side of 1.
|
|
58
|
+
*/
|
|
59
|
+
export declare const limited: (chart: Internals, s: Span, k: number, min?: number, max?: number, at?: number, a?: number) => number;
|