swiftplot 0.5.2 → 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 +112 -16
- 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 +54 -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 +5 -3
- package/dist/color.js +6 -4
- package/dist/features/brush.js +7 -2
- package/dist/features/pointer.d.ts +14 -1
- package/dist/features/pointer.js +50 -0
- package/dist/features/tooltip.d.ts +2 -2
- package/dist/features/tooltip.js +54 -14
- package/dist/features/zoom.d.ts +4 -0
- package/dist/features/zoom.js +11 -3
- 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/transforms.d.ts +6 -0
- package/dist/transforms.js +13 -0
- package/dist/types.d.ts +14 -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,9 +1,11 @@
|
|
|
1
1
|
/** The window an element is in, or this one for an element of a document that has none. */
|
|
2
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;
|
|
3
5
|
/**
|
|
4
|
-
* CSS variables in a colour, resolved where the chart stands: anything but a
|
|
5
|
-
* back as it is. A colour with no value becomes `muted`, with one warning.
|
|
6
|
-
* 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().
|
|
7
9
|
*/
|
|
8
10
|
export declare function resolver(root: HTMLElement, muted: () => string): {
|
|
9
11
|
color: <T>(css: T) => T | string;
|
package/dist/color.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { light } from './theme.js';
|
|
2
2
|
/** The window an element is in, or this one for an element of a document that has none. */
|
|
3
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);
|
|
4
6
|
/**
|
|
5
|
-
* CSS variables in a colour, resolved where the chart stands: anything but a
|
|
6
|
-
* back as it is. A colour with no value becomes `muted`, with one warning.
|
|
7
|
-
* 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().
|
|
8
10
|
*/
|
|
9
11
|
export function resolver(root, muted) {
|
|
10
12
|
const known = new Map();
|
|
@@ -26,7 +28,7 @@ export function resolver(root, muted) {
|
|
|
26
28
|
return floodColor === lightingColor ? floodColor : undefined;
|
|
27
29
|
};
|
|
28
30
|
const color = (css) => {
|
|
29
|
-
if (typeof css !== 'string' || !css
|
|
31
|
+
if (typeof css !== 'string' || !placed(css))
|
|
30
32
|
return css;
|
|
31
33
|
let found = known.get(css);
|
|
32
34
|
if (found !== undefined)
|
package/dist/features/brush.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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 => {
|
|
@@ -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 {
|
|
@@ -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;
|
package/dist/features/pointer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { clamp } from '../chart.js';
|
|
1
2
|
export const SLOP_PX = 4;
|
|
2
3
|
export const unselect = (e) => e.preventDefault();
|
|
3
4
|
/** Clears `drag.active` after the click the browser sends for this release, which a drag must not count as one. */
|
|
@@ -40,3 +41,52 @@ export function locate(chart, e) {
|
|
|
40
41
|
const { inside, ...where } = position(chart, e.clientX, e.clientY);
|
|
41
42
|
return { ...where, over: inside && chart.frame() === 'axes' && e.target === chart.root };
|
|
42
43
|
}
|
|
44
|
+
export const widthOf = (chart, s) => Math.abs(chart.real('x', s[1]) - chart.real('x', s[0]));
|
|
45
|
+
/** 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. */
|
|
46
|
+
export const fittedWidth = (chart, s) => {
|
|
47
|
+
const { x } = chart.domain();
|
|
48
|
+
return widthOf(chart, clamp(s, x) ?? x);
|
|
49
|
+
};
|
|
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 const narrowest = new WeakMap();
|
|
52
|
+
/** Where between lo and hi the increasing f reaches target, trying `guess` first: it is exact on a linear axis. */
|
|
53
|
+
export const reach = (f, target, lo, hi, guess) => {
|
|
54
|
+
if (Math.abs(f(guess) - target) <= target * 1e-9)
|
|
55
|
+
return guess;
|
|
56
|
+
for (let i = 0; i < 60; i++) {
|
|
57
|
+
const m = (lo + hi) / 2;
|
|
58
|
+
if (f(m) < target)
|
|
59
|
+
lo = m;
|
|
60
|
+
else
|
|
61
|
+
hi = m;
|
|
62
|
+
}
|
|
63
|
+
return (lo + hi) / 2;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* 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
|
|
67
|
+
* further past `min` or `max` than it already is: a pinch that moves to where a log axis is wider or narrower can need
|
|
68
|
+
* a factor on the other side of 1.
|
|
69
|
+
*/
|
|
70
|
+
export const limited = (chart, s, k, min = 0, max = Infinity, at = 0.5, a = s[0] + at * (s[1] - s[0])) => {
|
|
71
|
+
const d = s[1] - s[0];
|
|
72
|
+
const moved = a !== s[0] + at * d;
|
|
73
|
+
if (!min && max === Infinity)
|
|
74
|
+
return k;
|
|
75
|
+
if (!moved && (k > 1 ? max === Infinity : !min))
|
|
76
|
+
return k;
|
|
77
|
+
const w = widthOf(chart, s);
|
|
78
|
+
if (!w)
|
|
79
|
+
return k;
|
|
80
|
+
const f = (t) => fittedWidth(chart, [a - at * d * t, a + (1 - at) * d * t]);
|
|
81
|
+
const [low, high, now] = [Math.min(min, w), Math.max(max, w), f(k)];
|
|
82
|
+
if (now >= low && now <= high)
|
|
83
|
+
return k;
|
|
84
|
+
const target = now > high ? high : low;
|
|
85
|
+
const { x } = chart.domain();
|
|
86
|
+
let [lo, hi] = [Math.min(k, 1), Math.max(Math.min(k, Math.abs((x[1] - x[0]) / d)), 1)];
|
|
87
|
+
for (let i = 0; i < 60 && f(lo) > target; i++)
|
|
88
|
+
lo /= 2;
|
|
89
|
+
for (let i = 0; i < 60 && f(hi) < target; i++)
|
|
90
|
+
hi *= 2;
|
|
91
|
+
return reach(f, target, lo, hi, target / w);
|
|
92
|
+
};
|
|
@@ -7,8 +7,8 @@ export type TooltipOptions =
|
|
|
7
7
|
mode: 'x';
|
|
8
8
|
/** The first line of the tooltip, for the x at the pointer. */
|
|
9
9
|
title: (x: number) => string;
|
|
10
|
-
/** A mark's value as text, after its label, for its y on the axis it is drawn against. */
|
|
11
|
-
value: (y: number, axis: 'y' | 'y2') => string;
|
|
10
|
+
/** A mark's value as text, after its label, for its y on the axis it is drawn against; label is the mark's label. */
|
|
11
|
+
value: (y: number, axis: 'y' | 'y2', label: string) => string;
|
|
12
12
|
/** The x to read the title and the marks at, such as the nearest row's, from the one at the pointer, both in the marks' space. */
|
|
13
13
|
snap?: (x: number) => number;
|
|
14
14
|
}
|
package/dist/features/tooltip.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NEAREST_PX } from '../chart.js';
|
|
2
|
-
import { owns, position, released } from './pointer.js';
|
|
2
|
+
import { owns, position, released, strayed } from './pointer.js';
|
|
3
3
|
const FOCUS_PX = 30;
|
|
4
4
|
export function tooltip(t) {
|
|
5
5
|
return chart => {
|
|
@@ -12,13 +12,19 @@ export function tooltip(t) {
|
|
|
12
12
|
Object.assign(tip.style, { color: text, background: surface, borderColor: border });
|
|
13
13
|
};
|
|
14
14
|
restyle();
|
|
15
|
+
// The pointer in the page and where the chart was then: a hover follows the page, a pin keeps to the chart.
|
|
15
16
|
let at = null;
|
|
16
17
|
let held = false;
|
|
17
18
|
// The user's pointer is on the chart, maybe over another feature's layer, so the app's is not shown.
|
|
18
19
|
let inside = false;
|
|
19
20
|
let fromApp = false;
|
|
20
|
-
|
|
21
|
+
// On another feature's layer, such as a legend, whose focus a tip let go must leave alone.
|
|
22
|
+
let aside = false;
|
|
23
|
+
const conceal = () => {
|
|
21
24
|
cross.style.display = tip.style.display = 'none';
|
|
25
|
+
};
|
|
26
|
+
const hide = () => {
|
|
27
|
+
conceal();
|
|
22
28
|
chart.focus(null);
|
|
23
29
|
};
|
|
24
30
|
const show = (x, y, rows) => {
|
|
@@ -43,7 +49,9 @@ export function tooltip(t) {
|
|
|
43
49
|
let py;
|
|
44
50
|
if (at) {
|
|
45
51
|
fromApp = false;
|
|
46
|
-
|
|
52
|
+
const [x, y, left, top] = at;
|
|
53
|
+
const box = held ? root.getBoundingClientRect() : { left, top };
|
|
54
|
+
({ px, py } = position(chart, x + box.left - left, y + box.top - top));
|
|
47
55
|
}
|
|
48
56
|
else {
|
|
49
57
|
const p = inside ? null : chart.pointer();
|
|
@@ -97,7 +105,7 @@ export function tooltip(t) {
|
|
|
97
105
|
: s && (s.band ? chart.real(on, s.y2) - chart.real(on, s.y1) : chart.real(on, s.value ?? s.y2));
|
|
98
106
|
if (k.label !== undefined) {
|
|
99
107
|
rows.push({
|
|
100
|
-
text: `${k.label}: ${value === undefined ? '–' : t.value(value, on)}`,
|
|
108
|
+
text: `${k.label}: ${value === undefined ? '–' : t.value(value, on, k.label)}`,
|
|
101
109
|
color: k.color,
|
|
102
110
|
});
|
|
103
111
|
}
|
|
@@ -127,20 +135,24 @@ export function tooltip(t) {
|
|
|
127
135
|
};
|
|
128
136
|
const move = (e) => {
|
|
129
137
|
inside = true;
|
|
138
|
+
aside = !owns(chart, e.target);
|
|
139
|
+
if (held) {
|
|
140
|
+
if (!aside)
|
|
141
|
+
place();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
130
144
|
if (chart.drag.active || chart.drag.child) {
|
|
131
145
|
at = null;
|
|
132
146
|
return hide();
|
|
133
147
|
}
|
|
134
148
|
// Over another feature's layer, such as a legend, the focus is that feature's to set.
|
|
135
|
-
if (
|
|
149
|
+
if (aside) {
|
|
136
150
|
at = null;
|
|
137
|
-
held = false;
|
|
138
151
|
fromApp = false;
|
|
139
|
-
|
|
140
|
-
return;
|
|
152
|
+
return conceal();
|
|
141
153
|
}
|
|
142
|
-
|
|
143
|
-
|
|
154
|
+
const { left, top } = root.getBoundingClientRect();
|
|
155
|
+
at = [e.clientX, e.clientY, left, top];
|
|
144
156
|
place();
|
|
145
157
|
};
|
|
146
158
|
const release = (e) => {
|
|
@@ -159,7 +171,9 @@ export function tooltip(t) {
|
|
|
159
171
|
};
|
|
160
172
|
// A lifted finger leaves the chart, and a tip it tapped open is meant to stay until a tap elsewhere.
|
|
161
173
|
const lift = (e) => {
|
|
162
|
-
inside = false;
|
|
174
|
+
inside = aside = false;
|
|
175
|
+
if (held)
|
|
176
|
+
return;
|
|
163
177
|
if (e.pointerType !== 'touch')
|
|
164
178
|
return out();
|
|
165
179
|
held = !!at && tip.style.display !== 'none';
|
|
@@ -173,27 +187,53 @@ export function tooltip(t) {
|
|
|
173
187
|
if (at)
|
|
174
188
|
out();
|
|
175
189
|
};
|
|
190
|
+
const pin = (e) => {
|
|
191
|
+
if (e.target !== root || strayed(chart, e) || chart.drag.active)
|
|
192
|
+
return;
|
|
193
|
+
held = !!at && tip.style.display !== 'none';
|
|
194
|
+
};
|
|
195
|
+
const letGo = () => {
|
|
196
|
+
if (!held)
|
|
197
|
+
return;
|
|
198
|
+
held = false;
|
|
199
|
+
at = null;
|
|
200
|
+
if (aside)
|
|
201
|
+
conceal();
|
|
202
|
+
else
|
|
203
|
+
hide();
|
|
204
|
+
};
|
|
205
|
+
const escape = (e) => {
|
|
206
|
+
if (held && e.key === 'Escape') {
|
|
207
|
+
letGo();
|
|
208
|
+
place();
|
|
209
|
+
}
|
|
210
|
+
};
|
|
176
211
|
root.addEventListener('pointerdown', move);
|
|
177
212
|
// Capturing, so a child that stops its own events still moves the tip and brings it back.
|
|
178
213
|
root.addEventListener('pointermove', move, true);
|
|
179
214
|
root.addEventListener('pointerup', release, true);
|
|
180
215
|
root.addEventListener('pointerleave', lift);
|
|
181
216
|
root.addEventListener('pointercancel', out);
|
|
217
|
+
root.addEventListener('click', pin);
|
|
182
218
|
doc.addEventListener('pointerdown', away, true);
|
|
219
|
+
doc.addEventListener('keydown', escape);
|
|
183
220
|
return {
|
|
184
221
|
update() {
|
|
185
222
|
restyle();
|
|
186
|
-
|
|
223
|
+
if (!(held && aside))
|
|
224
|
+
place();
|
|
187
225
|
},
|
|
188
|
-
//
|
|
189
|
-
letGo
|
|
226
|
+
// What a tap or a click pinned is let go: the chart under it is redrawn.
|
|
227
|
+
letGo,
|
|
190
228
|
destroy() {
|
|
191
229
|
root.removeEventListener('pointerdown', move);
|
|
192
230
|
root.removeEventListener('pointermove', move, true);
|
|
193
231
|
root.removeEventListener('pointerup', release, true);
|
|
194
232
|
root.removeEventListener('pointerleave', lift);
|
|
195
233
|
root.removeEventListener('pointercancel', out);
|
|
234
|
+
root.removeEventListener('click', pin);
|
|
196
235
|
doc.removeEventListener('pointerdown', away, true);
|
|
236
|
+
doc.removeEventListener('keydown', escape);
|
|
197
237
|
cross.remove();
|
|
198
238
|
tip.remove();
|
|
199
239
|
},
|