@adecore/charts 0.41.1 → 0.43.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/Root.d.ts +3 -1
- package/dist/Root.js +410 -339
- package/dist/annotations.js +210 -85
- package/dist/axes.d.ts +3 -2
- package/dist/axes.js +541 -74
- package/dist/band.d.ts +2 -0
- package/dist/band.js +10 -0
- package/dist/context.d.ts +3 -0
- package/dist/cursor.d.ts +1 -1
- package/dist/cursor.js +5 -4
- package/dist/locales/en.d.ts +1 -0
- package/dist/locales/en.js +1 -0
- package/dist/locales/nl.d.ts +1 -0
- package/dist/locales/nl.js +1 -0
- package/dist/overlays.js +204 -132
- package/dist/series.d.ts +4 -4
- package/dist/series.js +329 -195
- package/dist/store.d.ts +2 -0
- package/dist/store.js +2 -1
- package/package.json +3 -3
- package/src/Root.tsx +137 -44
- package/src/annotations.tsx +31 -4
- package/src/axes.tsx +182 -6
- package/src/band.ts +13 -0
- package/src/context.ts +9 -0
- package/src/cursor.ts +12 -4
- package/src/locales/en.ts +1 -0
- package/src/locales/nl.ts +1 -0
- package/src/overlays.tsx +51 -1
- package/src/series.tsx +144 -28
- package/src/store.ts +5 -1
package/dist/store.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface SeriesEntry {
|
|
|
12
12
|
readonly icon: IconProps['icon'] | null;
|
|
13
13
|
readonly valueLabels: boolean;
|
|
14
14
|
readonly dimOpacity?: number;
|
|
15
|
+
readonly endRoom?: number;
|
|
15
16
|
}
|
|
16
17
|
export interface RuleEntry {
|
|
17
18
|
readonly value: number;
|
|
@@ -32,6 +33,7 @@ export interface BandEntry {
|
|
|
32
33
|
}
|
|
33
34
|
export interface AxisSizes {
|
|
34
35
|
readonly end: number | null;
|
|
36
|
+
readonly start: number | null;
|
|
35
37
|
}
|
|
36
38
|
export type YAxisSide = 'left' | 'right';
|
|
37
39
|
export interface YAxisEntry {
|
package/dist/store.js
CHANGED
|
@@ -12,7 +12,8 @@ const sameEntry = (one, other) => one !== undefined && Object.entries(other).eve
|
|
|
12
12
|
export function createChartStore() {
|
|
13
13
|
const listeners = new Set();
|
|
14
14
|
let axes = {
|
|
15
|
-
end: null
|
|
15
|
+
end: null,
|
|
16
|
+
start: null
|
|
16
17
|
};
|
|
17
18
|
let formats = {};
|
|
18
19
|
const emit = () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.43.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",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"lucide-react": "^1.61.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@adecore/ui": "^0.
|
|
46
|
+
"@adecore/ui": "^0.43.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.
|
|
53
|
+
"@adecore/ui": "0.43.0",
|
|
54
54
|
"@types/bun": "^1.4.4",
|
|
55
55
|
"@types/react": "^19.3.0",
|
|
56
56
|
"@types/react-dom": "^19.3.0",
|
package/src/Root.tsx
CHANGED
|
@@ -15,7 +15,7 @@ import clsx from 'clsx';
|
|
|
15
15
|
import { useTranslation } from 'react-i18next';
|
|
16
16
|
import { useContentSize } from '@adecore/ui';
|
|
17
17
|
import { formatNumber, formatRounded } from '@adecore/ui/format';
|
|
18
|
-
import { readValue, warnMissingKey, xOf } from './access.ts';
|
|
18
|
+
import { readValue, warnMissingKey, warnOnce, xOf } from './access.ts';
|
|
19
19
|
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';
|
|
@@ -60,6 +60,13 @@ export interface ChartRootProps<Row> {
|
|
|
60
60
|
readonly y?: ChartYOptions;
|
|
61
61
|
/* The name of the chart, for a screen reader and the table of values. */
|
|
62
62
|
readonly label: string;
|
|
63
|
+
/*
|
|
64
|
+
* `horizontal` turns the buckets of a band scale into rows down the chart, a name on the left and a bar running to
|
|
65
|
+
* the right, for ranked categories with long names. The x is still the bucket and the y still the value.
|
|
66
|
+
*/
|
|
67
|
+
readonly orientation?: 'vertical' | 'horizontal';
|
|
68
|
+
/* The pixels a row of a horizontal chart takes, which the height follows in place of `height`. */
|
|
69
|
+
readonly rowHeight?: number;
|
|
63
70
|
/* In pixels, the x axis included; with `grow` the least height. The width follows the container. */
|
|
64
71
|
readonly height?: number;
|
|
65
72
|
/* Takes the height its container gives it, never less than `height`, such as a chart that fills a panel. */
|
|
@@ -110,6 +117,13 @@ const Y_TICK_SPACING = 40;
|
|
|
110
117
|
const POINT_REACH = 16;
|
|
111
118
|
/* One row of value labels over the tallest column; labels pushed up a row further need a larger `margin.top`. */
|
|
112
119
|
const VALUE_LABEL_ROOM = 24;
|
|
120
|
+
const ROW_HEIGHT = 32;
|
|
121
|
+
/* Ticks under a horizontal plot stand apart by the width of a label, which takes more room than its line beside a plot. */
|
|
122
|
+
const VALUE_TICK_SPACING = 80;
|
|
123
|
+
/* The line of the values under a horizontal plot, and the room a rule label takes above it. */
|
|
124
|
+
const VALUE_AXIS_LINE = 16;
|
|
125
|
+
const RULE_LABEL_ROOM = 20;
|
|
126
|
+
const NO_ENTRIES = new Map<string, never>();
|
|
113
127
|
|
|
114
128
|
const defaultValue = (value: number): string => (Number.isInteger(value) ? formatNumber(value) : formatRounded(value, 2));
|
|
115
129
|
|
|
@@ -127,6 +141,8 @@ export function ChartRoot<Row>({
|
|
|
127
141
|
yScale = 'linear',
|
|
128
142
|
y = {},
|
|
129
143
|
label,
|
|
144
|
+
orientation = 'vertical',
|
|
145
|
+
rowHeight = ROW_HEIGHT,
|
|
130
146
|
height: leastHeight = 200,
|
|
131
147
|
grow = false,
|
|
132
148
|
margin = {},
|
|
@@ -149,12 +165,12 @@ export function ChartRoot<Row>({
|
|
|
149
165
|
const { t } = useTranslation('charts');
|
|
150
166
|
const id = useId();
|
|
151
167
|
const [store] = useState(createChartStore);
|
|
152
|
-
const
|
|
168
|
+
const allSeries = useSyncExternalStore(store.subscribe, store.series, store.series);
|
|
153
169
|
const axes = useSyncExternalStore(store.subscribe, store.axes, store.axes);
|
|
154
170
|
const xAxes = useSyncExternalStore(store.subscribe, store.xAxes, store.xAxes);
|
|
155
171
|
const yAxes = useSyncExternalStore(store.subscribe, store.yAxes, store.yAxes);
|
|
156
|
-
const
|
|
157
|
-
const
|
|
172
|
+
const allMarkers = useSyncExternalStore(store.subscribe, store.markers, store.markers);
|
|
173
|
+
const allBands = useSyncExternalStore(store.subscribe, store.bands, store.bands);
|
|
158
174
|
const rules = useSyncExternalStore(store.subscribe, store.rules, store.rules);
|
|
159
175
|
const formats = useSyncExternalStore(store.subscribe, store.formats, store.formats);
|
|
160
176
|
const chartGroup = useContext(GroupContext);
|
|
@@ -169,16 +185,33 @@ export function ChartRoot<Row>({
|
|
|
169
185
|
const [leftSlot, setLeftSlot] = useState<SVGGElement | null>(null);
|
|
170
186
|
const [rightSlot, setRightSlot] = useState<SVGGElement | null>(null);
|
|
171
187
|
const [rulesSlot, setRulesSlot] = useState<SVGGElement | null>(null);
|
|
188
|
+
const [rowsSlot, setRowsSlot] = useState<HTMLElement | null>(null);
|
|
172
189
|
const [scroll, setScroll] = useState(0);
|
|
173
190
|
const [announce, setAnnounce] = useState(false);
|
|
174
191
|
const [measure, size] = useContentSize();
|
|
175
192
|
const [measureGroups, groupsSize] = useContentSize();
|
|
176
193
|
// The row of group labels under the plot takes its share of the height a growing chart is given.
|
|
177
|
-
const
|
|
194
|
+
const boxHeight = grow ? Math.max(leastHeight, size.height - groupsSize.height) : leastHeight;
|
|
178
195
|
const scroller = useRef<HTMLDivElement>(null);
|
|
179
196
|
|
|
180
197
|
const points = rows === 'points';
|
|
181
198
|
const band = xScale === 'band' && !points;
|
|
199
|
+
const horizontal = orientation === 'horizontal' && band;
|
|
200
|
+
// Only columns turn into rows; a line, a point, a marker or a band across rows would read nothing a bar does not.
|
|
201
|
+
const series = horizontal ? new Map([...allSeries].filter(([, entry]) => entry.kind === 'bar')) : allSeries;
|
|
202
|
+
const markers = horizontal ? NO_ENTRIES : allMarkers;
|
|
203
|
+
const bands = horizontal ? NO_ENTRIES : allBands;
|
|
204
|
+
const dropped = horizontal && (series.size < allSeries.size || allMarkers.size > 0 || allBands.size > 0);
|
|
205
|
+
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (orientation === 'horizontal' && !band) {
|
|
208
|
+
warnOnce('Chart.Root: `orientation="horizontal"` takes the buckets of a band scale; this chart stays vertical.');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (dropped) {
|
|
212
|
+
warnOnce('Chart.Root: a horizontal chart draws `Chart.Bar` only; its lines, areas, points, markers and bands are left out.');
|
|
213
|
+
}
|
|
214
|
+
}, [orientation, band, dropped]);
|
|
182
215
|
|
|
183
216
|
const xs = data.map((row, index) => {
|
|
184
217
|
const value = readValue(x, row, index);
|
|
@@ -192,24 +225,52 @@ export function ChartRoot<Row>({
|
|
|
192
225
|
const groupLabels = groupKeys === null ? null : groupLabel === undefined ? groupKeys : data.map((row, index) => String(readValue(groupLabel, row, index)));
|
|
193
226
|
const leftY = yAxes.get('left');
|
|
194
227
|
const rightY = yAxes.get('right');
|
|
195
|
-
|
|
228
|
+
// A horizontal chart has no y axis beside it: the names of its rows take that place, and its values run underneath.
|
|
229
|
+
const [leftAxis, rightAxis] = useAlignedAxes(
|
|
230
|
+
chartGroup?.widths ?? null,
|
|
231
|
+
horizontal ? (axes.start ?? 0) : (leftY?.width ?? 0),
|
|
232
|
+
horizontal ? 0 : (rightY?.width ?? 0)
|
|
233
|
+
);
|
|
234
|
+
|
|
196
235
|
const room = Math.max(0, size.width - leftAxis - rightAxis);
|
|
197
236
|
// A bucket of a time scale runs from its x for one step and its point stands in the middle, as a column would.
|
|
198
237
|
const bucketStep = xScale === 'time' && !points ? (stepProp ?? bucketStepOf(xs as number[])) : 0;
|
|
199
238
|
const inset = band || bucketStep > 0 ? 0 : INSET;
|
|
200
239
|
const marginLeft = margin.left ?? inset;
|
|
201
240
|
const marginRight = margin.right ?? inset;
|
|
202
|
-
|
|
203
|
-
const
|
|
241
|
+
|
|
242
|
+
const layout = horizontal
|
|
243
|
+
? bandLayout(xs.length, groupKeys, 0, rowHeight, groupGap)
|
|
244
|
+
: band
|
|
245
|
+
? bandLayout(xs.length, groupKeys, Math.max(0, room - marginLeft - marginRight), minBucketWidth, groupGap)
|
|
246
|
+
: null;
|
|
247
|
+
|
|
248
|
+
const width = layout === null || horizontal ? room : Math.max(room, Math.ceil(layout.width + marginLeft + marginRight));
|
|
204
249
|
const scrolls = layout !== null && width > room;
|
|
205
250
|
// A scrolling plot clips what hangs out of its first buckets, such as a value label or a slanted name, so the
|
|
206
251
|
// scroller reaches over the y axis by its width and gives that back as padding: the plot stays where it was.
|
|
207
252
|
const overhang = scrolls ? leftAxis : 0;
|
|
208
253
|
const xRows = [...xAxes.values()];
|
|
209
|
-
const
|
|
210
|
-
const
|
|
211
|
-
|
|
254
|
+
const valueRoom = horizontal ? Math.max(0, ...[...series.values()].map((entry) => entry.endRoom ?? 0)) : 0;
|
|
255
|
+
const titleRoom = leftY?.top ?? rightY?.top ?? 0;
|
|
256
|
+
let top: number;
|
|
257
|
+
let bottom: number;
|
|
258
|
+
let height: number;
|
|
259
|
+
|
|
260
|
+
if (horizontal) {
|
|
261
|
+
top = Math.max(margin.top ?? 8, titleRoom + (rules.size > 0 ? RULE_LABEL_ROOM : 0));
|
|
262
|
+
// Without rows the plot keeps the room of two, where it says there is no data.
|
|
263
|
+
bottom = top + (xs.length === 0 ? 2 * rowHeight : layout!.width);
|
|
264
|
+
height = bottom + (margin.bottom ?? 0) + (yAxes.size > 0 ? X_AXIS_GAP + VALUE_AXIS_LINE : 0);
|
|
265
|
+
} else {
|
|
266
|
+
height = boxHeight;
|
|
267
|
+
bottom = height - (xRows.length === 0 ? (margin.bottom ?? 0) : (margin.bottom ?? 4) + X_AXIS_GAP + xRows.reduce((sum, row) => sum + row.room, 0));
|
|
268
|
+
top = Math.max(margin.top ?? 8, leftY?.top ?? 0, rightY?.top ?? 0, [...series.values()].some((entry) => entry.valueLabels) ? VALUE_LABEL_ROOM : 0);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const right = width - marginRight - (axes.end ?? 0) - valueRoom;
|
|
212
272
|
const plot = { left: marginLeft, top, right, bottom, width: Math.max(0, right - marginLeft), height: Math.max(0, bottom - top) };
|
|
273
|
+
const valueRange: [number, number] = horizontal ? [plot.left, plot.right] : [plot.bottom, plot.top];
|
|
213
274
|
const continuousKind = xScale === 'band' ? 'linear' : xScale;
|
|
214
275
|
|
|
215
276
|
const continuous = band
|
|
@@ -228,8 +289,8 @@ export function ChartRoot<Row>({
|
|
|
228
289
|
yScale,
|
|
229
290
|
extentOf(stacked.values(), yScale, !points),
|
|
230
291
|
yOptions,
|
|
231
|
-
|
|
232
|
-
leftY?.ticks ?? rightY?.ticks ?? tickCount(plot.height, Y_TICK_SPACING),
|
|
292
|
+
valueRange,
|
|
293
|
+
leftY?.ticks ?? rightY?.ticks ?? (horizontal ? tickCount(plot.width, VALUE_TICK_SPACING) : tickCount(plot.height, Y_TICK_SPACING)),
|
|
233
294
|
!points
|
|
234
295
|
);
|
|
235
296
|
|
|
@@ -239,7 +300,7 @@ export function ChartRoot<Row>({
|
|
|
239
300
|
const pixelOfY = (value: number): number | null => {
|
|
240
301
|
const pixel = scaleY.at(value);
|
|
241
302
|
|
|
242
|
-
return pixel === null ? null : Math.min(
|
|
303
|
+
return pixel === null ? null : Math.min(Math.max(...valueRange), Math.max(Math.min(...valueRange), pixel));
|
|
243
304
|
};
|
|
244
305
|
|
|
245
306
|
const colors = new Map<string, string>();
|
|
@@ -315,9 +376,11 @@ export function ChartRoot<Row>({
|
|
|
315
376
|
|
|
316
377
|
const pointColor = (index: number): string => placing?.rowColors?.[index] ?? placing?.color ?? groupColorOf(index);
|
|
317
378
|
|
|
379
|
+
const bucketStart = horizontal ? plot.top : plot.left;
|
|
380
|
+
|
|
318
381
|
const centerOf = (index: number): number => {
|
|
319
382
|
if (layout !== null) {
|
|
320
|
-
return
|
|
383
|
+
return bucketStart + (layout.lefts[index] ?? 0) + layout.bucket / 2;
|
|
321
384
|
}
|
|
322
385
|
|
|
323
386
|
return continuous!.at((xs[index] as number) + bucketStep / 2);
|
|
@@ -325,7 +388,7 @@ export function ChartRoot<Row>({
|
|
|
325
388
|
|
|
326
389
|
const bucketAt = (index: number): { left: number; width: number } | null => {
|
|
327
390
|
if (layout !== null) {
|
|
328
|
-
return { left:
|
|
391
|
+
return { left: bucketStart + (layout.lefts[index] ?? 0), width: layout.bucket };
|
|
329
392
|
}
|
|
330
393
|
|
|
331
394
|
if (bucketStep <= 0 || xs[index] === undefined) {
|
|
@@ -480,9 +543,9 @@ export function ChartRoot<Row>({
|
|
|
480
543
|
warnMissingKey(x, data, 'Chart.Root');
|
|
481
544
|
}, [x, data]);
|
|
482
545
|
|
|
483
|
-
const indexAtPointer = (e: MouseEvent<
|
|
546
|
+
const indexAtPointer = (e: MouseEvent<Element>): number => {
|
|
484
547
|
const box = e.currentTarget.getBoundingClientRect();
|
|
485
|
-
const pixel = e.clientX - box.left;
|
|
548
|
+
const pixel = horizontal ? e.clientY - box.top : e.clientX - box.left;
|
|
486
549
|
|
|
487
550
|
if (points) {
|
|
488
551
|
return nearestPoint(placed, pixel, e.clientY - box.top, POINT_REACH);
|
|
@@ -494,7 +557,7 @@ export function ChartRoot<Row>({
|
|
|
494
557
|
);
|
|
495
558
|
};
|
|
496
559
|
|
|
497
|
-
const onPointerMove = (e: PointerEvent<
|
|
560
|
+
const onPointerMove = (e: PointerEvent<Element>): void => {
|
|
498
561
|
setAnnounce(false);
|
|
499
562
|
|
|
500
563
|
if (xs.length === 0) {
|
|
@@ -551,7 +614,7 @@ export function ChartRoot<Row>({
|
|
|
551
614
|
|
|
552
615
|
const target =
|
|
553
616
|
keys === null
|
|
554
|
-
? moveIndex(e.key, from, xs.length, groupStarts)
|
|
617
|
+
? moveIndex(e.key, from, xs.length, groupStarts, horizontal ? 'rows' : 'columns')
|
|
555
618
|
: movePoint(
|
|
556
619
|
pointGroups.map((run) => run.indexes.filter((index) => positions[index] !== null)),
|
|
557
620
|
from,
|
|
@@ -600,6 +663,8 @@ export function ChartRoot<Row>({
|
|
|
600
663
|
width,
|
|
601
664
|
band: layout,
|
|
602
665
|
continuous,
|
|
666
|
+
horizontal,
|
|
667
|
+
valueRoom,
|
|
603
668
|
y: scaleY,
|
|
604
669
|
yStep,
|
|
605
670
|
series,
|
|
@@ -616,7 +681,16 @@ export function ChartRoot<Row>({
|
|
|
616
681
|
dimmed: dimmedRows,
|
|
617
682
|
labelFocus,
|
|
618
683
|
labelDim: columnDim ?? 0.4,
|
|
619
|
-
elements: {
|
|
684
|
+
elements: {
|
|
685
|
+
legend: legendSlot,
|
|
686
|
+
table: tableSlot,
|
|
687
|
+
tooltip: tooltipSlot,
|
|
688
|
+
groups: groupsSlot,
|
|
689
|
+
left: leftSlot,
|
|
690
|
+
right: rightSlot,
|
|
691
|
+
rules: rulesSlot,
|
|
692
|
+
rows: rowsSlot
|
|
693
|
+
},
|
|
620
694
|
scroll,
|
|
621
695
|
box: size.width,
|
|
622
696
|
leftAxis,
|
|
@@ -651,7 +725,7 @@ export function ChartRoot<Row>({
|
|
|
651
725
|
|
|
652
726
|
const hint = [
|
|
653
727
|
ruleText,
|
|
654
|
-
points ? t('keyboard.points') : t('keyboard.move'),
|
|
728
|
+
points ? t('keyboard.points') : horizontal ? t('keyboard.rows') : t('keyboard.move'),
|
|
655
729
|
layout !== null && layout.groups.length > 0 ? t('keyboard.groups') : null,
|
|
656
730
|
pressable ? t('keyboard.activate') : null
|
|
657
731
|
]
|
|
@@ -660,15 +734,49 @@ export function ChartRoot<Row>({
|
|
|
660
734
|
|
|
661
735
|
const highlighted = layout === null ? [] : xs.flatMap((_, index) => ((cursor === 'full' && index === activeIndex) || selectedRows[index] ? [index] : []));
|
|
662
736
|
|
|
737
|
+
// A bucket across the plot, where the cursor lights it; pressable, it also reaches over what is written beside it.
|
|
738
|
+
const bucketBox = (index: number, pressing: boolean): { x: number; y: number; width: number; height: number } => {
|
|
739
|
+
const start = Math.round(bucketStart + (layout?.lefts[index] ?? 0));
|
|
740
|
+
const size = Math.max(1, Math.round(layout?.bucket ?? 0));
|
|
741
|
+
|
|
742
|
+
if (horizontal) {
|
|
743
|
+
return pressing
|
|
744
|
+
? { x: 0, y: start, width, height: size }
|
|
745
|
+
: { x: plot.left, y: start, width: Math.max(0, plot.right + valueRoom - plot.left), height: size };
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
return { x: start, y: plot.top, width: size, height: pressing ? height - plot.top : plot.height };
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
// The names of the rows stand beside the plot, and a person points at a name as much as at its bar.
|
|
752
|
+
const pointer = {
|
|
753
|
+
onPointerMove,
|
|
754
|
+
onPointerLeave: () => setActiveX(keyX),
|
|
755
|
+
onClick: pressable ? (e: MouseEvent<Element>) => activate(indexAtPointer(e)) : undefined
|
|
756
|
+
};
|
|
757
|
+
|
|
663
758
|
return (
|
|
664
759
|
<ChartContext value={context}>
|
|
665
760
|
<div ref={ref} className={clsx('flex min-w-0 flex-col gap-3', grow && 'h-full min-h-0', className)}>
|
|
666
761
|
<div ref={setLegendSlot} className="empty:hidden" />
|
|
667
762
|
{/* The ring stands outside the box, where it clears the labels at the edges of the axes. */}
|
|
668
|
-
<div
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
763
|
+
<div
|
|
764
|
+
ref={measure}
|
|
765
|
+
className={clsx(
|
|
766
|
+
'focus-ring-within relative flex rounded-md',
|
|
767
|
+
grow && !horizontal && 'min-h-0 flex-1',
|
|
768
|
+
horizontal && pressable && 'cursor-pointer'
|
|
769
|
+
)}
|
|
770
|
+
style={{ outlineOffset: 4 }}
|
|
771
|
+
{...(horizontal ? pointer : {})}
|
|
772
|
+
>
|
|
773
|
+
{horizontal ? (
|
|
774
|
+
<div ref={setRowsSlot} aria-hidden className="relative shrink-0" style={{ width: leftAxis, height }} />
|
|
775
|
+
) : (
|
|
776
|
+
<svg width={leftAxis} height={height} aria-hidden className="shrink-0 overflow-visible">
|
|
777
|
+
<g ref={setLeftSlot} tabIndex={-1} />
|
|
778
|
+
</svg>
|
|
779
|
+
)}
|
|
672
780
|
<div
|
|
673
781
|
ref={scroller}
|
|
674
782
|
className={clsx('min-w-0 flex-1', scrolls ? 'overflow-x-auto overflow-y-hidden overscroll-x-contain' : 'overflow-visible')}
|
|
@@ -694,9 +802,7 @@ export function ChartRoot<Row>({
|
|
|
694
802
|
aria-describedby={interactive ? `${id}-hint` : undefined}
|
|
695
803
|
aria-activedescendant={pressable && from >= 0 ? `${id}-${from}` : undefined}
|
|
696
804
|
className={clsx('block overflow-visible outline-none', pressable && 'cursor-pointer')}
|
|
697
|
-
|
|
698
|
-
onPointerLeave={() => setActiveX(keyX)}
|
|
699
|
-
onClick={pressable ? (e) => activate(indexAtPointer(e)) : undefined}
|
|
805
|
+
{...(horizontal ? {} : pointer)}
|
|
700
806
|
onKeyDown={interactive ? onKeyDown : undefined}
|
|
701
807
|
onBlur={() => {
|
|
702
808
|
setAnnounce(false);
|
|
@@ -708,17 +814,7 @@ export function ChartRoot<Row>({
|
|
|
708
814
|
}
|
|
709
815
|
}}
|
|
710
816
|
>
|
|
711
|
-
{layout !== null &&
|
|
712
|
-
highlighted.map((index) => (
|
|
713
|
-
<rect
|
|
714
|
-
key={index}
|
|
715
|
-
x={Math.round(plot.left + layout.lefts[index]!)}
|
|
716
|
-
y={plot.top}
|
|
717
|
-
width={Math.max(1, Math.round(layout.bucket))}
|
|
718
|
-
height={plot.height}
|
|
719
|
-
className="fill-surface-hover"
|
|
720
|
-
/>
|
|
721
|
-
))}
|
|
817
|
+
{layout !== null && highlighted.map((index) => <rect key={index} {...bucketBox(index, false)} className="fill-surface-hover" />)}
|
|
722
818
|
{children}
|
|
723
819
|
<g ref={setRulesSlot} tabIndex={-1} />
|
|
724
820
|
{continuous !== null && !points && activeIndex >= 0 && cursor !== 'none' && (
|
|
@@ -751,10 +847,7 @@ export function ChartRoot<Row>({
|
|
|
751
847
|
role="button"
|
|
752
848
|
aria-pressed={selectedRows[index]}
|
|
753
849
|
aria-label={describe(index)}
|
|
754
|
-
|
|
755
|
-
y={plot.top}
|
|
756
|
-
width={Math.max(1, Math.round(layout.bucket))}
|
|
757
|
-
height={height - plot.top}
|
|
850
|
+
{...bucketBox(index, true)}
|
|
758
851
|
fill="transparent"
|
|
759
852
|
/>
|
|
760
853
|
))}
|
package/src/annotations.tsx
CHANGED
|
@@ -34,7 +34,7 @@ export interface ChartBandProps {
|
|
|
34
34
|
export interface ChartRuleProps {
|
|
35
35
|
/* Where the line crosses the value axis. */
|
|
36
36
|
readonly value: number;
|
|
37
|
-
/* Written at the right end of the line with the value, such as "A · 71". */
|
|
37
|
+
/* Written at the right end of the line with the value, such as "A · 71"; above the plot in a horizontal chart. */
|
|
38
38
|
readonly label: string;
|
|
39
39
|
readonly tone?: ChartColor;
|
|
40
40
|
readonly dashed?: boolean;
|
|
@@ -49,7 +49,10 @@ const RULE_LABEL = 18;
|
|
|
49
49
|
/* Between the line and the label it carries, above it or below it. */
|
|
50
50
|
const RULE_GAP = 7;
|
|
51
51
|
|
|
52
|
-
/*
|
|
52
|
+
/*
|
|
53
|
+
* A line at a value across the plot, such as a reference to compare against, drawn above the series: horizontal over
|
|
54
|
+
* columns, vertical through the rows of a horizontal chart.
|
|
55
|
+
*/
|
|
53
56
|
export function ChartRule({ value, label, tone = 'muted', dashed = false, className, ref }: ChartRuleProps) {
|
|
54
57
|
const chart = useChartContext('Chart.Rule');
|
|
55
58
|
const id = useId();
|
|
@@ -61,6 +64,30 @@ export function ChartRule({ value, label, tone = 'muted', dashed = false, classN
|
|
|
61
64
|
return null;
|
|
62
65
|
}
|
|
63
66
|
|
|
67
|
+
if (chart.horizontal) {
|
|
68
|
+
const left = Math.round(pixel) + 0.5;
|
|
69
|
+
// Above the plot, on the side of the line that has the room, so it never covers the bars of the first row.
|
|
70
|
+
const toward = pixel > (chart.plot.left + chart.plot.right) / 2 ? 'end' : 'start';
|
|
71
|
+
|
|
72
|
+
return createPortal(
|
|
73
|
+
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color: colorOf(tone)! }} aria-hidden>
|
|
74
|
+
<line x1={left} x2={left} y1={chart.plot.top} y2={chart.plot.bottom} stroke="currentColor" strokeDasharray={dashed ? '4 4' : undefined} />
|
|
75
|
+
<text
|
|
76
|
+
x={Math.round(pixel) + (toward === 'end' ? 4 : -4)}
|
|
77
|
+
y={chart.plot.top - RULE_GAP}
|
|
78
|
+
textAnchor={toward}
|
|
79
|
+
strokeWidth={4}
|
|
80
|
+
paintOrder="stroke"
|
|
81
|
+
className="fill-text text-xs font-medium tabular-nums"
|
|
82
|
+
style={{ stroke: chart.halo }}
|
|
83
|
+
>
|
|
84
|
+
{`${label} · ${chart.formatY(value)}`}
|
|
85
|
+
</text>
|
|
86
|
+
</g>,
|
|
87
|
+
target
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
64
91
|
const top = Math.round(pixel) + 0.5;
|
|
65
92
|
const below = top - RULE_LABEL < 0;
|
|
66
93
|
|
|
@@ -94,7 +121,7 @@ export function ChartMarker({ at, label, detail, tone = 'muted', className, ref
|
|
|
94
121
|
const pixel = chart.pixelOfX(x);
|
|
95
122
|
|
|
96
123
|
// A moment outside the axis, such as a deploy before the first bucket, has no place on the plot.
|
|
97
|
-
if (pixel === null || pixel < chart.plot.left || pixel > chart.plot.right) {
|
|
124
|
+
if (chart.horizontal || pixel === null || pixel < chart.plot.left || pixel > chart.plot.right) {
|
|
98
125
|
return null;
|
|
99
126
|
}
|
|
100
127
|
|
|
@@ -134,7 +161,7 @@ export function ChartBand({ from, to, label, detail, tone = 'muted', className,
|
|
|
134
161
|
}
|
|
135
162
|
}
|
|
136
163
|
|
|
137
|
-
if (left === null || right === null || right <= left) {
|
|
164
|
+
if (chart.horizontal || left === null || right === null || right <= left) {
|
|
138
165
|
return null;
|
|
139
166
|
}
|
|
140
167
|
|