@adecore/charts 0.38.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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* A table of values as it is shown; hidden, it stays for a screen reader without any of these. */
|
|
2
|
+
const TABLE_CLASSES = {
|
|
3
|
+
box: 'max-h-80 w-full overflow-auto rounded-md border border-border-soft',
|
|
4
|
+
table: 'w-full border-collapse text-xs tabular-nums',
|
|
5
|
+
head: 'sticky top-0 bg-surface-hover',
|
|
6
|
+
column: 'h-8 px-2 text-right font-medium whitespace-nowrap text-text-muted',
|
|
7
|
+
row: 'border-t border-border-soft',
|
|
8
|
+
rowHeading: 'h-8 px-2 text-left font-normal whitespace-nowrap text-text-muted',
|
|
9
|
+
cell: 'h-8 px-2 text-right whitespace-nowrap text-text'
|
|
10
|
+
};
|
|
11
|
+
export const tableClasses = shown => shown ? TABLE_CLASSES : {
|
|
12
|
+
box: 'sr-only'
|
|
13
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Series charts, scatters, heatmaps, trace timelines and sparklines in the theme of @adecore/ui, readable by keyboard and screen reader.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"author": "Bas Milius",
|
|
@@ -40,24 +40,24 @@
|
|
|
40
40
|
"clsx": "^2.1.1",
|
|
41
41
|
"d3-scale": "^4.0.2",
|
|
42
42
|
"d3-shape": "^3.2.0",
|
|
43
|
-
"lucide-react": "^1.
|
|
43
|
+
"lucide-react": "^1.61.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@adecore/ui": "^0.
|
|
46
|
+
"@adecore/ui": "^0.40.0",
|
|
47
47
|
"i18next": "^26.4.2",
|
|
48
48
|
"react": "^19.3.0",
|
|
49
49
|
"react-dom": "^19.3.0",
|
|
50
50
|
"react-i18next": "^17.0.15"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@adecore/ui": "0.
|
|
54
|
-
"@types/bun": "^1.4.
|
|
53
|
+
"@adecore/ui": "0.40.0",
|
|
54
|
+
"@types/bun": "^1.4.4",
|
|
55
55
|
"@types/react": "^19.3.0",
|
|
56
56
|
"@types/react-dom": "^19.3.0",
|
|
57
|
-
"i18next": "^26.
|
|
58
|
-
"react": "^19.3.
|
|
59
|
-
"react-dom": "^19.3.
|
|
60
|
-
"react-i18next": "^17.
|
|
57
|
+
"i18next": "^26.5.11",
|
|
58
|
+
"react": "^19.3.1",
|
|
59
|
+
"react-dom": "^19.3.1",
|
|
60
|
+
"react-i18next": "^17.1.9",
|
|
61
61
|
"typescript": "^7.0.2"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/Heatmap.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import { formatNumber } from '@adecore/ui/format';
|
|
|
6
6
|
import { colorOf } from './color.ts';
|
|
7
7
|
import { FLOOR, heatMix, heatShare, moveCell, type HeatCell } from './heat.ts';
|
|
8
8
|
import type { ChartColor } from './types.ts';
|
|
9
|
+
import { tableClasses } from './values-table.ts';
|
|
9
10
|
|
|
10
11
|
export type HeatmapCell = HeatCell;
|
|
11
12
|
|
|
@@ -86,18 +87,26 @@ export function Heatmap({
|
|
|
86
87
|
const [shown, setShown] = useState(false);
|
|
87
88
|
|
|
88
89
|
const color = colorOf(tone)!;
|
|
90
|
+
|
|
89
91
|
const valueAt = (row: number, column: number): number | null => values[row]?.[column] ?? null;
|
|
92
|
+
|
|
90
93
|
const top = max ?? Math.max(0, ...values.flatMap((line) => line.filter((value): value is number => value !== null && Number.isFinite(value))));
|
|
91
94
|
const empty = rows.length === 0 || columns.length === 0;
|
|
95
|
+
|
|
92
96
|
const write = (value: number | null): string => (value === null ? t('noData') : format(value));
|
|
97
|
+
|
|
93
98
|
const describe = (cell: HeatCell): string => {
|
|
94
99
|
const value = valueAt(cell.row, cell.column);
|
|
100
|
+
|
|
95
101
|
return `${rows[cell.row]!.label}, ${columns[cell.column]!.label}, ${value === null ? t('noData') : `${valueLabel} ${format(value)}`}`;
|
|
96
102
|
};
|
|
103
|
+
|
|
97
104
|
const current = active !== null && active.row < rows.length && active.column < columns.length ? active : null;
|
|
105
|
+
const classes = tableClasses(table && shown);
|
|
98
106
|
|
|
99
107
|
const activate = (cell: HeatCell | null): void => {
|
|
100
108
|
setActive(cell);
|
|
109
|
+
|
|
101
110
|
if (!sameCell(cell, current)) {
|
|
102
111
|
onActiveChange?.(cell);
|
|
103
112
|
}
|
|
@@ -105,9 +114,11 @@ export function Heatmap({
|
|
|
105
114
|
|
|
106
115
|
const onKeyDown = (e: KeyboardEvent<HTMLDivElement>): void => {
|
|
107
116
|
const target = moveCell(e.key, current, rows.length, columns.length, e.ctrlKey || e.metaKey);
|
|
117
|
+
|
|
108
118
|
if (target === undefined) {
|
|
109
119
|
return;
|
|
110
120
|
}
|
|
121
|
+
|
|
111
122
|
e.preventDefault();
|
|
112
123
|
setAnnounce(target !== null);
|
|
113
124
|
activate(target);
|
|
@@ -150,6 +161,7 @@ export function Heatmap({
|
|
|
150
161
|
const value = valueAt(rowIndex, columnIndex);
|
|
151
162
|
const mix = heatMix(value, top, steps);
|
|
152
163
|
const isActive = current !== null && current.row === rowIndex && current.column === columnIndex;
|
|
164
|
+
|
|
153
165
|
return (
|
|
154
166
|
<span
|
|
155
167
|
key={column.key}
|
|
@@ -222,18 +234,14 @@ export function Heatmap({
|
|
|
222
234
|
{shown ? t('values.hide') : t('values.show')}
|
|
223
235
|
</Button>
|
|
224
236
|
)}
|
|
225
|
-
<div id={`${id}-table`} className={
|
|
226
|
-
<table className={table
|
|
237
|
+
<div id={`${id}-table`} className={classes.box}>
|
|
238
|
+
<table className={classes.table}>
|
|
227
239
|
<caption className="sr-only">{t('values.caption', { label })}</caption>
|
|
228
|
-
<thead className={
|
|
240
|
+
<thead className={classes.head}>
|
|
229
241
|
<tr>
|
|
230
242
|
<td />
|
|
231
243
|
{columns.map((column) => (
|
|
232
|
-
<th
|
|
233
|
-
key={column.key}
|
|
234
|
-
scope="col"
|
|
235
|
-
className={table && shown ? 'h-8 px-2 text-right font-medium whitespace-nowrap text-text-muted' : undefined}
|
|
236
|
-
>
|
|
244
|
+
<th key={column.key} scope="col" className={classes.column}>
|
|
237
245
|
{column.label}
|
|
238
246
|
</th>
|
|
239
247
|
))}
|
|
@@ -241,17 +249,15 @@ export function Heatmap({
|
|
|
241
249
|
</thead>
|
|
242
250
|
<tbody>
|
|
243
251
|
{rows.map((row, rowIndex) => (
|
|
244
|
-
<tr key={row.key} className={
|
|
245
|
-
<th
|
|
246
|
-
scope="row"
|
|
247
|
-
className={table && shown ? 'h-8 px-2 text-left font-normal whitespace-nowrap text-text-muted' : undefined}
|
|
248
|
-
>
|
|
252
|
+
<tr key={row.key} className={classes.row}>
|
|
253
|
+
<th scope="row" className={classes.rowHeading}>
|
|
249
254
|
{row.label}
|
|
250
255
|
</th>
|
|
251
256
|
{columns.map((column, columnIndex) => {
|
|
252
257
|
const value = valueAt(rowIndex, columnIndex);
|
|
258
|
+
|
|
253
259
|
return (
|
|
254
|
-
<td key={column.key} className={
|
|
260
|
+
<td key={column.key} className={classes.cell}>
|
|
255
261
|
{value === null ? '' : format(value)}
|
|
256
262
|
</td>
|
|
257
263
|
);
|
package/src/Root.tsx
CHANGED
|
@@ -20,7 +20,7 @@ import { LABEL_GAP as X_AXIS_GAP } from './axes.tsx';
|
|
|
20
20
|
import { bandLayout, GROUP_GAP } from './band.ts';
|
|
21
21
|
import { seriesColor } from './color.ts';
|
|
22
22
|
import { ChartContext, GroupContext, type ChartContextValue } from './context.ts';
|
|
23
|
-
import { containingIndex, moveIndex, nearestIndex, snapIndex } from './cursor.ts';
|
|
23
|
+
import { containingIndex, keyIndexOf, moveIndex, nearestIndex, snapIndex } from './cursor.ts';
|
|
24
24
|
import { useAlignedAxes } from './group-axes.ts';
|
|
25
25
|
import { movePoint, nearestPoint, type PlacedPoint } from './points.ts';
|
|
26
26
|
import { bucketStepOf, continuousXScale, extentOf, pointXScale, stackSeries, tickCount, yScaleOf } from './scale.ts';
|
|
@@ -179,10 +179,13 @@ export function ChartRoot<Row>({
|
|
|
179
179
|
|
|
180
180
|
const points = rows === 'points';
|
|
181
181
|
const band = xScale === 'band' && !points;
|
|
182
|
+
|
|
182
183
|
const xs = data.map((row, index) => {
|
|
183
184
|
const value = readValue(x, row, index);
|
|
185
|
+
|
|
184
186
|
return value === null || value === undefined ? Number.NaN : xOf(value);
|
|
185
187
|
});
|
|
188
|
+
|
|
186
189
|
const keys = points ? data.map((row, index) => (rowKey === undefined ? String(index) : String(readValue(rowKey, row, index)))) : null;
|
|
187
190
|
const rowLabels = rowLabel !== undefined ? data.map((row, index) => String(readValue(rowLabel, row, index))) : null;
|
|
188
191
|
const groupKeys = (band || points) && group !== undefined ? data.map((row, index) => String(readValue(group, row, index))) : null;
|
|
@@ -208,6 +211,7 @@ export function ChartRoot<Row>({
|
|
|
208
211
|
const right = width - marginRight - (axes.end ?? 0);
|
|
209
212
|
const plot = { left: marginLeft, top, right, bottom, width: Math.max(0, right - marginLeft), height: Math.max(0, bottom - top) };
|
|
210
213
|
const continuousKind = xScale === 'band' ? 'linear' : xScale;
|
|
214
|
+
|
|
211
215
|
const continuous = band
|
|
212
216
|
? null
|
|
213
217
|
: points
|
|
@@ -219,6 +223,7 @@ export function ChartRoot<Row>({
|
|
|
219
223
|
// A reference line stays on the plot, also where the data does not reach it.
|
|
220
224
|
const ruleValues = [...rules.values()].map((rule) => rule.value);
|
|
221
225
|
const yOptions = ruleValues.length === 0 ? y : { ...y, include: [...(y.include ?? []), ...ruleValues] };
|
|
226
|
+
|
|
222
227
|
const scaleY = yScaleOf(
|
|
223
228
|
yScale,
|
|
224
229
|
extentOf(stacked.values(), yScale, !points),
|
|
@@ -227,25 +232,38 @@ export function ChartRoot<Row>({
|
|
|
227
232
|
leftY?.ticks ?? rightY?.ticks ?? tickCount(plot.height, Y_TICK_SPACING),
|
|
228
233
|
!points
|
|
229
234
|
);
|
|
235
|
+
|
|
230
236
|
const yStep = scaleY.ticks.length > 1 ? scaleY.ticks[1]! - scaleY.ticks[0]! : 1;
|
|
237
|
+
|
|
231
238
|
// A value past `y.max` or `y.min` stops at the edge of the plot, which the svg does not clip.
|
|
232
239
|
const pixelOfY = (value: number): number | null => {
|
|
233
240
|
const pixel = scaleY.at(value);
|
|
241
|
+
|
|
234
242
|
return pixel === null ? null : Math.min(plot.bottom, Math.max(plot.top, pixel));
|
|
235
243
|
};
|
|
236
244
|
|
|
237
245
|
const colors = new Map<string, string>();
|
|
238
246
|
let next = 0;
|
|
247
|
+
|
|
239
248
|
for (const [seriesId, entry] of series) {
|
|
240
249
|
colors.set(seriesId, entry.color ?? seriesColor(next++));
|
|
241
250
|
}
|
|
251
|
+
|
|
242
252
|
const slots = new Map<string, { slot: number; count: number }>();
|
|
243
253
|
const members = new Map<string, string[]>();
|
|
254
|
+
|
|
244
255
|
for (const [seriesId, entry] of series) {
|
|
245
256
|
if (entry.kind === 'bar') {
|
|
246
|
-
|
|
257
|
+
const ids = members.get(entry.bars ?? '');
|
|
258
|
+
|
|
259
|
+
if (ids === undefined) {
|
|
260
|
+
members.set(entry.bars ?? '', [seriesId]);
|
|
261
|
+
} else {
|
|
262
|
+
ids.push(seriesId);
|
|
263
|
+
}
|
|
247
264
|
}
|
|
248
265
|
}
|
|
266
|
+
|
|
249
267
|
for (const ids of members.values()) {
|
|
250
268
|
const stackedBars = ids.some((seriesId) => series.get(seriesId)!.stack !== null);
|
|
251
269
|
ids.forEach((seriesId, index) => slots.set(seriesId, stackedBars ? { slot: 0, count: 1 } : { slot: index, count: ids.length }));
|
|
@@ -255,121 +273,165 @@ export function ChartRoot<Row>({
|
|
|
255
273
|
const placing = points ? [...series.values()].find((entry) => entry.kind === 'point' || entry.kind === 'line') : undefined;
|
|
256
274
|
const pointGroups: { key: string; indexes: number[] }[] = [];
|
|
257
275
|
const groupIndexes: number[] = [];
|
|
276
|
+
|
|
258
277
|
if (points) {
|
|
259
278
|
const found = new Map<string, number>();
|
|
279
|
+
|
|
260
280
|
data.forEach((_, index) => {
|
|
261
281
|
const key = groupKeys?.[index] ?? '';
|
|
262
282
|
let at = found.get(key);
|
|
283
|
+
|
|
263
284
|
if (at === undefined) {
|
|
264
285
|
at = pointGroups.length;
|
|
265
286
|
found.set(key, at);
|
|
266
287
|
pointGroups.push({ key, indexes: [] });
|
|
267
288
|
}
|
|
289
|
+
|
|
268
290
|
pointGroups[at]!.indexes.push(index);
|
|
269
291
|
groupIndexes.push(at);
|
|
270
292
|
});
|
|
271
293
|
}
|
|
294
|
+
|
|
272
295
|
const placed: PlacedPoint[] = [];
|
|
296
|
+
|
|
273
297
|
const positions: ([number, number] | null)[] = data.map((_, index) => {
|
|
274
298
|
const value = placing?.values[index];
|
|
275
299
|
const left = continuous === null || !points ? Number.NaN : continuous.at(xs[index] as number);
|
|
276
300
|
const pixel = value === null || value === undefined ? null : pixelOfY(value);
|
|
301
|
+
|
|
277
302
|
if (pixel === null || !Number.isFinite(left)) {
|
|
278
303
|
return null;
|
|
279
304
|
}
|
|
305
|
+
|
|
280
306
|
placed.push({ index, x: left, y: pixel });
|
|
307
|
+
|
|
281
308
|
return [left, pixel];
|
|
282
309
|
});
|
|
310
|
+
|
|
283
311
|
// A point and the line through it often read the same value, which a person hears once.
|
|
284
312
|
const readable = [...series].filter(([, entry], index, all) => !points || all.findIndex(([, other]) => other.label === entry.label) === index);
|
|
313
|
+
|
|
285
314
|
const groupColorOf = (index: number): string => seriesColor(groupIndexes[index] ?? 0);
|
|
315
|
+
|
|
286
316
|
const pointColor = (index: number): string => placing?.rowColors?.[index] ?? placing?.color ?? groupColorOf(index);
|
|
287
317
|
|
|
288
318
|
const centerOf = (index: number): number => {
|
|
289
319
|
if (layout !== null) {
|
|
290
320
|
return plot.left + (layout.lefts[index] ?? 0) + layout.bucket / 2;
|
|
291
321
|
}
|
|
322
|
+
|
|
292
323
|
return continuous!.at((xs[index] as number) + bucketStep / 2);
|
|
293
324
|
};
|
|
325
|
+
|
|
294
326
|
const bucketAt = (index: number): { left: number; width: number } | null => {
|
|
295
327
|
if (layout !== null) {
|
|
296
328
|
return { left: plot.left + (layout.lefts[index] ?? 0), width: layout.bucket };
|
|
297
329
|
}
|
|
330
|
+
|
|
298
331
|
if (bucketStep <= 0 || xs[index] === undefined) {
|
|
299
332
|
return null;
|
|
300
333
|
}
|
|
334
|
+
|
|
301
335
|
const left = continuous!.at(xs[index] as number);
|
|
336
|
+
|
|
302
337
|
return { left, width: continuous!.at((xs[index] as number) + bucketStep) - left };
|
|
303
338
|
};
|
|
339
|
+
|
|
304
340
|
const pixelOfX = (value: ChartX): number | null => {
|
|
305
341
|
if (layout !== null) {
|
|
306
|
-
const index = xs
|
|
342
|
+
const index = keyIndexOf(xs, value);
|
|
343
|
+
|
|
307
344
|
return index < 0 ? null : centerOf(index);
|
|
308
345
|
}
|
|
346
|
+
|
|
309
347
|
return typeof value === 'number' ? continuous!.at(value) : null;
|
|
310
348
|
};
|
|
311
349
|
|
|
312
350
|
const span = continuous === null ? 0 : continuous.domain[1] - continuous.domain[0];
|
|
313
351
|
// A row under another names something wider, such as the model under its effort, so a bucket reads from the bottom row up.
|
|
314
352
|
const rowFormats = xRows.flatMap((row) => (row.format === undefined ? [] : [row.format])).toReversed();
|
|
353
|
+
|
|
315
354
|
const formatX = (value: ChartX): string => {
|
|
316
355
|
if (rowFormats.length > 0) {
|
|
317
356
|
return rowFormats.map((format) => format(value)).join(', ');
|
|
318
357
|
}
|
|
358
|
+
|
|
319
359
|
if (xScale === 'time' && typeof value === 'number') {
|
|
320
360
|
return formatTime(value, timeLabelOf(span, bucketStep));
|
|
321
361
|
}
|
|
362
|
+
|
|
322
363
|
return typeof value === 'number' ? defaultValue(value) : value;
|
|
323
364
|
};
|
|
365
|
+
|
|
324
366
|
// Both axes share one scale, so the tooltip, the live region and the table write a value as the left one does.
|
|
325
367
|
const formatY = format ?? leftY?.format ?? rightY?.format ?? defaultValue;
|
|
368
|
+
|
|
326
369
|
/* The x of a point with the title of its axis, as a person reads it out: "Cost per task $0.12". */
|
|
327
370
|
const xValueOf = (index: number): string => {
|
|
328
371
|
const text = formatX(xs[index]!);
|
|
372
|
+
|
|
329
373
|
return points && formats.xTitle !== undefined ? `${formats.xTitle} ${text}` : text;
|
|
330
374
|
};
|
|
331
375
|
|
|
332
376
|
const notesAt = (index: number): ChartNote[] => {
|
|
333
377
|
const at = xs[index];
|
|
378
|
+
|
|
334
379
|
if (at === undefined) {
|
|
335
380
|
return [];
|
|
336
381
|
}
|
|
382
|
+
|
|
337
383
|
const notes: ChartNote[] = [];
|
|
384
|
+
|
|
338
385
|
for (const marker of markers.values()) {
|
|
339
386
|
// The points of a chart stand in no order, so a moment belongs to none of them.
|
|
340
387
|
const inside = band
|
|
341
388
|
? String(marker.at) === String(at)
|
|
342
389
|
: !points && typeof marker.at === 'number' && containingIndex(xs as number[], marker.at, bucketStep || undefined) === index;
|
|
390
|
+
|
|
343
391
|
if (inside) {
|
|
344
392
|
notes.push({ kind: 'marker', label: marker.label, detail: marker.detail, color: marker.color });
|
|
345
393
|
}
|
|
346
394
|
}
|
|
395
|
+
|
|
347
396
|
for (const range of bands.values()) {
|
|
348
397
|
let inside: boolean;
|
|
398
|
+
|
|
349
399
|
if (band) {
|
|
350
|
-
const from = xs
|
|
351
|
-
const to = xs
|
|
400
|
+
const from = keyIndexOf(xs, range.from);
|
|
401
|
+
const to = keyIndexOf(xs, range.to);
|
|
352
402
|
inside = from >= 0 && to >= 0 && index >= Math.min(from, to) && index <= Math.max(from, to);
|
|
353
403
|
} else if (bucketStep > 0) {
|
|
354
404
|
inside = (at as number) < (range.to as number) && (at as number) + bucketStep > (range.from as number);
|
|
355
405
|
} else {
|
|
356
406
|
inside = (at as number) >= (range.from as number) && (at as number) < (range.to as number);
|
|
357
407
|
}
|
|
408
|
+
|
|
358
409
|
if (inside) {
|
|
359
410
|
notes.push({ kind: 'band', label: range.label, detail: range.detail, color: range.color });
|
|
360
411
|
}
|
|
361
412
|
}
|
|
413
|
+
|
|
362
414
|
return notes;
|
|
363
415
|
};
|
|
364
416
|
|
|
365
417
|
const controlled = activeX !== undefined;
|
|
366
418
|
const cursorX = controlled ? activeX : sharedCursor !== null ? sharedCursor.x : ownX;
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
419
|
+
|
|
420
|
+
const indexAt = (value: ChartX | null): number => {
|
|
421
|
+
if (keys === null) {
|
|
422
|
+
return snapIndex(xs, xScale, value, bucketStep);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const index = value === null ? -1 : keys.indexOf(String(value));
|
|
426
|
+
|
|
427
|
+
return positions[index] == null ? -1 : index;
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const activeIndex = indexAt(cursorX);
|
|
431
|
+
const keyIndex = indexAt(keyX);
|
|
371
432
|
const from = keyIndex >= 0 ? keyIndex : activeIndex;
|
|
372
433
|
const ownsCursor = controlled || sharedCursor === null || sharedCursor.owner === id;
|
|
434
|
+
|
|
373
435
|
const setActiveX = (value: ChartX | null): void => {
|
|
374
436
|
if (!controlled) {
|
|
375
437
|
if (sharedCursor !== null) {
|
|
@@ -378,6 +440,7 @@ export function ChartRoot<Row>({
|
|
|
378
440
|
setOwnX(value);
|
|
379
441
|
}
|
|
380
442
|
}
|
|
443
|
+
|
|
381
444
|
onActiveXChange?.(value);
|
|
382
445
|
};
|
|
383
446
|
|
|
@@ -388,10 +451,12 @@ export function ChartRoot<Row>({
|
|
|
388
451
|
// A label under a column follows it: dimmed with it, and stronger while it is under the cursor or picked.
|
|
389
452
|
const anyPicked = selectedRows.some(Boolean);
|
|
390
453
|
const columnDim = [...series.values()].find((entry) => entry.kind === 'bar')?.dimOpacity ?? null;
|
|
454
|
+
|
|
391
455
|
const labelFocus = xs.map((_, index): 'strong' | 'dim' | null => {
|
|
392
456
|
if (index === activeIndex || selectedRows[index]) {
|
|
393
457
|
return 'strong';
|
|
394
458
|
}
|
|
459
|
+
|
|
395
460
|
return dimmedRows[index] || anyPicked ? 'dim' : null;
|
|
396
461
|
});
|
|
397
462
|
|
|
@@ -399,12 +464,15 @@ export function ChartRoot<Row>({
|
|
|
399
464
|
const parts = (
|
|
400
465
|
points ? [groupLabels?.[index], rowLabels?.[index], xValueOf(index)] : [groupLabels?.[index], rowLabels?.[index] ?? xValueOf(index)]
|
|
401
466
|
).filter((part): part is string => part !== undefined);
|
|
467
|
+
|
|
402
468
|
for (const [, entry] of readable) {
|
|
403
469
|
const value = entry.values[index];
|
|
470
|
+
|
|
404
471
|
if (value !== null && value !== undefined) {
|
|
405
472
|
parts.push(`${entry.label} ${formatY(value)}`);
|
|
406
473
|
}
|
|
407
474
|
}
|
|
475
|
+
|
|
408
476
|
return [...parts, ...notesAt(index).map((note) => (note.detail === null ? note.label : `${note.label}, ${note.detail}`))].join(', ');
|
|
409
477
|
};
|
|
410
478
|
|
|
@@ -415,9 +483,11 @@ export function ChartRoot<Row>({
|
|
|
415
483
|
const indexAtPointer = (e: MouseEvent<SVGSVGElement>): number => {
|
|
416
484
|
const box = e.currentTarget.getBoundingClientRect();
|
|
417
485
|
const pixel = e.clientX - box.left;
|
|
486
|
+
|
|
418
487
|
if (points) {
|
|
419
488
|
return nearestPoint(placed, pixel, e.clientY - box.top, POINT_REACH);
|
|
420
489
|
}
|
|
490
|
+
|
|
421
491
|
return nearestIndex(
|
|
422
492
|
xs.map((_, index) => centerOf(index)),
|
|
423
493
|
pixel
|
|
@@ -426,33 +496,43 @@ export function ChartRoot<Row>({
|
|
|
426
496
|
|
|
427
497
|
const onPointerMove = (e: PointerEvent<SVGSVGElement>): void => {
|
|
428
498
|
setAnnounce(false);
|
|
499
|
+
|
|
429
500
|
if (xs.length === 0) {
|
|
430
501
|
return;
|
|
431
502
|
}
|
|
503
|
+
|
|
432
504
|
if (keys !== null) {
|
|
433
505
|
const index = indexAtPointer(e);
|
|
506
|
+
|
|
434
507
|
if (index !== activeIndex) {
|
|
435
508
|
setActiveX(index < 0 ? null : keys[index]!);
|
|
436
509
|
}
|
|
510
|
+
|
|
437
511
|
return;
|
|
438
512
|
}
|
|
513
|
+
|
|
439
514
|
if (continuous !== null) {
|
|
440
515
|
const pixel = e.clientX - e.currentTarget.getBoundingClientRect().left;
|
|
441
516
|
// The end of the last bucket is the start of none, so the pointer stops a pixel short of it.
|
|
442
517
|
const index = snapIndex(xs, xScale, continuous.invert(Math.min(plot.right - 1, Math.max(plot.left, pixel))), bucketStep);
|
|
443
518
|
const value = index < 0 ? null : xs[index]!;
|
|
519
|
+
|
|
444
520
|
if (value !== cursorX) {
|
|
445
521
|
setActiveX(value);
|
|
446
522
|
}
|
|
523
|
+
|
|
447
524
|
return;
|
|
448
525
|
}
|
|
526
|
+
|
|
449
527
|
const index = indexAtPointer(e);
|
|
528
|
+
|
|
450
529
|
if (index >= 0 && String(xs[index]) !== String(cursorX)) {
|
|
451
530
|
setActiveX(xs[index]!);
|
|
452
531
|
}
|
|
453
532
|
};
|
|
454
533
|
|
|
455
534
|
const pressable = interactive && onActivate !== undefined && (layout !== null || points);
|
|
535
|
+
|
|
456
536
|
const activate = (index: number): void => {
|
|
457
537
|
if (pressable && index >= 0) {
|
|
458
538
|
onActivate(data[index]!, index);
|
|
@@ -463,9 +543,12 @@ export function ChartRoot<Row>({
|
|
|
463
543
|
if (pressable && from >= 0 && (e.key === 'Enter' || e.key === ' ')) {
|
|
464
544
|
e.preventDefault();
|
|
465
545
|
activate(from);
|
|
546
|
+
|
|
466
547
|
return;
|
|
467
548
|
}
|
|
549
|
+
|
|
468
550
|
const groupStarts = layout?.groups.map((run) => run.first) ?? [];
|
|
551
|
+
|
|
469
552
|
const target =
|
|
470
553
|
keys === null
|
|
471
554
|
? moveIndex(e.key, from, xs.length, groupStarts)
|
|
@@ -474,24 +557,32 @@ export function ChartRoot<Row>({
|
|
|
474
557
|
from,
|
|
475
558
|
e.key
|
|
476
559
|
);
|
|
560
|
+
|
|
477
561
|
if (target === undefined) {
|
|
478
562
|
return;
|
|
479
563
|
}
|
|
564
|
+
|
|
480
565
|
e.preventDefault();
|
|
566
|
+
|
|
481
567
|
if (target === null) {
|
|
482
568
|
// A dialog around the chart closes on an Escape that reaches the document, whether or not it was prevented.
|
|
483
569
|
e.stopPropagation();
|
|
484
570
|
setKeyX(null);
|
|
485
571
|
setActiveX(null);
|
|
572
|
+
|
|
486
573
|
return;
|
|
487
574
|
}
|
|
575
|
+
|
|
488
576
|
setAnnounce(true);
|
|
489
|
-
|
|
490
|
-
|
|
577
|
+
const value = keys === null ? xs[target]! : keys[target]!;
|
|
578
|
+
setKeyX(value);
|
|
579
|
+
setActiveX(value);
|
|
491
580
|
const box = scroller.current;
|
|
581
|
+
|
|
492
582
|
if (box !== null && box.scrollWidth > box.clientWidth) {
|
|
493
583
|
const center = centerOf(target);
|
|
494
584
|
const visible = box.clientWidth - overhang;
|
|
585
|
+
|
|
495
586
|
if (center < box.scrollLeft || center > box.scrollLeft + visible) {
|
|
496
587
|
box.scrollLeft = Math.max(0, center - visible / 2);
|
|
497
588
|
}
|
|
@@ -557,6 +648,7 @@ export function ChartRoot<Row>({
|
|
|
557
648
|
|
|
558
649
|
// A reference line holds for every bucket, so a screen reader hears it once with the chart instead of in every bucket.
|
|
559
650
|
const ruleText = rules.size === 0 ? null : `${[...rules.values()].map((rule) => `${rule.label} ${formatY(rule.value)}`).join(', ')}.`;
|
|
651
|
+
|
|
560
652
|
const hint = [
|
|
561
653
|
ruleText,
|
|
562
654
|
points ? t('keyboard.points') : t('keyboard.move'),
|
|
@@ -565,6 +657,7 @@ export function ChartRoot<Row>({
|
|
|
565
657
|
]
|
|
566
658
|
.filter(Boolean)
|
|
567
659
|
.join(' ');
|
|
660
|
+
|
|
568
661
|
const highlighted = layout === null ? [] : xs.flatMap((_, index) => ((cursor === 'full' && index === activeIndex) || selectedRows[index] ? [index] : []));
|
|
569
662
|
|
|
570
663
|
return (
|
|
@@ -608,6 +701,7 @@ export function ChartRoot<Row>({
|
|
|
608
701
|
onBlur={() => {
|
|
609
702
|
setAnnounce(false);
|
|
610
703
|
setKeyX(null);
|
|
704
|
+
|
|
611
705
|
// A bucket the keyboard left, or any point, keeps no preview open where the focus went on.
|
|
612
706
|
if (activeIndex >= 0 && (points || announce)) {
|
|
613
707
|
setActiveX(null);
|
|
@@ -711,12 +805,14 @@ export function ChartRoot<Row>({
|
|
|
711
805
|
function Crosshair({ context, dots }: { context: ChartContextValue; dots: boolean }) {
|
|
712
806
|
const { activeIndex, centerOf, plot, series, stacked, colors, pixelOfY } = context;
|
|
713
807
|
const left = Math.round(centerOf(activeIndex)) + 0.5;
|
|
808
|
+
|
|
714
809
|
return (
|
|
715
810
|
<g className="pointer-events-none">
|
|
716
811
|
<line x1={left} x2={left} y1={plot.top} y2={plot.bottom} className="stroke-border-strong" />
|
|
717
812
|
{[...series].map(([seriesId, entry]) => {
|
|
718
813
|
const top = stacked.get(seriesId)?.top[activeIndex];
|
|
719
814
|
const pixel = !dots || top === null || top === undefined || entry.kind === 'bar' ? null : pixelOfY(top);
|
|
815
|
+
|
|
720
816
|
return pixel === null ? null : (
|
|
721
817
|
<circle
|
|
722
818
|
key={seriesId}
|
|
@@ -738,19 +834,24 @@ const HALO = { strokeWidth: 4, strokeLinejoin: 'round', paintOrder: 'stroke' } a
|
|
|
738
834
|
/* Dashed guides from the point under the cursor and every picked point to both axes, with their values on the axes. */
|
|
739
835
|
function Guides({ context }: { context: ChartContextValue }) {
|
|
740
836
|
const { activeIndex, positions, selected, plot, xs, formatX, formatY, series, data } = context;
|
|
837
|
+
|
|
741
838
|
const shown = [...new Set([activeIndex, ...selected.flatMap((picked, index) => (picked ? [index] : []))])].filter(
|
|
742
839
|
(index) => index >= 0 && positions[index] != null
|
|
743
840
|
);
|
|
841
|
+
|
|
744
842
|
const placing = [...series.values()].find((entry) => entry.kind === 'point' || entry.kind === 'line');
|
|
843
|
+
|
|
745
844
|
if (shown.length === 0 || placing === undefined || data.length === 0) {
|
|
746
845
|
return null;
|
|
747
846
|
}
|
|
847
|
+
|
|
748
848
|
return (
|
|
749
849
|
<g className="pointer-events-none" aria-hidden>
|
|
750
850
|
{shown.map((index) => {
|
|
751
851
|
const [left, top] = positions[index]!;
|
|
752
852
|
const x = Math.round(left);
|
|
753
853
|
const y = Math.round(top);
|
|
854
|
+
|
|
754
855
|
return (
|
|
755
856
|
<g key={index}>
|
|
756
857
|
<path d={`M${plot.left} ${y + 0.5} H${x + 0.5} V${plot.bottom}`} fill="none" className="stroke-text-muted" strokeDasharray="4 4" />
|
package/src/SeriesLegend.tsx
CHANGED
|
@@ -79,17 +79,22 @@ export function SeriesLegend<Item extends SeriesLegendItem>({
|
|
|
79
79
|
const query = queryProp ?? ownQuery;
|
|
80
80
|
const off = new Set(hidden);
|
|
81
81
|
const shown = matchingItems(items, query);
|
|
82
|
+
|
|
82
83
|
const sections =
|
|
83
84
|
groups === undefined ? [{ key: '', label: null as string | null }] : groups.map((group) => ({ key: group.key, label: group.label as string | null }));
|
|
85
|
+
|
|
84
86
|
const searchLabel = search === undefined ? t('legend.search') : search;
|
|
85
87
|
|
|
86
88
|
const setQuery = (next: string): void => {
|
|
87
89
|
if (queryProp === undefined) {
|
|
88
90
|
setOwnQuery(next);
|
|
89
91
|
}
|
|
92
|
+
|
|
90
93
|
onQueryChange?.(next);
|
|
91
94
|
};
|
|
95
|
+
|
|
92
96
|
const toggle = (key: string): void => onHiddenChange(off.has(key) ? hidden.filter((entry) => entry !== key) : [...hidden, key]);
|
|
97
|
+
|
|
93
98
|
const highlight = (key: string | null): void => {
|
|
94
99
|
if (key !== highlighted) {
|
|
95
100
|
onHighlightedChange?.(key);
|
|
@@ -104,11 +109,14 @@ export function SeriesLegend<Item extends SeriesLegendItem>({
|
|
|
104
109
|
<div role="group" aria-label={label} className="flex min-h-0 flex-col gap-4">
|
|
105
110
|
{sections.map((section) => {
|
|
106
111
|
const own = shown.filter((item) => groups === undefined || item.group === section.key);
|
|
112
|
+
|
|
107
113
|
if (own.length === 0) {
|
|
108
114
|
return null;
|
|
109
115
|
}
|
|
116
|
+
|
|
110
117
|
const members = items.filter((item) => item.group === section.key && item.disabled !== true);
|
|
111
118
|
const allOn = members.every((item) => !off.has(item.key));
|
|
119
|
+
|
|
112
120
|
return (
|
|
113
121
|
<div key={section.key} className="flex flex-col">
|
|
114
122
|
{section.label !== null && (
|
|
@@ -126,6 +134,7 @@ export function SeriesLegend<Item extends SeriesLegendItem>({
|
|
|
126
134
|
const statusId = `${id}-${item.key}`;
|
|
127
135
|
const color = item.color ?? 'var(--text-muted)';
|
|
128
136
|
const name = item.name ?? item.label;
|
|
137
|
+
|
|
129
138
|
return (
|
|
130
139
|
<Tooltip key={item.key} label={item.status ?? name} side="left">
|
|
131
140
|
<button
|
package/src/Sparkline.tsx
CHANGED
|
@@ -33,6 +33,7 @@ export function Sparkline({ values, kind = 'line', label, tone = 'accent', forma
|
|
|
33
33
|
const high = Math.max(...present);
|
|
34
34
|
const low = Math.min(...present);
|
|
35
35
|
const step = values.length > 1 ? (width - 2) / (values.length - 1) : 0;
|
|
36
|
+
|
|
36
37
|
const path =
|
|
37
38
|
kind === 'line'
|
|
38
39
|
? line<number | null>()
|
|
@@ -40,6 +41,7 @@ export function Sparkline({ values, kind = 'line', label, tone = 'accent', forma
|
|
|
40
41
|
.x((_, index) => 1 + index * step)
|
|
41
42
|
.y((value) => 1 + (high === low ? height / 2 : ((high - value!) / (high - low)) * (height - 2)))(values)
|
|
42
43
|
: null;
|
|
44
|
+
|
|
43
45
|
return (
|
|
44
46
|
<Tooltip label={text}>
|
|
45
47
|
<svg
|