basalt-ui 2.22.0 → 2.24.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/CHANGELOG.md +68 -0
- package/MIGRATING.md +400 -12
- package/agent/agents/basalt-design-reviewer.md +3 -3
- package/agent/skills/basalt-design/SKILL.md +2 -2
- package/agent/skills/basalt-design/references/catalogue.md +22 -19
- package/agent/skills/basalt-design/references/states.md +39 -0
- package/dist/charts/BandStrip.d.ts.map +1 -1
- package/dist/charts/BandStrip.js +51 -5
- package/dist/charts/Bars.d.ts.map +1 -1
- package/dist/charts/Bars.js +19 -3
- package/dist/charts/CartesianChart.d.ts +2 -0
- package/dist/charts/CartesianChart.d.ts.map +1 -1
- package/dist/charts/CartesianChart.js +7 -3
- package/dist/charts/ChartCard.d.ts +1 -1
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +25 -15
- package/dist/charts/Sparkline.d.ts +8 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +95 -15
- package/dist/charts/StackedArea.d.ts.map +1 -1
- package/dist/charts/StackedArea.js +5 -1
- package/dist/charts/band-fold.d.ts +22 -4
- package/dist/charts/band-fold.d.ts.map +1 -1
- package/dist/charts/band-fold.js +10 -2
- package/dist/charts/chart-dates.d.ts +12 -0
- package/dist/charts/chart-dates.d.ts.map +1 -0
- package/dist/charts/chart-dates.js +16 -0
- package/dist/charts/chart-legend.module.css +16 -0
- package/dist/charts/compile.d.ts +10 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +77 -24
- package/dist/charts/date-tick-format.d.ts +5 -5
- package/dist/charts/date-tick-format.d.ts.map +1 -1
- package/dist/charts/date-tick-format.js +3 -7
- package/dist/charts/labels.d.ts +7 -3
- package/dist/charts/labels.d.ts.map +1 -1
- package/dist/charts/labels.js +28 -17
- package/dist/charts/legend.d.ts +0 -6
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +87 -35
- package/dist/charts/ref-label.d.ts +17 -0
- package/dist/charts/ref-label.d.ts.map +1 -1
- package/dist/charts/ref-label.js +13 -1
- package/dist/charts/sparkline.module.css +62 -12
- package/dist/charts/sparkline.module.css.d.ts +1 -0
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +4 -2
- package/dist/charts/types.d.ts +15 -3
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +9 -5
- package/dist/cli/shot/detect.js +8 -5
- package/dist/cli/shot/measure.js +19 -23
- package/dist/cli/shot/playwright.d.ts +8 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -1
- package/dist/cli/shot/playwright.js +5 -1
- package/dist/cli/shot/types.d.ts +2 -0
- package/dist/cli/shot/types.d.ts.map +1 -1
- package/dist/cli/tokens-css.d.ts +5 -7
- package/dist/cli/tokens-css.d.ts.map +1 -1
- package/dist/cli/tokens-css.js +11 -12
- package/dist/dashboard/bar-list.d.ts +6 -1
- package/dist/dashboard/bar-list.d.ts.map +1 -1
- package/dist/dashboard/bar-list.js +8 -0
- package/dist/dashboard/bar-list.module.css +18 -0
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +2 -2
- package/dist/dashboard/empty-state.d.ts.map +1 -1
- package/dist/dashboard/meter-model.d.ts +55 -21
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +98 -17
- package/dist/dashboard/meter-parts.d.ts +9 -2
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +99 -21
- package/dist/dashboard/meter.d.ts +21 -8
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +14 -3
- package/dist/dashboard/meter.module.css +57 -2
- package/dist/dashboard/meter.module.css.d.ts +5 -0
- package/dist/dashboard/query-state.d.ts +7 -3
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +15 -7
- package/dist/dashboard/section.d.ts +2 -1
- package/dist/dashboard/section.d.ts.map +1 -1
- package/dist/dashboard/section.module.css +12 -0
- package/dist/dashboard/settings-section.d.ts +1 -1
- package/dist/dashboard/settings-section.d.ts.map +1 -1
- package/dist/dashboard/settings-section.js +3 -3
- package/dist/dashboard/stat-card-parts.js +1 -1
- package/dist/dashboard/stat-card.module.css +43 -10
- package/dist/dashboard/stat-group.module.css +15 -0
- package/dist/dashboard/stat-strip.d.ts +16 -5
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +100 -74
- package/dist/dashboard/stat-strip.module.css +68 -62
- package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +19 -1
- package/dist/dashboard/widget-header.module.css +6 -0
- package/dist/guard/index.d.ts +2 -2
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +1 -17
- package/dist/styles.css +18 -11
- package/dist/table/data-table.js +1 -1
- package/dist/theme/controls.module.css +19 -0
- package/dist/theme/controls.module.css.d.ts +1 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +12 -7
- package/dist/theme/status-verdict.d.ts.map +1 -1
- package/dist/theme/status-verdict.js +9 -1
- package/dist/tokens/palette.d.ts +7 -10
- package/dist/tokens/palette.d.ts.map +1 -1
- package/dist/tokens/palette.js +7 -10
- package/llms.txt +7 -4
- package/package.json +1 -1
- package/src/charts/BandStrip.tsx +55 -5
- package/src/charts/Bars.tsx +31 -3
- package/src/charts/CartesianChart.tsx +9 -3
- package/src/charts/ChartCard.tsx +32 -19
- package/src/charts/Sparkline.tsx +139 -21
- package/src/charts/StackedArea.tsx +12 -1
- package/src/charts/band-fold.ts +25 -6
- package/src/charts/chart-dates.ts +30 -0
- package/src/charts/chart-legend.module.css +16 -0
- package/src/charts/compile.ts +148 -25
- package/src/charts/date-tick-format.ts +10 -13
- package/src/charts/labels.ts +43 -22
- package/src/charts/legend.tsx +70 -3
- package/src/charts/ref-label.ts +27 -0
- package/src/charts/sparkline.module.css +62 -12
- package/src/charts/sparkline.module.css.d.ts +1 -0
- package/src/charts/state.tsx +7 -4
- package/src/charts/types.ts +13 -3
- package/src/cli/shot/capture.ts +15 -3
- package/src/cli/shot/detect.ts +9 -9
- package/src/cli/shot/measure.ts +27 -27
- package/src/cli/shot/playwright.ts +18 -2
- package/src/cli/shot/types.ts +2 -0
- package/src/cli/tokens-css.ts +18 -20
- package/src/dashboard/bar-list.module.css +18 -0
- package/src/dashboard/bar-list.tsx +29 -0
- package/src/dashboard/delta-badge.tsx +6 -2
- package/src/dashboard/empty-state.tsx +4 -2
- package/src/dashboard/meter-model.ts +181 -47
- package/src/dashboard/meter-parts.tsx +187 -30
- package/src/dashboard/meter.module.css +57 -2
- package/src/dashboard/meter.module.css.d.ts +5 -0
- package/src/dashboard/meter.tsx +33 -10
- package/src/dashboard/query-state.tsx +20 -9
- package/src/dashboard/section.module.css +12 -0
- package/src/dashboard/section.tsx +2 -1
- package/src/dashboard/settings-section.tsx +4 -4
- package/src/dashboard/stat-card-parts.tsx +1 -1
- package/src/dashboard/stat-card.module.css +43 -10
- package/src/dashboard/stat-group.module.css +15 -0
- package/src/dashboard/stat-strip.module.css +68 -62
- package/src/dashboard/stat-strip.module.css.d.ts +1 -0
- package/src/dashboard/stat-strip.tsx +118 -59
- package/src/dashboard/widget-header.module.css +6 -0
- package/src/dashboard/widget-header.tsx +28 -1
- package/src/guard/index.ts +3 -19
- package/src/styles.css +18 -11
- package/src/table/data-table.tsx +1 -1
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +14 -8
- package/src/theme/controls.module.css +19 -0
- package/src/theme/controls.module.css.d.ts +1 -0
- package/src/theme/index.ts +12 -7
- package/src/theme/status-verdict.tsx +5 -1
- package/src/tokens/palette.ts +7 -10
package/src/charts/compile.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* binding, the tooltip content, and the container-class guide ladder.
|
|
6
6
|
* TanStack owns margins (measured), layout, focus and keyboard, the tooltip surface and SSR.
|
|
7
7
|
*/
|
|
8
|
+
import { headerDay, readoutClock } from './chart-dates'
|
|
8
9
|
import { dateTickFormat, utcAxisDate } from './date-tick-format'
|
|
9
10
|
import { crosshair } from '@tanstack/charts/crosshair'
|
|
10
11
|
import { decorative } from '@tanstack/charts/mark/decorative'
|
|
@@ -26,7 +27,7 @@ import type {
|
|
|
26
27
|
StoredChartSpec,
|
|
27
28
|
} from '@tanstack/charts/types/core'
|
|
28
29
|
import { compact } from '../format/number'
|
|
29
|
-
import { NON_FINITE,
|
|
30
|
+
import { NON_FINITE, numberFormat } from '../format/shared'
|
|
30
31
|
import { VX } from '../tokens'
|
|
31
32
|
import type { ContainerClass } from '../tokens/size-classes'
|
|
32
33
|
import { cursorBinding, followerOptions, quietWhileFollowing } from './cursor'
|
|
@@ -98,6 +99,8 @@ export type CartesianInput<T, X extends ChartX> = Pick<
|
|
|
98
99
|
tooltipBelow?: boolean
|
|
99
100
|
/** The vertical rule under the cursor. Default `true`; a bar kind has none (its hover is the bar). */
|
|
100
101
|
crosshair?: boolean
|
|
102
|
+
/** The engine's point marker on the focused datum. Default `true`; a bar kind has none (its hover is the bar). */
|
|
103
|
+
focusRing?: boolean
|
|
101
104
|
}
|
|
102
105
|
|
|
103
106
|
/** The one tick-label size, at every container width: the caption size, so a card never mixes sizes. */
|
|
@@ -113,7 +116,7 @@ const LADDER = {
|
|
|
113
116
|
|
|
114
117
|
/**
|
|
115
118
|
* A tick within this share of the x domain from its start counts as on the left edge: half a
|
|
116
|
-
*
|
|
119
|
+
* day label is ~15 px, 4–5% of a compact plot. Round ticks rarely land on the domain start
|
|
117
120
|
* exactly, so an equality test would miss the common case.
|
|
118
121
|
*/
|
|
119
122
|
const LEFT_EDGE_SHARE = 0.05
|
|
@@ -127,7 +130,7 @@ export function defaultNumberFormat(v: number): string {
|
|
|
127
130
|
* calendar date (east of UTC it is `22:00` UTC of the day before); any other key is its UTC date, so
|
|
128
131
|
* a key at 12:00 UTC or later never rolls to the next day.
|
|
129
132
|
*/
|
|
130
|
-
function bucketDay(d: Date): Date {
|
|
133
|
+
export function bucketDay(d: Date): Date {
|
|
131
134
|
const local = d.getHours() === 0 && d.getMinutes() === 0 && d.getSeconds() === 0
|
|
132
135
|
return new Date(
|
|
133
136
|
local
|
|
@@ -144,22 +147,9 @@ function bucketDay(d: Date): Date {
|
|
|
144
147
|
*/
|
|
145
148
|
export function utcHeaderDate(d: Date, dayOnly = false): string {
|
|
146
149
|
if (!Number.isFinite(d.getTime())) return NON_FINITE
|
|
147
|
-
const
|
|
148
|
-
const date = dateFormat(undefined, {
|
|
149
|
-
weekday: 'short',
|
|
150
|
-
day: 'numeric',
|
|
151
|
-
month: 'short',
|
|
152
|
-
year: 'numeric',
|
|
153
|
-
timeZone: 'UTC',
|
|
154
|
-
}).format(at)
|
|
150
|
+
const date = headerDay(dayOnly ? bucketDay(d) : d)
|
|
155
151
|
if (dayOnly || d.getTime() % DAY_MS === 0) return date
|
|
156
|
-
|
|
157
|
-
hour: '2-digit',
|
|
158
|
-
minute: '2-digit',
|
|
159
|
-
hourCycle: 'h23',
|
|
160
|
-
timeZone: 'UTC',
|
|
161
|
-
}).format(d)
|
|
162
|
-
return `${date} ${clock}`
|
|
152
|
+
return `${date} ${readoutClock(d)}`
|
|
163
153
|
}
|
|
164
154
|
|
|
165
155
|
const DAY_MS = 86_400_000
|
|
@@ -221,6 +211,7 @@ export function seriesRow(
|
|
|
221
211
|
|
|
222
212
|
/** Keys the compiler reserves for the legend entries it derives from `refLines`. */
|
|
223
213
|
const REF_KEY_PREFIX = '__ref-'
|
|
214
|
+
const ZONE_KEY_PREFIX = '__zone-'
|
|
224
215
|
|
|
225
216
|
/** The legend key of a reference line that names itself (`RefLine.legend`). */
|
|
226
217
|
const refLegendKey = (index: number): string => `${REF_KEY_PREFIX}${index}`
|
|
@@ -261,6 +252,25 @@ export function refLegendSeries<T>(refLines: readonly RefLine[] | undefined): Ch
|
|
|
261
252
|
)
|
|
262
253
|
}
|
|
263
254
|
|
|
255
|
+
/** Legend-only entries for the zones that name themselves (`ZoneSpec.legend`), in zone order. */
|
|
256
|
+
export function zoneLegendSeries<T>(zones: readonly ZoneSpec[] | undefined): ChartSeries<T>[] {
|
|
257
|
+
return (zones ?? []).flatMap((z, i) =>
|
|
258
|
+
z.legend === undefined
|
|
259
|
+
? []
|
|
260
|
+
: [
|
|
261
|
+
{
|
|
262
|
+
key: `${ZONE_KEY_PREFIX}${i}`,
|
|
263
|
+
label: z.legend.label,
|
|
264
|
+
color: z.legend.color ?? SWATCH_SOLID.get(z.fill) ?? z.fill,
|
|
265
|
+
mark: z.legend.mark ?? 'bar',
|
|
266
|
+
role: 'reference' as const,
|
|
267
|
+
tooltip: false,
|
|
268
|
+
getValue: () => null,
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
|
|
264
274
|
/** The visible series: not toggled off, and not a companion whose parent is. */
|
|
265
275
|
export function visibleSeries<T>(
|
|
266
276
|
series: readonly ChartSeries<T>[],
|
|
@@ -273,6 +283,63 @@ export function visibleSeries<T>(
|
|
|
273
283
|
|
|
274
284
|
type YAxis = { domain: readonly [number, number]; format: (v: number) => string }
|
|
275
285
|
|
|
286
|
+
/** The page locale's decimal separator (`.` or `,`). */
|
|
287
|
+
const decimalSeparator = (): string =>
|
|
288
|
+
numberFormat(undefined, {})
|
|
289
|
+
.formatToParts(1.1)
|
|
290
|
+
.find((p) => p.type === 'decimal')?.value ?? '.'
|
|
291
|
+
|
|
292
|
+
/** A trailing zero fraction behind a digit (`2.0`, `2,00`), the separator being a decimal one. */
|
|
293
|
+
const zeroFraction = (separator: string): RegExp =>
|
|
294
|
+
new RegExp(`(\\d)${separator.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}0+(?!\\d)`, 'g')
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The tick formatter. When every tick is a whole number (a whole step), a formatter that pads
|
|
298
|
+
* decimals (`toFixed(1)`) prints integers: `2 s`, not `2.0 s`. Only the page locale's decimal
|
|
299
|
+
* separator is read as one, so a thousands group (`1,000`, `1.000` in de) is never cut; a formatter
|
|
300
|
+
* that writes `.` under a `,` locale keeps its padding. The tooltip keeps the full format. The
|
|
301
|
+
* ticks are the engine's own (`count` goes through the same linear scale the engine builds).
|
|
302
|
+
*/
|
|
303
|
+
function wholeStepFormat<T>(
|
|
304
|
+
axis: YAxis,
|
|
305
|
+
cfg: AxisConfig<T> | undefined,
|
|
306
|
+
ticks: { values: readonly number[] } | { count: number },
|
|
307
|
+
): (v: number) => string {
|
|
308
|
+
const [lo, hi] = [Math.min(...axis.domain), Math.max(...axis.domain)]
|
|
309
|
+
const values =
|
|
310
|
+
'values' in ticks
|
|
311
|
+
? ticks.values
|
|
312
|
+
: cfg?.scale === 'log'
|
|
313
|
+
? []
|
|
314
|
+
: scaleLinear().domain([lo, hi]).ticks(ticks.count)
|
|
315
|
+
if (values.length === 0 || !values.every(Number.isInteger)) return axis.format
|
|
316
|
+
const padded = zeroFraction(decimalSeparator())
|
|
317
|
+
return (v) => axis.format(v).replace(padded, '$1')
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** A fixed domain's end this near the last round tick (as a share of the tick step) stays unlabelled. */
|
|
321
|
+
const END_TICK_MIN_GAP = 0.4
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The compact ladder's three ticks can stop short of a fixed domain's end (`[0, 25]` ticks `0 10 20`),
|
|
325
|
+
* and a unit the formatter states on the top tick (`25 days`) goes with it. A fixed domain whose end
|
|
326
|
+
* leaves at least {@link END_TICK_MIN_GAP} of a step above the last tick gets the end as a tick too.
|
|
327
|
+
*/
|
|
328
|
+
function withEndTick<T>(
|
|
329
|
+
cfg: AxisConfig<T> | undefined,
|
|
330
|
+
axis: YAxis,
|
|
331
|
+
count: number,
|
|
332
|
+
): { values: readonly number[] } | null {
|
|
333
|
+
if (!Array.isArray(cfg?.domain) || cfg.scale === 'log' || cfg.nice === true) return null
|
|
334
|
+
const [lo, hi] = [Math.min(...axis.domain), Math.max(...axis.domain)]
|
|
335
|
+
const round = scaleLinear().domain([lo, hi]).ticks(count)
|
|
336
|
+
const last = round.at(-1)
|
|
337
|
+
const next = round.at(1)
|
|
338
|
+
const first = round.at(0)
|
|
339
|
+
if (last === undefined || next === undefined || first === undefined || last >= hi) return null
|
|
340
|
+
return hi - last >= (next - first) * END_TICK_MIN_GAP ? { values: [...round, hi] } : null
|
|
341
|
+
}
|
|
342
|
+
|
|
276
343
|
export function yGuide<T>(opts: {
|
|
277
344
|
cfg: AxisConfig<T> | undefined
|
|
278
345
|
axis: YAxis
|
|
@@ -285,16 +352,19 @@ export function yGuide<T>(opts: {
|
|
|
285
352
|
const ticks =
|
|
286
353
|
typeof cfg?.ticks === 'object'
|
|
287
354
|
? { values: cfg.ticks }
|
|
288
|
-
:
|
|
355
|
+
: ((cls === 'compact' && cfg?.ticks === undefined
|
|
356
|
+
? withEndTick(cfg, axis, ladder.yTicks)
|
|
357
|
+
: null) ?? { count: typeof cfg?.ticks === 'number' ? cfg.ticks : ladder.yTicks })
|
|
289
358
|
const gridLine = grid ? { grid: GRID_LINE } : {}
|
|
290
359
|
if (cls === 'micro') return { ...gridLine, axis: false }
|
|
360
|
+
const tickFormat = wholeStepFormat(axis, cfg, ticks)
|
|
291
361
|
// Labels sit in a gutter OUTSIDE the plot, against its edge: a line never runs through one.
|
|
292
362
|
return {
|
|
293
363
|
...gridLine,
|
|
294
364
|
...(side === 'right' && { side: 'right' as const }),
|
|
295
365
|
axis: {
|
|
296
366
|
line: false,
|
|
297
|
-
ticks: { ...ticks, format:
|
|
367
|
+
ticks: { ...ticks, format: tickFormat, line: false },
|
|
298
368
|
tickLabels: { fontSize: TICK_FONT, opacity: 1 },
|
|
299
369
|
},
|
|
300
370
|
}
|
|
@@ -708,6 +778,8 @@ function underlayMarks<T, X extends ChartX>(
|
|
|
708
778
|
return [...zones, ...xZones]
|
|
709
779
|
}
|
|
710
780
|
|
|
781
|
+
/** A band's bar spans this share of its half step (`paddingInner` 0.2 leaves 0.8 of the step). */
|
|
782
|
+
const BAR_BODY = 0.8
|
|
711
783
|
const REF_LABEL_DX = 6
|
|
712
784
|
const REF_LABEL_DY_ABOVE = -5
|
|
713
785
|
const REF_LABEL_DY_BELOW = 11
|
|
@@ -831,7 +903,11 @@ function buildScales<T, X extends ChartX>(opts: {
|
|
|
831
903
|
}
|
|
832
904
|
}
|
|
833
905
|
|
|
834
|
-
/**
|
|
906
|
+
/**
|
|
907
|
+
* The tooltip title: the consumer's header, a long UTC date for a Date x, else the axis format. A
|
|
908
|
+
* day-granular series keeps its long date under a consumer `formatX` too: that formatter shortens
|
|
909
|
+
* the ticks (`2 Sep`), it does not rename the day.
|
|
910
|
+
*/
|
|
835
911
|
function tooltipHeader<T, X extends ChartX>(
|
|
836
912
|
input: CartesianInput<T, X>,
|
|
837
913
|
formatX: (v: ChartValue) => string,
|
|
@@ -840,7 +916,7 @@ function tooltipHeader<T, X extends ChartX>(
|
|
|
840
916
|
d: T,
|
|
841
917
|
): string {
|
|
842
918
|
if (input.tooltip && input.tooltip.formatHeader) return input.tooltip.formatHeader(v as X, d)
|
|
843
|
-
if (v instanceof Date && !input.formatX) return utcHeaderDate(v, dayOnly)
|
|
919
|
+
if (v instanceof Date && (!input.formatX || dayOnly)) return utcHeaderDate(v, dayOnly)
|
|
844
920
|
return formatX(v)
|
|
845
921
|
}
|
|
846
922
|
|
|
@@ -929,6 +1005,39 @@ const stripAnchor = (
|
|
|
929
1005
|
const STRIP_TOOLTIP_PLACEMENT = ['bottom', 'top'] as const
|
|
930
1006
|
const STRIP_TOOLTIP_OFFSET = 8
|
|
931
1007
|
|
|
1008
|
+
/** A tooltip's chrome (title row, padding) and each series row, px: the guess the bar anchor reserves. */
|
|
1009
|
+
const TIP_CHROME = 30
|
|
1010
|
+
const TIP_ROW = 18
|
|
1011
|
+
/** Air between a clamped tooltip's top and the plot's top edge, px. */
|
|
1012
|
+
const TIP_TOP_AIR = 4
|
|
1013
|
+
|
|
1014
|
+
/** A tall bar's tooltip steps aside by this much (a tooltip's half width plus a bar's), px. */
|
|
1015
|
+
const TIP_SIDE_STEP = 96
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* A bar's tooltip opens above the bar, and above a tall bar that is above the plot, over the card's
|
|
1019
|
+
* headline. For a bar too tall to fit it, the anchor drops to where the open-above tooltip's top meets
|
|
1020
|
+
* the plot's top edge (`rows` series rows tall) and steps to the side with more room, so the tooltip
|
|
1021
|
+
* sits beside the bar, in the plot, and not on the bar's top. A short bar is unaffected.
|
|
1022
|
+
*/
|
|
1023
|
+
const barAnchor =
|
|
1024
|
+
(rows: number) =>
|
|
1025
|
+
(
|
|
1026
|
+
points: readonly ChartPoint[],
|
|
1027
|
+
{ plot }: ChartTooltipAnchorContext,
|
|
1028
|
+
): { x: number; y: number } => {
|
|
1029
|
+
const xs = points.map((p) => p.x)
|
|
1030
|
+
const ys = points.map((p) => p.y)
|
|
1031
|
+
const centre = (Math.min(...xs) + Math.max(...xs)) / 2
|
|
1032
|
+
const x = Math.min(Math.max(centre, plot.x), plot.x + plot.width)
|
|
1033
|
+
const y = (Math.min(...ys) + Math.max(...ys)) / 2
|
|
1034
|
+
const floor = plot.y + TIP_TOP_AIR + TIP_CHROME + rows * TIP_ROW
|
|
1035
|
+
if (y >= floor) return { x, y }
|
|
1036
|
+
const toRight = x - plot.x < plot.width / 2
|
|
1037
|
+
const stepped = x + (toRight ? TIP_SIDE_STEP : -TIP_SIDE_STEP)
|
|
1038
|
+
return { x: Math.min(Math.max(stepped, plot.x), plot.x + plot.width), y: floor }
|
|
1039
|
+
}
|
|
1040
|
+
|
|
932
1041
|
function tooltipSpec<T, X extends ChartX>(opts: {
|
|
933
1042
|
input: CartesianInput<T, X>
|
|
934
1043
|
axes: Axes<T>
|
|
@@ -948,7 +1057,11 @@ function tooltipSpec<T, X extends ChartX>(opts: {
|
|
|
948
1057
|
use: quietWhileFollowing,
|
|
949
1058
|
...followerOptions(input.tooltip, input.cursorGroup, input.chartId),
|
|
950
1059
|
portal,
|
|
951
|
-
anchor: input.tooltipBelow
|
|
1060
|
+
anchor: input.tooltipBelow
|
|
1061
|
+
? stripAnchor
|
|
1062
|
+
: visible.some((s) => s.mark === 'bar')
|
|
1063
|
+
? barAnchor(rowSeries.length)
|
|
1064
|
+
: 'group-center',
|
|
952
1065
|
...(input.tooltipBelow && { placement: STRIP_TOOLTIP_PLACEMENT, offset: STRIP_TOOLTIP_OFFSET }),
|
|
953
1066
|
content: (points: readonly ChartPoint[]) => {
|
|
954
1067
|
const primary = points[0]
|
|
@@ -1019,14 +1132,23 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
1019
1132
|
)
|
|
1020
1133
|
const dayOnly = dayGranular(model.values)
|
|
1021
1134
|
const names = (input.refLines ?? []).some((r) => r.label !== undefined && r.label !== '')
|
|
1022
|
-
// What an unpinned reference caption avoids: each visible series as (x share, y) points.
|
|
1135
|
+
// What an unpinned reference caption avoids: each visible series as (x share, y) points. A bar is
|
|
1136
|
+
// a filled column from zero, so it walks round its body instead of through its top centre.
|
|
1137
|
+
const barHalf = model.halfStep * BAR_BODY
|
|
1023
1138
|
const linesOn = (side: 'left' | 'right' | undefined): RefLabelPoint[][] =>
|
|
1024
1139
|
visible
|
|
1025
1140
|
.filter((s) => shared.onRight(s) === (side === 'right' && shared.right !== null))
|
|
1026
1141
|
.map((s) =>
|
|
1027
1142
|
rows(s.key).flatMap((r) => {
|
|
1028
1143
|
const fx = model.fraction(r.x)
|
|
1029
|
-
|
|
1144
|
+
if (fx === null || r.y === null) return []
|
|
1145
|
+
if (!(model.band && s.mark === 'bar')) return [{ fx, y: r.y }]
|
|
1146
|
+
return [
|
|
1147
|
+
{ fx: fx - barHalf, y: 0 },
|
|
1148
|
+
{ fx: fx - barHalf, y: r.y },
|
|
1149
|
+
{ fx: fx + barHalf, y: r.y },
|
|
1150
|
+
{ fx: fx + barHalf, y: 0 },
|
|
1151
|
+
]
|
|
1030
1152
|
}),
|
|
1031
1153
|
)
|
|
1032
1154
|
|
|
@@ -1090,6 +1212,7 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
1090
1212
|
// Followers resolve the leader's group, so they draw its dots; `quietWhileFollowing` mutes the
|
|
1091
1213
|
// tooltip unless the chart opts into following tooltips.
|
|
1092
1214
|
focus: 'group-x',
|
|
1215
|
+
...(input.focusRing === false && { focusRing: false as const }),
|
|
1093
1216
|
maxFocusDistance: Number.POSITIVE_INFINITY,
|
|
1094
1217
|
cursor: cursorBinding({
|
|
1095
1218
|
group: input.cursorGroup,
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The default Date x tick labels: chosen from the TICK step (not the data step), read in UTC like
|
|
3
3
|
* the scale that placed them. A label may recur on one axis only behind a MARKER label (a day or a
|
|
4
|
-
* year) — `12:00, 18:00, 18
|
|
4
|
+
* year) — `12:00, 18:00, 18 Oct, 06:00, 12:00` is fine, `17 Oct, 17 Oct, 18 Oct` is not. Each span has
|
|
5
5
|
* a ladder of levels, plainest first; the first level that holds the invariant wins. The last rung
|
|
6
6
|
* of every ladder names the whole date (or instant) of its tick, so the invariant always holds.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
export const pad2 = (n: number): string => String(n).padStart(2, '0')
|
|
9
|
+
import { axisDay, axisDayYear, monthName } from './chart-dates'
|
|
11
10
|
|
|
12
|
-
export const
|
|
11
|
+
export const pad2 = (n: number): string => String(n).padStart(2, '0')
|
|
13
12
|
|
|
14
13
|
const DAY_MS = 86_400_000
|
|
15
14
|
const HOUR_MS = 3_600_000
|
|
@@ -19,18 +18,16 @@ const HOUR_MS = 3_600_000
|
|
|
19
18
|
*/
|
|
20
19
|
const MONTH_STEP_MS = 27 * DAY_MS
|
|
21
20
|
|
|
22
|
-
/** `
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
21
|
+
/** The day (`4 Oct`, in the page locale), read in UTC like the time scale (a local read shifts a
|
|
22
|
+
* day west of UTC). */
|
|
23
|
+
export const utcAxisDate = axisDay
|
|
26
24
|
|
|
27
25
|
const utcClock = (d: Date): string => `${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}`
|
|
28
26
|
const utcClockSeconds = (d: Date): string => `${utcClock(d)}:${pad2(d.getUTCSeconds())}`
|
|
29
27
|
const utcClockMillis = (d: Date): string =>
|
|
30
28
|
`${utcClockSeconds(d)}.${String(d.getUTCMilliseconds()).padStart(3, '0')}`
|
|
31
|
-
/** Two digits: a widened day label stays
|
|
32
|
-
const utcAxisDateYear =
|
|
33
|
-
`${utcAxisDate(d)}.${String(d.getUTCFullYear()).slice(2)}`
|
|
29
|
+
/** Two digits: a widened day label stays the same family (`4 Oct 26`). */
|
|
30
|
+
const utcAxisDateYear = axisDayYear
|
|
34
31
|
|
|
35
32
|
const isMidnight = (t: number): boolean => t % DAY_MS === 0
|
|
36
33
|
const isJanuary = (d: Date): boolean => d.getUTCMonth() === 0
|
|
@@ -69,7 +66,7 @@ const subDayLevels: Level[] = [
|
|
|
69
66
|
]
|
|
70
67
|
|
|
71
68
|
/**
|
|
72
|
-
* A day or more: `
|
|
69
|
+
* A day or more: the day (`18 Oct`). Ticks are at least a day apart here, so they fall on different UTC days
|
|
73
70
|
* and the last rung, which names the full date, cannot repeat.
|
|
74
71
|
*/
|
|
75
72
|
const dayLevels: Level[] = [
|
|
@@ -121,7 +118,7 @@ function stepOf(times: readonly number[]): number {
|
|
|
121
118
|
|
|
122
119
|
/**
|
|
123
120
|
* A formatter for one tick set. A Date that is not among the ticks (a hover, a consumer reading
|
|
124
|
-
* the axis format) reads as plain
|
|
121
|
+
* the axis format) reads as the plain day: its position in the set is undefined.
|
|
125
122
|
*/
|
|
126
123
|
export function dateTickFormat(ticks: readonly Date[]): (d: Date) => string {
|
|
127
124
|
const times = [...new Set(ticks.map((t) => t.getTime()))].toSorted((a, b) => a - b)
|
package/src/charts/labels.ts
CHANGED
|
@@ -19,10 +19,15 @@ const END_LABEL_DX = 6
|
|
|
19
19
|
* that did not need to flip sits above its point, one that needed to and did not is cut.
|
|
20
20
|
*/
|
|
21
21
|
const END_LABEL_GLYPH_EM = 0.85
|
|
22
|
-
/** A label flipped inside the plot sits this far above its point. */
|
|
22
|
+
/** A label flipped inside the plot sits this far above (or, below its point, `BELOW`) its point. */
|
|
23
23
|
const END_LABEL_FLIP_DY = -9
|
|
24
|
+
const END_LABEL_FLIP_DY_BELOW = 13
|
|
25
|
+
/** The room a label's centre keeps from the plot's top and bottom edge, px: half its line. */
|
|
26
|
+
const END_LABEL_EDGE = 7
|
|
24
27
|
|
|
25
28
|
export type EndLabelAnchor = 'start' | 'end'
|
|
29
|
+
/** Which side of its line a flipped label sits: away from the line's last segment. */
|
|
30
|
+
export type EndLabelSide = 'above' | 'below'
|
|
26
31
|
|
|
27
32
|
/**
|
|
28
33
|
* The side of its point an end label takes: `start` (right of the point) unless the point leaves
|
|
@@ -49,6 +54,8 @@ export type EndLabel<T, X extends ChartX> = {
|
|
|
49
54
|
series: ChartSeries<T>
|
|
50
55
|
row: PlotRow<T, X>
|
|
51
56
|
anchor: EndLabelAnchor
|
|
57
|
+
/** Only a flipped label (left of its point) can meet its own line; it takes the clear side. */
|
|
58
|
+
side: EndLabelSide
|
|
52
59
|
/** A vertical shift in px (negative is up) that keeps this label clear of its neighbours. */
|
|
53
60
|
nudge: number
|
|
54
61
|
}
|
|
@@ -58,24 +65,26 @@ type LastPoint<T, X extends ChartX> = {
|
|
|
58
65
|
row: PlotRow<T, X>
|
|
59
66
|
y: number
|
|
60
67
|
fraction: number
|
|
68
|
+
/** The value one finite point earlier: where the line's last segment comes from. */
|
|
69
|
+
prev: number | null
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
/**
|
|
64
73
|
* Pushes labels apart: `ys` are the labels' heights in px from the plot's bottom, returned as the
|
|
65
74
|
* shift each needs (positive is up) so no two sit closer than {@link END_LABEL_MIN_GAP} and the
|
|
66
|
-
* topmost
|
|
75
|
+
* lowest and the topmost stay {@link END_LABEL_EDGE} inside the plot. Order is kept.
|
|
67
76
|
*/
|
|
68
77
|
export function spreadLabels(ys: readonly number[], plotHeight: number): number[] {
|
|
69
78
|
const order = ys.map((y, i) => i).toSorted((a, b) => (ys[a] as number) - (ys[b] as number))
|
|
70
79
|
const placed = new Map<number, number>()
|
|
71
|
-
let floor =
|
|
80
|
+
let floor = END_LABEL_EDGE
|
|
72
81
|
for (const i of order) {
|
|
73
82
|
const at = Math.max(ys[i] as number, floor)
|
|
74
83
|
placed.set(i, at)
|
|
75
84
|
floor = at + END_LABEL_MIN_GAP
|
|
76
85
|
}
|
|
77
86
|
// Pushed past the top: slide the whole stack back down, then re-sweep from the top.
|
|
78
|
-
const top = plotHeight -
|
|
87
|
+
const top = plotHeight - END_LABEL_EDGE
|
|
79
88
|
let ceiling = Infinity
|
|
80
89
|
for (const i of order.toReversed()) {
|
|
81
90
|
const at = Math.min(placed.get(i) as number, ceiling, top)
|
|
@@ -101,40 +110,52 @@ export function endLabelPlan<T, X extends ChartX>(
|
|
|
101
110
|
if (naming === 'auto' && (plot.containerClass !== 'wide' || lines.length > MAX_END_LABELS))
|
|
102
111
|
return []
|
|
103
112
|
const last = lines.flatMap((series): LastPoint<T, X>[] => {
|
|
104
|
-
const
|
|
113
|
+
const finite = plot.rows(series.key).filter((r) => r.y !== null)
|
|
114
|
+
const row = finite.at(-1)
|
|
105
115
|
if (row === undefined || row.y === null) return []
|
|
106
116
|
const fraction = plot.xFraction(row.x)
|
|
107
117
|
if (fraction === null || fraction < 0 || fraction > 1) return []
|
|
108
|
-
return [{ series, row, y: row.y, fraction }]
|
|
118
|
+
return [{ series, row, y: row.y, fraction, prev: finite.at(-2)?.y ?? null }]
|
|
109
119
|
})
|
|
110
120
|
// No `y2` here (returned above), so every label reads the left domain.
|
|
111
121
|
const [lo, hi] = plot.yDomain
|
|
122
|
+
const placed = last.map(({ series, fraction, y, prev }) => {
|
|
123
|
+
const anchor = endLabelAnchor({
|
|
124
|
+
fraction,
|
|
125
|
+
plotWidth: plot.plotWidth,
|
|
126
|
+
label: series.label,
|
|
127
|
+
fontSize: VX.text.micro,
|
|
128
|
+
})
|
|
129
|
+
// A line falling into its end point comes from above: the label drops below it.
|
|
130
|
+
const side = prev !== null && prev > y ? ('below' as const) : ('above' as const)
|
|
131
|
+
// The label's centre, px from the plot's bottom, with its flip: what the spread keeps apart.
|
|
132
|
+
const centre = ((y - lo) / (hi - lo || 1)) * plot.plotHeight - endLabelOffset(anchor, side).dy
|
|
133
|
+
return { anchor, side, centre }
|
|
134
|
+
})
|
|
112
135
|
const shifts = spreadLabels(
|
|
113
|
-
|
|
136
|
+
placed.map((p) => p.centre),
|
|
114
137
|
plot.plotHeight,
|
|
115
138
|
)
|
|
116
|
-
return last.map(({ series, row
|
|
139
|
+
return last.map(({ series, row }, i) => ({
|
|
117
140
|
series,
|
|
118
141
|
row,
|
|
119
|
-
anchor:
|
|
120
|
-
|
|
121
|
-
plotWidth: plot.plotWidth,
|
|
122
|
-
label: series.label,
|
|
123
|
-
fontSize: VX.text.micro,
|
|
124
|
-
}),
|
|
142
|
+
anchor: (placed[i] as (typeof placed)[number]).anchor,
|
|
143
|
+
side: (placed[i] as (typeof placed)[number]).side,
|
|
125
144
|
nudge: 0 - (shifts[i] as number),
|
|
126
145
|
}))
|
|
127
146
|
}
|
|
128
147
|
|
|
129
|
-
/** The offset of a label from its point: right of it, level; or left of it
|
|
130
|
-
export function endLabelOffset(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
148
|
+
/** The offset of a label from its point: right of it, level; or left of it, above or below. */
|
|
149
|
+
export function endLabelOffset(
|
|
150
|
+
anchor: EndLabelAnchor,
|
|
151
|
+
side: EndLabelSide = 'above',
|
|
152
|
+
): { dx: number; dy: number } {
|
|
153
|
+
if (anchor === 'start') return { dx: END_LABEL_DX, dy: 0 }
|
|
154
|
+
return { dx: -END_LABEL_DX, dy: side === 'above' ? END_LABEL_FLIP_DY : END_LABEL_FLIP_DY_BELOW }
|
|
134
155
|
}
|
|
135
156
|
|
|
136
157
|
export function endLabelMarks<T, X extends ChartX>(plan: readonly EndLabel<T, X>[]): ChartMarks {
|
|
137
|
-
return plan.map(({ series, row, anchor, nudge }) =>
|
|
158
|
+
return plan.map(({ series, row, anchor, side, nudge }) =>
|
|
138
159
|
decorative(
|
|
139
160
|
text([row], {
|
|
140
161
|
id: `${series.key}-end-label`,
|
|
@@ -144,8 +165,8 @@ export function endLabelMarks<T, X extends ChartX>(plan: readonly EndLabel<T, X>
|
|
|
144
165
|
fill: series.color,
|
|
145
166
|
fontSize: VX.text.micro,
|
|
146
167
|
anchor,
|
|
147
|
-
dx: endLabelOffset(anchor).dx,
|
|
148
|
-
dy: endLabelOffset(anchor).dy + nudge,
|
|
168
|
+
dx: endLabelOffset(anchor, side).dx,
|
|
169
|
+
dy: endLabelOffset(anchor, side).dy + nudge,
|
|
149
170
|
}),
|
|
150
171
|
),
|
|
151
172
|
)
|
package/src/charts/legend.tsx
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* its parent's entry. Toggling an entry hides the series from the plot, the tooltip and the auto
|
|
5
5
|
* domain together; hovering one emphasizes it (the definition is rebuilt with `highlighted`).
|
|
6
6
|
*/
|
|
7
|
+
import { useEffect, useLayoutEffect, useRef } from 'react'
|
|
7
8
|
import type { CSSProperties, ReactElement } from 'react'
|
|
8
9
|
import { VX } from '../tokens'
|
|
9
10
|
import classes from './chart-legend.module.css'
|
|
@@ -28,13 +29,17 @@ const ROLE_ORDER = ['series', 'overlay', 'reference'] as const
|
|
|
28
29
|
*/
|
|
29
30
|
const DOTS_GAP = 30
|
|
30
31
|
|
|
32
|
+
const byReferenceFirst = <T,>(a: ChartSeries<T>, b: ChartSeries<T>): number =>
|
|
33
|
+
Number(b.role === 'reference') - Number(a.role === 'reference')
|
|
34
|
+
|
|
31
35
|
/** The entries a series list derives, in display order (by role when `groups`). */
|
|
32
36
|
export function legendEntries<T>(
|
|
33
37
|
series: readonly ChartSeries<T>[],
|
|
34
38
|
groups: boolean,
|
|
35
39
|
): ChartSeries<T>[][] {
|
|
36
40
|
const entries = series.filter((s) => s.legend !== false)
|
|
37
|
-
|
|
41
|
+
// Zones and reference lines, which hue alone cannot name, lead: the compact row folds from its end.
|
|
42
|
+
if (!groups) return entries.length ? [entries.toSorted(byReferenceFirst)] : []
|
|
38
43
|
return ROLE_ORDER.map((role) => entries.filter((s) => (s.role ?? 'series') === role)).filter(
|
|
39
44
|
(g) => g.length > 0,
|
|
40
45
|
)
|
|
@@ -57,8 +62,10 @@ const rowBaseStyle: CSSProperties = {
|
|
|
57
62
|
const rowStyle: CSSProperties = { ...rowBaseStyle, display: 'flex' }
|
|
58
63
|
/** The gaps `chart-legend.module.css` reads, as custom properties. */
|
|
59
64
|
type LegendVars = CSSProperties & Record<'--bc-legend-gap' | '--bc-dots-gap', string>
|
|
65
|
+
// `flex-wrap` is the stylesheet's too: one folded row below 480px, wrapping rows from it.
|
|
66
|
+
const { flexWrap: _wrap, ...autoBaseStyle } = rowBaseStyle
|
|
60
67
|
const autoRowStyle: LegendVars = {
|
|
61
|
-
...
|
|
68
|
+
...autoBaseStyle,
|
|
62
69
|
'--bc-legend-gap': `${VX.legendGap}px`,
|
|
63
70
|
'--bc-dots-gap': `${DOTS_GAP}px`,
|
|
64
71
|
}
|
|
@@ -187,12 +194,69 @@ export type ChartLegendProps<T> = {
|
|
|
187
194
|
hideAtWide?: boolean
|
|
188
195
|
}
|
|
189
196
|
|
|
197
|
+
const VISUALLY_HIDDEN = {
|
|
198
|
+
position: 'absolute',
|
|
199
|
+
width: '1px',
|
|
200
|
+
height: '1px',
|
|
201
|
+
overflow: 'hidden',
|
|
202
|
+
clipPath: 'inset(50%)',
|
|
203
|
+
whiteSpace: 'nowrap',
|
|
204
|
+
} as const
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The compact legend is ONE row (the stylesheet sets `nowrap` below 480px). Entries that do not fit
|
|
208
|
+
* fold into a `+N` chip (named for assistive tech by the folded labels), the last entry first. The
|
|
209
|
+
* entries are in display order: the reference/zone entries, which hue alone cannot name, lead the
|
|
210
|
+
* series (the title and tooltip name those).
|
|
211
|
+
* Measured on the node, never in React state, so a hover or a resize does not re-render.
|
|
212
|
+
*/
|
|
213
|
+
function foldLegend(row: HTMLElement): void {
|
|
214
|
+
const items = [...row.querySelectorAll<HTMLElement>('[data-legend-key]')]
|
|
215
|
+
const more = row.querySelector<HTMLElement>('[data-legend-more]')
|
|
216
|
+
for (const it of items) it.removeAttribute('data-legend-folded')
|
|
217
|
+
more?.setAttribute('hidden', '')
|
|
218
|
+
if (more === null || getComputedStyle(row).flexWrap !== 'nowrap') return
|
|
219
|
+
const display = items
|
|
220
|
+
const folded: HTMLElement[] = []
|
|
221
|
+
while (row.scrollWidth > row.clientWidth + 0.5 && folded.length < display.length - 1) {
|
|
222
|
+
const last = display[display.length - 1 - folded.length]
|
|
223
|
+
if (last === undefined) break
|
|
224
|
+
last.setAttribute('data-legend-folded', '')
|
|
225
|
+
folded.push(last)
|
|
226
|
+
more.removeAttribute('hidden')
|
|
227
|
+
const names = folded.map((f) => f.textContent).join(', ')
|
|
228
|
+
more.textContent = `+${folded.length}`
|
|
229
|
+
more.title = names
|
|
230
|
+
// The folded labels, visually hidden: assistive tech reads `+2: Caution, Danger`.
|
|
231
|
+
const hiddenNames = document.createElement('span')
|
|
232
|
+
hiddenNames.textContent = `: ${names}`
|
|
233
|
+
Object.assign(hiddenNames.style, VISUALLY_HIDDEN)
|
|
234
|
+
more.append(hiddenNames)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
190
238
|
export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null {
|
|
239
|
+
const rowRef = useRef<HTMLFieldSetElement>(null)
|
|
240
|
+
const auto = props.mode === 'auto'
|
|
241
|
+
// Re-fold when the entries change; a resize re-folds through the observer.
|
|
242
|
+
const signature = props.series.map((s) => s.key).join('|')
|
|
243
|
+
// `signature` is intentionally an extra dep: the fold must re-run when the entries change.
|
|
244
|
+
useLayoutEffect(() => {
|
|
245
|
+
if (auto && rowRef.current !== null) foldLegend(rowRef.current)
|
|
246
|
+
}, [auto, signature])
|
|
247
|
+
// Same: `signature` re-subscribes the observer to the re-rendered row.
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
const row = rowRef.current
|
|
250
|
+
if (!auto || row === null || typeof ResizeObserver === 'undefined') return
|
|
251
|
+
const observer = new ResizeObserver(() => foldLegend(row))
|
|
252
|
+
observer.observe(row)
|
|
253
|
+
return () => observer.disconnect()
|
|
254
|
+
}, [auto, signature])
|
|
191
255
|
const groups = legendEntries(props.series, props.groups)
|
|
192
256
|
if (groups.length === 0) return null
|
|
193
|
-
const auto = props.mode === 'auto'
|
|
194
257
|
return (
|
|
195
258
|
<fieldset
|
|
259
|
+
ref={rowRef}
|
|
196
260
|
aria-label="Legend"
|
|
197
261
|
data-basalt-chart-legend={props.mode}
|
|
198
262
|
// `visibility: hidden` is the real guard (nothing is announced, focused or clickable);
|
|
@@ -219,6 +283,7 @@ export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null
|
|
|
219
283
|
<span
|
|
220
284
|
key={s.key}
|
|
221
285
|
data-legend-key={s.key}
|
|
286
|
+
data-legend-role={s.role ?? 'series'}
|
|
222
287
|
style={itemStyle}
|
|
223
288
|
onMouseEnter={() => props.onHighlight(s.key)}
|
|
224
289
|
onMouseLeave={() => props.onHighlight(null)}
|
|
@@ -231,6 +296,7 @@ export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null
|
|
|
231
296
|
key={s.key}
|
|
232
297
|
type="button"
|
|
233
298
|
data-legend-key={s.key}
|
|
299
|
+
data-legend-role={s.role ?? 'series'}
|
|
234
300
|
data-basalt-hit=""
|
|
235
301
|
aria-pressed={!off}
|
|
236
302
|
style={itemStyle}
|
|
@@ -245,6 +311,7 @@ export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null
|
|
|
245
311
|
)
|
|
246
312
|
}),
|
|
247
313
|
])}
|
|
314
|
+
{auto && <span data-legend-more="" hidden style={noteStyle} />}
|
|
248
315
|
</fieldset>
|
|
249
316
|
)
|
|
250
317
|
}
|
package/src/charts/ref-label.ts
CHANGED
|
@@ -102,3 +102,30 @@ export function refLabelPosition(opts: {
|
|
|
102
102
|
order.find((p) => inPlot(p) && clear(p)) ?? order.find(inPlot) ?? (order[0] as RefLabelPosition)
|
|
103
103
|
)
|
|
104
104
|
}
|
|
105
|
+
|
|
106
|
+
/** The gap between a sparkline reference label's box and its line, px. */
|
|
107
|
+
const SPARK_LABEL_GAP = 2
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Which side of its line a sparkline's reference label sits on, and its `dy` (the engine centres
|
|
111
|
+
* the text on its y): above by default, below when the line is too near the plot top for the label
|
|
112
|
+
* box to fit above it. The box clears the line by {@link SPARK_LABEL_GAP}, so the dashed rule never
|
|
113
|
+
* strikes the text, and stays inside the plot (`height` less the line inset each side).
|
|
114
|
+
*/
|
|
115
|
+
export function referenceLabelPlacement(opts: {
|
|
116
|
+
value: number
|
|
117
|
+
domain: readonly [number, number]
|
|
118
|
+
height: number
|
|
119
|
+
/** The plot's margin inside the svg box, px. */
|
|
120
|
+
inset: number
|
|
121
|
+
fontSize: number
|
|
122
|
+
}): { side: 'above' | 'below'; dy: number } {
|
|
123
|
+
const { value, domain, height, inset, fontSize } = opts
|
|
124
|
+
const [lo, hi] = [Math.min(...domain), Math.max(...domain)]
|
|
125
|
+
const share = hi === lo ? 0 : (value - lo) / (hi - lo)
|
|
126
|
+
const line = inset + (height - 2 * inset) * (1 - share)
|
|
127
|
+
const reach = SPARK_LABEL_GAP + fontSize + 1
|
|
128
|
+
const side = line - reach >= 0 || line + reach > height ? 'above' : 'below'
|
|
129
|
+
const dy = (SPARK_LABEL_GAP + (fontSize + 1) / 2) * (side === 'above' ? -1 : 1)
|
|
130
|
+
return { side, dy }
|
|
131
|
+
}
|