@adecore/charts 0.39.0 → 0.40.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/dist/Heatmap.js +161 -144
- package/dist/Root.js +71 -60
- package/dist/annotations.js +87 -145
- package/dist/cursor.d.ts +1 -0
- package/dist/cursor.js +5 -1
- package/dist/heat.js +2 -2
- package/dist/overlays.js +242 -233
- package/dist/points.js +0 -1
- package/dist/register.js +2 -3
- package/dist/series.js +147 -155
- package/dist/spark.js +1 -4
- package/dist/store.js +40 -70
- package/dist/timeline-layout.js +6 -1
- package/dist/values-table.d.ts +13 -0
- package/dist/values-table.js +13 -0
- package/package.json +9 -9
- package/src/Heatmap.tsx +20 -14
- package/src/Root.tsx +112 -11
- package/src/SeriesLegend.tsx +9 -0
- package/src/Sparkline.tsx +2 -0
- package/src/access.ts +3 -0
- package/src/annotations.tsx +14 -2
- package/src/axes.tsx +37 -2
- package/src/band.ts +25 -0
- package/src/color.ts +1 -0
- package/src/context.ts +2 -0
- package/src/cursor.ts +24 -1
- package/src/group-axes.ts +8 -0
- package/src/heat.ts +8 -2
- package/src/layer.tsx +3 -0
- package/src/legend.ts +2 -0
- package/src/overlays.tsx +43 -16
- package/src/points.ts +19 -2
- package/src/register.ts +3 -3
- package/src/scale.ts +60 -0
- package/src/scatter.tsx +9 -0
- package/src/series.tsx +51 -8
- package/src/spark.ts +4 -4
- package/src/store.ts +46 -70
- package/src/time-format.ts +1 -0
- package/src/timeline-layout.ts +27 -1
- package/src/timeline.tsx +28 -0
- package/src/use-chart.ts +1 -0
- package/src/values-table.ts +12 -0
package/src/access.ts
CHANGED
|
@@ -23,6 +23,7 @@ export function warnOnce(message: string): void {
|
|
|
23
23
|
if ((typeof process !== 'undefined' && process.env.NODE_ENV === 'production') || warned.has(message)) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
+
|
|
26
27
|
warned.add(message);
|
|
27
28
|
console.warn(message);
|
|
28
29
|
}
|
|
@@ -32,7 +33,9 @@ export function warnMissingKey(accessor: unknown, data: readonly unknown[], part
|
|
|
32
33
|
if (typeof accessor !== 'string' || data.length === 0) {
|
|
33
34
|
return;
|
|
34
35
|
}
|
|
36
|
+
|
|
35
37
|
const first = data[0];
|
|
38
|
+
|
|
36
39
|
if (typeof first === 'object' && first !== null && !(accessor in first)) {
|
|
37
40
|
warnOnce(`${part}: the rows have no key "${accessor}".`);
|
|
38
41
|
}
|
package/src/annotations.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import clsx from 'clsx';
|
|
|
4
4
|
import { xOf } from './access.ts';
|
|
5
5
|
import { colorOf } from './color.ts';
|
|
6
6
|
import { useChartContext } from './context.ts';
|
|
7
|
+
import { keyIndexOf } from './cursor.ts';
|
|
7
8
|
import { useRegistration } from './register.ts';
|
|
8
9
|
import type { ChartColor, ChartXInput } from './types.ts';
|
|
9
10
|
|
|
@@ -55,11 +56,14 @@ export function ChartRule({ value, label, tone = 'muted', dashed = false, classN
|
|
|
55
56
|
useRegistration(chart.store, 'rule', id, { value, label });
|
|
56
57
|
const pixel = chart.pixelOfY(value);
|
|
57
58
|
const target = chart.elements.rules;
|
|
59
|
+
|
|
58
60
|
if (pixel === null || target === null) {
|
|
59
61
|
return null;
|
|
60
62
|
}
|
|
63
|
+
|
|
61
64
|
const top = Math.round(pixel) + 0.5;
|
|
62
65
|
const below = top - RULE_LABEL < 0;
|
|
66
|
+
|
|
63
67
|
return createPortal(
|
|
64
68
|
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color: colorOf(tone)! }} aria-hidden>
|
|
65
69
|
<line x1={chart.plot.left} x2={chart.plot.right} y1={top} y2={top} stroke="currentColor" strokeDasharray={dashed ? '4 4' : undefined} />
|
|
@@ -88,11 +92,14 @@ export function ChartMarker({ at, label, detail, tone = 'muted', className, ref
|
|
|
88
92
|
const x = xOf(at);
|
|
89
93
|
useRegistration(chart.store, 'marker', id, { at: x, label, detail: detail ?? null, color });
|
|
90
94
|
const pixel = chart.pixelOfX(x);
|
|
95
|
+
|
|
91
96
|
// A moment outside the axis, such as a deploy before the first bucket, has no place on the plot.
|
|
92
97
|
if (pixel === null || pixel < chart.plot.left || pixel > chart.plot.right) {
|
|
93
98
|
return null;
|
|
94
99
|
}
|
|
100
|
+
|
|
95
101
|
const left = Math.round(pixel) + 0.5;
|
|
102
|
+
|
|
96
103
|
return (
|
|
97
104
|
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color }} aria-hidden>
|
|
98
105
|
<line x1={left} x2={left} y1={chart.plot.top} y2={chart.plot.bottom} stroke="currentColor" strokeDasharray="3 3" opacity={0.7} />
|
|
@@ -113,23 +120,28 @@ export function ChartBand({ from, to, label, detail, tone = 'muted', className,
|
|
|
113
120
|
const { plot, band, continuous } = chart;
|
|
114
121
|
let left: number | null = null;
|
|
115
122
|
let right: number | null = null;
|
|
123
|
+
|
|
116
124
|
if (continuous !== null && typeof start === 'number' && typeof end === 'number') {
|
|
117
125
|
left = Math.max(plot.left, continuous.at(Math.min(start, end)));
|
|
118
126
|
right = Math.min(plot.right, continuous.at(Math.max(start, end)));
|
|
119
127
|
} else if (band !== null) {
|
|
120
|
-
const first = chart.xs
|
|
121
|
-
const last = chart.xs
|
|
128
|
+
const first = keyIndexOf(chart.xs, start);
|
|
129
|
+
const last = keyIndexOf(chart.xs, end);
|
|
130
|
+
|
|
122
131
|
if (first >= 0 && last >= 0) {
|
|
123
132
|
left = plot.left + band.lefts[Math.min(first, last)]!;
|
|
124
133
|
right = plot.left + band.lefts[Math.max(first, last)]! + band.bucket;
|
|
125
134
|
}
|
|
126
135
|
}
|
|
136
|
+
|
|
127
137
|
if (left === null || right === null || right <= left) {
|
|
128
138
|
return null;
|
|
129
139
|
}
|
|
140
|
+
|
|
130
141
|
const x = Math.round(left);
|
|
131
142
|
// A short range still reads as a band, not as a line like a marker.
|
|
132
143
|
const width = Math.max(MIN_BAND, Math.round(right) - x);
|
|
144
|
+
|
|
133
145
|
return (
|
|
134
146
|
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color }} aria-hidden>
|
|
135
147
|
<defs>
|
package/src/axes.tsx
CHANGED
|
@@ -103,14 +103,17 @@ const crisp = (pixel: number): number => Math.round(pixel) + 0.5;
|
|
|
103
103
|
*/
|
|
104
104
|
const apart = <Label extends { readonly x: number; readonly end?: boolean }>(labels: readonly Label[], spacing: number): Label[] => {
|
|
105
105
|
const kept: Label[] = [];
|
|
106
|
+
|
|
106
107
|
for (const label of labels) {
|
|
107
108
|
const last = kept.at(-1);
|
|
109
|
+
|
|
108
110
|
if (last === undefined || label.x - last.x >= spacing) {
|
|
109
111
|
kept.push(label);
|
|
110
112
|
} else if (label.end === true && last.end !== true) {
|
|
111
113
|
kept[kept.length - 1] = label;
|
|
112
114
|
}
|
|
113
115
|
}
|
|
116
|
+
|
|
114
117
|
return kept;
|
|
115
118
|
};
|
|
116
119
|
|
|
@@ -119,23 +122,28 @@ export function ChartGrid({ axis = 'y', dashed = false, lines = 'all', className
|
|
|
119
122
|
const chart = useChartContext('Chart.Grid');
|
|
120
123
|
const { plot, y, continuous, band } = chart;
|
|
121
124
|
const ground = y.domain[0] <= 0 && y.domain[1] >= 0 ? 0 : y.domain[0];
|
|
125
|
+
|
|
122
126
|
if (lines === 'ground') {
|
|
123
127
|
const pixel = y.at(ground);
|
|
128
|
+
|
|
124
129
|
return pixel === null ? null : (
|
|
125
130
|
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
126
131
|
<line x1={plot.left} x2={plot.right} y1={crisp(pixel)} y2={crisp(pixel)} className={dashed ? 'stroke-border' : 'stroke-border-strong'} />
|
|
127
132
|
</g>
|
|
128
133
|
);
|
|
129
134
|
}
|
|
135
|
+
|
|
130
136
|
const columns =
|
|
131
137
|
continuous !== null
|
|
132
138
|
? continuous.ticks(tickCount(plot.width, X_TICK_SPACING)).map((tick) => continuous.at(tick))
|
|
133
139
|
: chart.xs.map((_, index) => chart.centerOf(index));
|
|
140
|
+
|
|
134
141
|
return (
|
|
135
142
|
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
136
143
|
{axis !== 'x' &&
|
|
137
144
|
y.ticks.map((tick) => {
|
|
138
145
|
const pixel = y.at(tick);
|
|
146
|
+
|
|
139
147
|
return pixel === null ? null : (
|
|
140
148
|
<line
|
|
141
149
|
key={tick}
|
|
@@ -190,31 +198,39 @@ export function ChartXAxis({
|
|
|
190
198
|
const rows = useSyncExternalStore(store.subscribe, store.xAxes, store.xAxes);
|
|
191
199
|
const widest = rows.get(id)?.widest ?? GUESS;
|
|
192
200
|
let above = 0;
|
|
201
|
+
|
|
193
202
|
for (const [rowId, row] of rows) {
|
|
194
203
|
if (rowId === id) {
|
|
195
204
|
break;
|
|
196
205
|
}
|
|
206
|
+
|
|
197
207
|
above += row.room;
|
|
198
208
|
}
|
|
209
|
+
|
|
199
210
|
const baseline = plot.bottom + LABEL_GAP + above;
|
|
200
211
|
const domainKey = continuous === null ? '' : `${continuous.domain.join(':')}:${Math.round(plot.width)}`;
|
|
201
212
|
const write = format ?? chart.formatX;
|
|
202
213
|
|
|
203
214
|
useBeforePaint(() => {
|
|
204
|
-
//
|
|
205
|
-
//
|
|
215
|
+
// Measuring only the placed labels can flip between two sets forever, since the widest decides how many are placed.
|
|
216
|
+
// A band scale measures every label; the ticks of a continuous one only let the width grow per domain.
|
|
206
217
|
let width = Math.ceil(widestIn(continuous === null ? candidates.current : labels.current));
|
|
218
|
+
|
|
207
219
|
if (continuous !== null) {
|
|
208
220
|
width = grown.current.key === domainKey ? Math.max(width, grown.current.width) : width;
|
|
209
221
|
grown.current = { key: domainKey, width };
|
|
210
222
|
}
|
|
223
|
+
|
|
211
224
|
store.setXAxis(id, { widest: width, room: roomOf(width, angle) + (title === undefined ? 0 : X_TITLE_ROOM), format });
|
|
212
225
|
});
|
|
226
|
+
|
|
213
227
|
useBeforePaint(() => () => store.setXAxis(id, null), [store, id]);
|
|
214
228
|
|
|
215
229
|
const inward = (x: number): number => Math.min(Math.max(x, widest / 2 - chart.leftAxis), chart.width - widest / 2);
|
|
230
|
+
|
|
216
231
|
let placed: { key: string; x: number; text: string; end?: boolean; index?: number }[];
|
|
217
232
|
const buckets = align === 'bucket' && chart.xScale === 'time' && !chart.points ? chart.bucketAt(0) : null;
|
|
233
|
+
|
|
218
234
|
if (continuous !== null && xs.length === 0) {
|
|
219
235
|
placed = [];
|
|
220
236
|
} else if (continuous !== null && buckets !== null && buckets.width > 0) {
|
|
@@ -222,6 +238,7 @@ export function ChartXAxis({
|
|
|
222
238
|
const span = continuous.domain[1] - continuous.domain[0];
|
|
223
239
|
const label = timeLabelOf(span, (buckets.width / Math.max(1, plot.width)) * span);
|
|
224
240
|
const every = thin ? Math.max(1, Math.ceil((widest + BAND_LABEL_GAP) / buckets.width)) : 1;
|
|
241
|
+
|
|
225
242
|
placed = xs.flatMap((key, index) =>
|
|
226
243
|
index % every !== 0
|
|
227
244
|
? []
|
|
@@ -232,10 +249,12 @@ export function ChartXAxis({
|
|
|
232
249
|
const step = values.length > 1 ? values[1]! - values[0]! : continuous.domain[1] - continuous.domain[0];
|
|
233
250
|
const label = timeLabelOf(continuous.domain[1] - continuous.domain[0], step);
|
|
234
251
|
const decimals = decimalsOf(step);
|
|
252
|
+
|
|
235
253
|
placed = values
|
|
236
254
|
.map((tick) => {
|
|
237
255
|
// A tick on an end of the axis, such as the last decade of a log scale, still names that end when moved in.
|
|
238
256
|
const end = tick === continuous.domain[0] || tick === continuous.domain[1];
|
|
257
|
+
|
|
239
258
|
return {
|
|
240
259
|
key: String(tick),
|
|
241
260
|
x: end ? inward(continuous.at(tick)) : continuous.at(tick),
|
|
@@ -245,23 +264,29 @@ export function ChartXAxis({
|
|
|
245
264
|
})
|
|
246
265
|
// Any other label that would hang past the end of the chart is left out rather than moved off its tick.
|
|
247
266
|
.filter((tick) => tick.x - widest / 2 >= -chart.leftAxis && tick.x + widest / 2 <= chart.width + 1);
|
|
267
|
+
|
|
248
268
|
placed = apart(placed, widest + LABEL_GAP);
|
|
249
269
|
} else if (band !== null && band.bucket > 0) {
|
|
250
270
|
const every = thin ? Math.max(1, Math.ceil((angle === 0 ? widest + BAND_LABEL_GAP : X_LABEL_LINE) / band.bucket)) : 1;
|
|
271
|
+
|
|
251
272
|
placed = xs.flatMap((key, index) => {
|
|
252
273
|
if (index % every !== 0) {
|
|
253
274
|
return [];
|
|
254
275
|
}
|
|
276
|
+
|
|
255
277
|
const center = chart.centerOf(index);
|
|
278
|
+
|
|
256
279
|
return [{ key: String(key), x: angle === 0 ? inward(center) : center, text: write(key), index }];
|
|
257
280
|
});
|
|
258
281
|
} else {
|
|
259
282
|
placed = [];
|
|
260
283
|
}
|
|
284
|
+
|
|
261
285
|
// Only an axis with a label per bucket leaves a bucket unnamed; round ticks of a continuous one name no bucket.
|
|
262
286
|
const named = (band !== null && band.bucket > 0) || buckets !== null;
|
|
263
287
|
const thinned = named ? placed.length < xs.length : null;
|
|
264
288
|
const reported = useRef<boolean | null>(null);
|
|
289
|
+
|
|
265
290
|
useEffect(() => {
|
|
266
291
|
if (thinned !== null && reported.current !== thinned) {
|
|
267
292
|
reported.current = thinned;
|
|
@@ -284,6 +309,7 @@ export function ChartXAxis({
|
|
|
284
309
|
{placed.map((tick) => {
|
|
285
310
|
const focus = tick.index === undefined ? null : (chart.labelFocus[tick.index] ?? null);
|
|
286
311
|
const opacity = focus === 'dim' ? chart.labelDim : undefined;
|
|
312
|
+
|
|
287
313
|
return angle === 0 ? (
|
|
288
314
|
<text
|
|
289
315
|
key={tick.key}
|
|
@@ -327,11 +353,13 @@ export function ChartXAxis({
|
|
|
327
353
|
/* Tells the root the title of the x axis, which a point chart reads out with each x; an axis without one says nothing. */
|
|
328
354
|
function useAxisTitle(title: string | undefined): void {
|
|
329
355
|
const { store } = useChartContext('Chart.XAxis');
|
|
356
|
+
|
|
330
357
|
useBeforePaint(() => {
|
|
331
358
|
if (title !== undefined) {
|
|
332
359
|
store.setFormat('xTitle', title);
|
|
333
360
|
}
|
|
334
361
|
});
|
|
362
|
+
|
|
335
363
|
useBeforePaint(
|
|
336
364
|
() => () => {
|
|
337
365
|
if (title !== undefined) {
|
|
@@ -357,17 +385,21 @@ export function ChartYAxis({ format, ticks, side = 'left', title, className, ref
|
|
|
357
385
|
format
|
|
358
386
|
});
|
|
359
387
|
});
|
|
388
|
+
|
|
360
389
|
useBeforePaint(() => () => store.setYAxis(side, null), [store, side]);
|
|
361
390
|
|
|
362
391
|
if (target === null) {
|
|
363
392
|
return null;
|
|
364
393
|
}
|
|
394
|
+
|
|
365
395
|
const decimals = decimalsOf(yStep);
|
|
396
|
+
|
|
366
397
|
return createPortal(
|
|
367
398
|
<g ref={ref} className={className}>
|
|
368
399
|
<g ref={labels}>
|
|
369
400
|
{y.ticks.map((tick) => {
|
|
370
401
|
const pixel = y.at(tick);
|
|
402
|
+
|
|
371
403
|
return pixel === null ? null : (
|
|
372
404
|
<text
|
|
373
405
|
key={tick}
|
|
@@ -402,11 +434,14 @@ export function ChartYAxis({ format, ticks, side = 'left', title, className, ref
|
|
|
402
434
|
export function ChartGroupAxis<Row>({ children, className, ref }: ChartGroupAxisProps<Row>) {
|
|
403
435
|
const chart = useChartContext('Chart.GroupAxis');
|
|
404
436
|
const target = chart.elements.groups;
|
|
437
|
+
|
|
405
438
|
if (target === null || chart.band === null) {
|
|
406
439
|
return null;
|
|
407
440
|
}
|
|
441
|
+
|
|
408
442
|
const rows = chart.data as Row[];
|
|
409
443
|
const band = chart.band;
|
|
444
|
+
|
|
410
445
|
return createPortal(
|
|
411
446
|
<div ref={ref} aria-hidden className={clsx('grid pt-1 text-xs', className)}>
|
|
412
447
|
{band.groups.map((run) => (
|
package/src/band.ts
CHANGED
|
@@ -30,14 +30,17 @@ export interface BandLayout {
|
|
|
30
30
|
/* The run of equal keys each bucket belongs to, in the order of the data; a key that comes back later starts a run of its own. */
|
|
31
31
|
export function groupRuns(keys: readonly string[]): { key: string; first: number; last: number }[] {
|
|
32
32
|
const runs: { key: string; first: number; last: number }[] = [];
|
|
33
|
+
|
|
33
34
|
keys.forEach((key, index) => {
|
|
34
35
|
const run = runs.at(-1);
|
|
36
|
+
|
|
35
37
|
if (run !== undefined && run.key === key) {
|
|
36
38
|
run.last = index;
|
|
37
39
|
} else {
|
|
38
40
|
runs.push({ key, first: index, last: index });
|
|
39
41
|
}
|
|
40
42
|
});
|
|
43
|
+
|
|
41
44
|
return runs;
|
|
42
45
|
}
|
|
43
46
|
|
|
@@ -48,25 +51,32 @@ export function groupRuns(keys: readonly string[]): { key: string; first: number
|
|
|
48
51
|
export function bandLayout(count: number, groupKeys: readonly string[] | null, room: number, minBucketWidth = 0, groupGap = GROUP_GAP): BandLayout {
|
|
49
52
|
const runs = groupKeys === null ? [] : groupRuns(groupKeys);
|
|
50
53
|
const gaps = runs.length * groupGap;
|
|
54
|
+
|
|
51
55
|
if (count === 0) {
|
|
52
56
|
return { bucket: 0, width: Math.max(0, room), lefts: [], groups: [], gap: 0 };
|
|
53
57
|
}
|
|
58
|
+
|
|
54
59
|
const bucket = Math.max(minBucketWidth, (room - gaps) / count);
|
|
55
60
|
const width = Math.max(room, bucket * count + gaps);
|
|
56
61
|
const lefts: number[] = [];
|
|
57
62
|
const groups: BandGroup[] = [];
|
|
63
|
+
|
|
58
64
|
if (runs.length === 0) {
|
|
59
65
|
for (let i = 0; i < count; i++) {
|
|
60
66
|
lefts.push(i * bucket);
|
|
61
67
|
}
|
|
68
|
+
|
|
62
69
|
return { bucket, width, lefts, groups, gap: 0 };
|
|
63
70
|
}
|
|
71
|
+
|
|
64
72
|
runs.forEach((run, groupIndex) => {
|
|
65
73
|
for (let i = run.first; i <= run.last; i++) {
|
|
66
74
|
lefts.push(i * bucket + (groupIndex + 0.5) * groupGap);
|
|
67
75
|
}
|
|
76
|
+
|
|
68
77
|
groups.push({ ...run, left: lefts[run.first]!, right: lefts[run.last]! + bucket });
|
|
69
78
|
});
|
|
79
|
+
|
|
70
80
|
return { bucket, width, lefts, groups, gap: groupGap };
|
|
71
81
|
}
|
|
72
82
|
|
|
@@ -84,11 +94,14 @@ export function columnsIn(left: number, bucket: number, count: number, widthOf?:
|
|
|
84
94
|
if (count <= 0) {
|
|
85
95
|
return [];
|
|
86
96
|
}
|
|
97
|
+
|
|
87
98
|
const room =
|
|
88
99
|
widthOf !== undefined ? Math.min(bucket, widthOf(bucket)) : Math.min(bucket - Math.max(1, bucket * (1 - COLUMN_SHARE)), MAX_COLUMN * count + count - 1);
|
|
100
|
+
|
|
89
101
|
const width = Math.max(2, Math.floor((room - (count - 1)) / count));
|
|
90
102
|
const total = width * count + count - 1;
|
|
91
103
|
const start = Math.round(left + (bucket - total) / 2);
|
|
104
|
+
|
|
92
105
|
return Array.from({ length: count }, (_, i) => ({ x: start + i * (width + 1), width }));
|
|
93
106
|
}
|
|
94
107
|
|
|
@@ -101,26 +114,32 @@ export function segmentOf(topPixel: number, basePixel: number, onGround: boolean
|
|
|
101
114
|
const upward = topPixel <= basePixel;
|
|
102
115
|
let top = Math.round(Math.min(topPixel, basePixel));
|
|
103
116
|
let bottom = Math.round(Math.max(topPixel, basePixel));
|
|
117
|
+
|
|
104
118
|
if (!onGround && upward) {
|
|
105
119
|
bottom -= 1;
|
|
106
120
|
} else if (!onGround) {
|
|
107
121
|
top += 1;
|
|
108
122
|
}
|
|
123
|
+
|
|
109
124
|
if (least > 0 && bottom - top < least) {
|
|
110
125
|
return upward ? { y: bottom - least, height: least } : { y: top, height: least };
|
|
111
126
|
}
|
|
127
|
+
|
|
112
128
|
if (value !== 0 && bottom - top < 1) {
|
|
113
129
|
return upward ? { y: bottom - 1, height: 1 } : { y: top, height: 1 };
|
|
114
130
|
}
|
|
131
|
+
|
|
115
132
|
return { y: top, height: Math.max(0, bottom - top) };
|
|
116
133
|
}
|
|
117
134
|
|
|
118
135
|
/* The outline of a column rounded only at its end away from the ground: the top for a value upward, else the bottom. */
|
|
119
136
|
export function endRoundedColumn(x: number, y: number, width: number, height: number, radius: number, upward: boolean): string {
|
|
120
137
|
const corner = Math.max(0, Math.min(radius, height, width / 2));
|
|
138
|
+
|
|
121
139
|
if (upward) {
|
|
122
140
|
return `M${x},${y + height}V${y + corner}A${corner},${corner} 0 0 1 ${x + corner},${y}H${x + width - corner}A${corner},${corner} 0 0 1 ${x + width},${y + corner}V${y + height}Z`;
|
|
123
141
|
}
|
|
142
|
+
|
|
124
143
|
return `M${x},${y}H${x + width}V${y + height - corner}A${corner},${corner} 0 0 1 ${x + width - corner},${y + height}H${x + corner}A${corner},${corner} 0 0 1 ${x},${y + height - corner}Z`;
|
|
125
144
|
}
|
|
126
145
|
|
|
@@ -130,6 +149,7 @@ export const LABEL_LIFT = 10;
|
|
|
130
149
|
const LABEL_SIZES = { '2xs': { row: 16, char: 8 }, xs: { row: 18, char: 9 } } as const;
|
|
131
150
|
|
|
132
151
|
export type ValueLabelSize = keyof typeof LABEL_SIZES;
|
|
152
|
+
|
|
133
153
|
const LABEL_AIR = 3;
|
|
134
154
|
|
|
135
155
|
/* The width a value label is taken to have before it is measured. */
|
|
@@ -142,9 +162,11 @@ export const guessLabelWidth = (text: string, size: ValueLabelSize = '2xs'): num
|
|
|
142
162
|
export function placeValueLabels(columns: readonly { center: number; top: number; width: number }[], size: ValueLabelSize = '2xs'): number[] {
|
|
143
163
|
const row = LABEL_SIZES[size].row;
|
|
144
164
|
const boxes: { left: number; right: number; top: number; bottom: number }[] = [];
|
|
165
|
+
|
|
145
166
|
return columns.map(({ center, top, width }) => {
|
|
146
167
|
const half = width / 2;
|
|
147
168
|
let baseline = top - LABEL_LIFT;
|
|
169
|
+
|
|
148
170
|
const blocking = () =>
|
|
149
171
|
boxes.find(
|
|
150
172
|
(box) =>
|
|
@@ -153,10 +175,13 @@ export function placeValueLabels(columns: readonly { center: number; top: number
|
|
|
153
175
|
box.top < baseline + LABEL_AIR &&
|
|
154
176
|
box.bottom > baseline - row
|
|
155
177
|
);
|
|
178
|
+
|
|
156
179
|
for (let box = blocking(); box !== undefined; box = blocking()) {
|
|
157
180
|
baseline = box.top - LABEL_AIR;
|
|
158
181
|
}
|
|
182
|
+
|
|
159
183
|
boxes.push({ left: center - half, right: center + half, top: baseline - row, bottom: baseline + LABEL_AIR });
|
|
184
|
+
|
|
160
185
|
return baseline;
|
|
161
186
|
});
|
|
162
187
|
}
|
package/src/color.ts
CHANGED
package/src/context.ts
CHANGED
|
@@ -99,9 +99,11 @@ export const ChartContext = createContext<ChartContextValue | null>(null);
|
|
|
99
99
|
|
|
100
100
|
export function useChartContext(part: string): ChartContextValue {
|
|
101
101
|
const context = useContext(ChartContext);
|
|
102
|
+
|
|
102
103
|
if (context === null) {
|
|
103
104
|
throw new Error(`${part} draws inside a Chart.Root.`);
|
|
104
105
|
}
|
|
106
|
+
|
|
105
107
|
return context;
|
|
106
108
|
}
|
|
107
109
|
|
package/src/cursor.ts
CHANGED
|
@@ -5,19 +5,24 @@ export function nearestIndex(xs: readonly number[], x: number): number {
|
|
|
5
5
|
if (xs.length === 0 || !Number.isFinite(x)) {
|
|
6
6
|
return -1;
|
|
7
7
|
}
|
|
8
|
+
|
|
8
9
|
let low = 0;
|
|
9
10
|
let high = xs.length - 1;
|
|
11
|
+
|
|
10
12
|
while (low < high) {
|
|
11
13
|
const middle = (low + high) >> 1;
|
|
14
|
+
|
|
12
15
|
if (xs[middle]! < x) {
|
|
13
16
|
low = middle + 1;
|
|
14
17
|
} else {
|
|
15
18
|
high = middle;
|
|
16
19
|
}
|
|
17
20
|
}
|
|
21
|
+
|
|
18
22
|
if (low > 0 && Math.abs(xs[low - 1]! - x) <= Math.abs(xs[low]! - x)) {
|
|
19
23
|
return low - 1;
|
|
20
24
|
}
|
|
25
|
+
|
|
21
26
|
return low;
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -30,21 +35,33 @@ export function containingIndex(xs: readonly number[], at: number, step?: number
|
|
|
30
35
|
if (xs.length === 0 || at < xs[0]!) {
|
|
31
36
|
return -1;
|
|
32
37
|
}
|
|
38
|
+
|
|
33
39
|
let index = nearestIndex(xs, at);
|
|
40
|
+
|
|
34
41
|
if (xs[index]! > at) {
|
|
35
42
|
index -= 1;
|
|
36
43
|
}
|
|
44
|
+
|
|
37
45
|
const last = xs.length - 1;
|
|
46
|
+
|
|
38
47
|
if (step === undefined && index < last) {
|
|
39
48
|
return index;
|
|
40
49
|
}
|
|
50
|
+
|
|
41
51
|
const length = step ?? (last > 0 ? xs[last]! - xs[last - 1]! : 0);
|
|
52
|
+
|
|
42
53
|
if (length <= 0) {
|
|
43
54
|
return at === xs[index] ? index : -1;
|
|
44
55
|
}
|
|
56
|
+
|
|
45
57
|
return at < xs[index]! + length ? index : -1;
|
|
46
58
|
}
|
|
47
59
|
|
|
60
|
+
/* The bucket of a band scale whose key reads as `x`; -1 for none. */
|
|
61
|
+
export function keyIndexOf(xs: readonly ChartX[], x: ChartX): number {
|
|
62
|
+
return xs.findIndex((key) => String(key) === String(x));
|
|
63
|
+
}
|
|
64
|
+
|
|
48
65
|
/*
|
|
49
66
|
* The bucket the cursor is on: the equal key on a band scale, the bucket the moment falls in on a time scale with
|
|
50
67
|
* buckets of `step`, and the nearest otherwise.
|
|
@@ -53,12 +70,15 @@ export function snapIndex(xs: readonly ChartX[], scale: ChartXScale, x: ChartX |
|
|
|
53
70
|
if (x === null) {
|
|
54
71
|
return -1;
|
|
55
72
|
}
|
|
73
|
+
|
|
56
74
|
if (scale === 'band') {
|
|
57
|
-
return xs
|
|
75
|
+
return keyIndexOf(xs, x);
|
|
58
76
|
}
|
|
77
|
+
|
|
59
78
|
if (typeof x !== 'number') {
|
|
60
79
|
return -1;
|
|
61
80
|
}
|
|
81
|
+
|
|
62
82
|
return scale === 'time' && step > 0 ? containingIndex(xs as number[], x, step) : nearestIndex(xs as number[], x);
|
|
63
83
|
}
|
|
64
84
|
|
|
@@ -71,6 +91,7 @@ export function moveIndex(key: string, current: number, count: number, groupStar
|
|
|
71
91
|
if (count === 0) {
|
|
72
92
|
return undefined;
|
|
73
93
|
}
|
|
94
|
+
|
|
74
95
|
switch (key) {
|
|
75
96
|
case 'ArrowRight':
|
|
76
97
|
return current < 0 ? 0 : Math.min(count - 1, current + 1);
|
|
@@ -87,8 +108,10 @@ export function moveIndex(key: string, current: number, count: number, groupStar
|
|
|
87
108
|
if (groupStarts.length === 0) {
|
|
88
109
|
return undefined;
|
|
89
110
|
}
|
|
111
|
+
|
|
90
112
|
const at = current < 0 ? -1 : groupStarts.findLastIndex((start) => start <= current);
|
|
91
113
|
const next = key === 'PageDown' ? Math.min(groupStarts.length - 1, at + 1) : Math.max(0, at - 1);
|
|
114
|
+
|
|
92
115
|
return groupStarts[next];
|
|
93
116
|
}
|
|
94
117
|
default:
|
package/src/group-axes.ts
CHANGED
|
@@ -15,20 +15,25 @@ export function createAxisWidths(): AxisWidths {
|
|
|
15
15
|
const widths = new Map<string, [number, number]>();
|
|
16
16
|
let left = 0;
|
|
17
17
|
let right = 0;
|
|
18
|
+
|
|
18
19
|
const settle = (): void => {
|
|
19
20
|
const nextLeft = Math.max(0, ...[...widths.values()].map(([width]) => width));
|
|
20
21
|
const nextRight = Math.max(0, ...[...widths.values()].map(([, width]) => width));
|
|
22
|
+
|
|
21
23
|
if (nextLeft !== left || nextRight !== right) {
|
|
22
24
|
left = nextLeft;
|
|
23
25
|
right = nextRight;
|
|
26
|
+
|
|
24
27
|
for (const listener of listeners) {
|
|
25
28
|
listener();
|
|
26
29
|
}
|
|
27
30
|
}
|
|
28
31
|
};
|
|
32
|
+
|
|
29
33
|
return {
|
|
30
34
|
subscribe(listener) {
|
|
31
35
|
listeners.add(listener);
|
|
36
|
+
|
|
32
37
|
return () => listeners.delete(listener);
|
|
33
38
|
},
|
|
34
39
|
set(id, nextLeft, nextRight) {
|
|
@@ -50,11 +55,14 @@ const ZERO = (): number => 0;
|
|
|
50
55
|
/* The y axes of a chart, widened to the widest of its group; outside a group they stay as they are. */
|
|
51
56
|
export function useAlignedAxes(widths: AxisWidths | null, left: number, right: number): [number, number] {
|
|
52
57
|
const id = useId();
|
|
58
|
+
|
|
53
59
|
useBeforePaint(() => {
|
|
54
60
|
widths?.set(id, left, right);
|
|
55
61
|
});
|
|
62
|
+
|
|
56
63
|
useBeforePaint(() => () => widths?.remove(id), [widths, id]);
|
|
57
64
|
const sharedLeft = useSyncExternalStore(widths?.subscribe ?? NONE, widths?.left ?? ZERO, widths?.left ?? ZERO);
|
|
58
65
|
const sharedRight = useSyncExternalStore(widths?.subscribe ?? NONE, widths?.right ?? ZERO, widths?.right ?? ZERO);
|
|
66
|
+
|
|
59
67
|
return [Math.max(left, sharedLeft), Math.max(right, sharedRight)];
|
|
60
68
|
}
|
package/src/heat.ts
CHANGED
|
@@ -11,25 +11,29 @@ export function heatStep(value: number | null, max: number, steps: number): numb
|
|
|
11
11
|
if (value === null || !Number.isFinite(value) || value <= 0 || max <= 0) {
|
|
12
12
|
return 0;
|
|
13
13
|
}
|
|
14
|
+
|
|
14
15
|
return Math.min(steps, Math.max(1, Math.ceil((value / max) * steps)));
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
/* The percentage of the color in a step
|
|
18
|
+
/* The percentage of the color in a step, up to 100 for the last; 0 for an empty cell. */
|
|
18
19
|
export function heatShare(step: number, steps: number): number {
|
|
19
20
|
if (step <= 0) {
|
|
20
21
|
return 0;
|
|
21
22
|
}
|
|
23
|
+
|
|
22
24
|
return steps <= 1 ? 100 : Math.round(FLOOR + ((step - 1) / (steps - 1)) * (100 - FLOOR));
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
/* The percentage of the color in a cell: in `steps` shades when given, else
|
|
27
|
+
/* The percentage of the color in a cell: in `steps` shades when given, else following the value; 0 for an empty cell. */
|
|
26
28
|
export function heatMix(value: number | null, max: number, steps?: number): number {
|
|
27
29
|
if (steps !== undefined) {
|
|
28
30
|
return heatShare(heatStep(value, max, steps), steps);
|
|
29
31
|
}
|
|
32
|
+
|
|
30
33
|
if (heatStep(value, max, 1) === 0) {
|
|
31
34
|
return 0;
|
|
32
35
|
}
|
|
36
|
+
|
|
33
37
|
return Math.round(FLOOR + Math.min(1, value! / max) * (100 - FLOOR));
|
|
34
38
|
}
|
|
35
39
|
|
|
@@ -41,7 +45,9 @@ export function moveCell(key: string, current: HeatCell | null, rows: number, co
|
|
|
41
45
|
if (rows === 0 || columns === 0) {
|
|
42
46
|
return undefined;
|
|
43
47
|
}
|
|
48
|
+
|
|
44
49
|
const at = current ?? { row: 0, column: 0 };
|
|
50
|
+
|
|
45
51
|
switch (key) {
|
|
46
52
|
case 'ArrowRight':
|
|
47
53
|
return current === null ? at : { row: at.row, column: Math.min(columns - 1, at.column + 1) };
|
package/src/layer.tsx
CHANGED
|
@@ -22,6 +22,7 @@ export interface ChartGroupProps {
|
|
|
22
22
|
/* A `<g>` in the plot for a drawing of the app's own, above the series. */
|
|
23
23
|
export function ChartLayer({ children, className, ref }: ChartLayerProps) {
|
|
24
24
|
useChartContext('Chart.Layer');
|
|
25
|
+
|
|
25
26
|
return (
|
|
26
27
|
<g ref={ref} className={className}>
|
|
27
28
|
{children}
|
|
@@ -33,7 +34,9 @@ export function ChartLayer({ children, className, ref }: ChartLayerProps) {
|
|
|
33
34
|
export function ChartGroup({ shareCursor = true, alignAxes = true, children, className, ref }: ChartGroupProps) {
|
|
34
35
|
const [cursor, setCursor] = useState<{ x: ChartX | null; owner: string | null }>({ x: null, owner: null });
|
|
35
36
|
const [widths] = useState(createAxisWidths);
|
|
37
|
+
|
|
36
38
|
const setX = (x: ChartX | null, owner: string): void => setCursor({ x, owner: x === null ? null : owner });
|
|
39
|
+
|
|
37
40
|
return (
|
|
38
41
|
<GroupContext value={{ cursor: shareCursor ? { ...cursor, setX } : null, widths: alignAxes ? widths : null }}>
|
|
39
42
|
<div ref={ref} className={className}>
|
package/src/legend.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { SeriesLegendItem } from './SeriesLegend.tsx';
|
|
|
3
3
|
/* The items whose label or full name holds the query, ignoring case and the space around it. */
|
|
4
4
|
export function matchingItems<Item extends SeriesLegendItem>(items: readonly Item[], query: string): Item[] {
|
|
5
5
|
const needle = query.trim().toLocaleLowerCase();
|
|
6
|
+
|
|
6
7
|
return needle === '' ? [...items] : items.filter((item) => [item.label, item.name ?? ''].some((text) => text.toLocaleLowerCase().includes(needle)));
|
|
7
8
|
}
|
|
8
9
|
|
|
@@ -10,5 +11,6 @@ export function matchingItems<Item extends SeriesLegendItem>(items: readonly Ite
|
|
|
10
11
|
export function toggleGroup(items: readonly SeriesLegendItem[], hidden: readonly string[], group: string, show: boolean): string[] {
|
|
11
12
|
const members = new Set(items.filter((item) => item.group === group && item.disabled !== true).map((item) => item.key));
|
|
12
13
|
const rest = hidden.filter((key) => !members.has(key));
|
|
14
|
+
|
|
13
15
|
return show ? rest : [...rest, ...members];
|
|
14
16
|
}
|