basalt-ui 1.22.0 → 1.23.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/AGENTS.md +9 -1
- package/CHANGELOG.md +19 -0
- package/MIGRATING.md +125 -12
- package/README.md +45 -11
- package/agent/rules/basalt-app.md +26 -2
- package/agent/rules/basalt-charts.md +40 -5
- package/agent/rules/basalt-tokens.md +17 -1
- package/agent/skills/basalt-charts/SKILL.md +23 -8
- package/agent/templates/DESIGN.md.tpl +3 -1
- package/configs/oxlint-plugin.js +49 -8
- package/configs/oxlint-plugin.test.ts +88 -31
- package/dist/charts/hooks/useBandPlot.d.ts +138 -0
- package/dist/charts/hooks/useBandPlot.d.ts.map +1 -0
- package/dist/charts/hooks/useBandPlot.js +127 -0
- package/dist/charts/index.d.ts +4 -0
- package/dist/charts/index.d.ts.map +1 -1
- package/dist/charts/index.js +14 -0
- package/dist/charts/kinds/BandStrip.d.ts +113 -0
- package/dist/charts/kinds/BandStrip.d.ts.map +1 -0
- package/dist/charts/kinds/BandStrip.js +218 -0
- package/dist/charts/kinds/BandStrip.test.d.ts +2 -0
- package/dist/charts/kinds/BandStrip.test.d.ts.map +1 -0
- package/dist/charts/kinds/Bars.d.ts +5 -1
- package/dist/charts/kinds/Bars.d.ts.map +1 -1
- package/dist/charts/kinds/Bars.js +2 -0
- package/dist/charts/kinds/MirroredBars.d.ts +93 -0
- package/dist/charts/kinds/MirroredBars.d.ts.map +1 -0
- package/dist/charts/kinds/MirroredBars.js +315 -0
- package/dist/charts/kinds/MirroredBars.test.d.ts +2 -0
- package/dist/charts/kinds/MirroredBars.test.d.ts.map +1 -0
- package/dist/charts/kinds/MultiLine.d.ts +5 -1
- package/dist/charts/kinds/MultiLine.d.ts.map +1 -1
- package/dist/charts/kinds/MultiLine.js +2 -0
- package/dist/charts/kinds/StackedArea.d.ts +5 -1
- package/dist/charts/kinds/StackedArea.d.ts.map +1 -1
- package/dist/charts/kinds/StackedArea.js +2 -0
- package/dist/charts/kinds/ZonedLine.d.ts +5 -1
- package/dist/charts/kinds/ZonedLine.d.ts.map +1 -1
- package/dist/charts/kinds/ZonedLine.js +2 -0
- package/dist/charts/primitives/CartesianChart.d.ts +18 -2
- package/dist/charts/primitives/CartesianChart.d.ts.map +1 -1
- package/dist/charts/primitives/CartesianChart.js +5 -2
- package/dist/charts/primitives/HatchPattern.d.ts +31 -0
- package/dist/charts/primitives/HatchPattern.d.ts.map +1 -0
- package/dist/charts/primitives/HatchPattern.js +31 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +47 -8
- package/dist/cli/shipped-versions.test.d.ts +2 -0
- package/dist/cli/shipped-versions.test.d.ts.map +1 -0
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +32 -11
- package/dist/vite.d.ts +42 -3
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +67 -30
- package/llms.txt +2 -2
- package/package.json +1 -1
- package/src/charts/hooks/useBandPlot.ts +302 -0
- package/src/charts/index.ts +14 -0
- package/src/charts/kinds/BandStrip.test.tsx +200 -0
- package/src/charts/kinds/BandStrip.tsx +364 -0
- package/src/charts/kinds/Bars.tsx +7 -1
- package/src/charts/kinds/DualPanel.test.tsx +10 -4
- package/src/charts/kinds/MirroredBars.test.tsx +193 -0
- package/src/charts/kinds/MirroredBars.tsx +455 -0
- package/src/charts/kinds/MultiLine.tsx +7 -1
- package/src/charts/kinds/StackedArea.tsx +7 -1
- package/src/charts/kinds/ZonedLine.tsx +7 -1
- package/src/charts/primitives/CartesianChart.test.tsx +86 -4
- package/src/charts/primitives/CartesianChart.tsx +28 -3
- package/src/charts/primitives/HatchPattern.tsx +55 -0
- package/src/cli/index.ts +125 -18
- package/src/cli/shipped-versions.test.ts +83 -0
- package/src/cli/toolchain-wiring.test.ts +171 -4
- package/src/cli/waiver-audit.test.ts +7 -3
- package/src/guard/check-source.test.ts +138 -42
- package/src/guard/index.ts +83 -17
- package/src/vite.test.ts +79 -0
- package/src/vite.ts +141 -39
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The choreography shared by every BANDED x plot — one slot per datum, drawn as a rect, with no
|
|
3
|
+
* point in between.
|
|
4
|
+
*
|
|
5
|
+
* `CartesianChart` cannot host these: its x scale is `scalePoint` (positions, no width) and it
|
|
6
|
+
* renders `AxisLeftNumeric` unconditionally. `BandStrip` has no numeric y axis at all and
|
|
7
|
+
* `MirroredBars` has two, in two independent scales. Both are therefore
|
|
8
|
+
* `basalt/hand-rolled-plot` shapes — and this module is what stops the second one from being a
|
|
9
|
+
* copy of the first, per `DualPanel`'s own instruction to "promote the shared choreography rather
|
|
10
|
+
* than copying this file".
|
|
11
|
+
*
|
|
12
|
+
* What lives here: width-driven folding, the measured gutters, the band scale and its
|
|
13
|
+
* half-bandwidth cursor correction, the shared cursor itself, and the source/follower tooltip
|
|
14
|
+
* anchor arithmetic. What does NOT: any mark. See `docs/CHARTS-SPEC.md` § "The contract".
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { scaleBand } from '@visx/scale'
|
|
18
|
+
import { useCallback, useMemo, useRef } from 'react'
|
|
19
|
+
import type { ReactNode, RefObject } from 'react'
|
|
20
|
+
import { VX } from '../../tokens'
|
|
21
|
+
import type { ChartMargin } from '../../tokens'
|
|
22
|
+
import type { CursorResolution } from '../cursor/resolve'
|
|
23
|
+
import { autoMargin } from '../layout/auto-margin'
|
|
24
|
+
import { maxTextWidth } from '../utils/measure-text'
|
|
25
|
+
import { smartTicks } from '../utils/ticks'
|
|
26
|
+
import { useChartCursor } from './useChartCursor'
|
|
27
|
+
import type { ChartCursor, CursorAnchor } from './useChartCursor'
|
|
28
|
+
|
|
29
|
+
type BandScale = ReturnType<typeof scaleBand<string>>
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Collapse adjacent data into one drawn band at widths where one-band-per-datum sub-pixels.
|
|
33
|
+
*
|
|
34
|
+
* 288 buckets over a 234px plot is a 0.81px pitch: every band draws at a flat 1px and a single
|
|
35
|
+
* catastrophic slot can be overdrawn by its neighbour. Folding is the only honest answer — the
|
|
36
|
+
* alternative is a chart that silently loses its worst column.
|
|
37
|
+
*/
|
|
38
|
+
export type BandFold<T> = {
|
|
39
|
+
/**
|
|
40
|
+
* Floor on the px pitch of a drawn band. Default 3 — not 2, because a band also has to be wide
|
|
41
|
+
* enough for a partial-absence split (`absentFraction`) to render as two visibly distinct
|
|
42
|
+
* pieces, and at 2px the fill and the hatch antialias into one smudge.
|
|
43
|
+
*/
|
|
44
|
+
minBandPx?: number
|
|
45
|
+
/**
|
|
46
|
+
* Collapse a group of adjacent source data into the one datum drawn in its place.
|
|
47
|
+
*
|
|
48
|
+
* The merge is the CONSUMER's, deliberately: whether a folded slot's value is a max, a sum, or
|
|
49
|
+
* a rate recomputed from summed parts is a question about the measurement, not about the
|
|
50
|
+
* chart — and averaging a fully-down bucket into its clean neighbours describes a bucket that
|
|
51
|
+
* never happened. The kind owns only the arithmetic of how many groups there are.
|
|
52
|
+
*
|
|
53
|
+
* Identity (`getX`) MUST come from `group[0]`, or the axis stops being monotone and the cursor
|
|
54
|
+
* key stops naming a real slot start.
|
|
55
|
+
*/
|
|
56
|
+
merge: (group: T[]) => T
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const DEFAULT_MIN_BAND_PX = 3
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Terminal-gutter cap, as a fraction of the plot width.
|
|
63
|
+
*
|
|
64
|
+
* A gutter that exists only so a centred terminal tick label does not clip is half a label wide —
|
|
65
|
+
* right at 1500px and absurd at 390, where half a `DD.MM HH:MM` label is 48px of a 338px chart
|
|
66
|
+
* spent on empty margin. A gutter measured from an AXIS's own labels is never capped: shrinking
|
|
67
|
+
* that one clips the reading itself.
|
|
68
|
+
*/
|
|
69
|
+
const TERMINAL_GUTTER_MAX_FRACTION = { left: 0.14, right: 0.12 }
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The grouping half of {@link BandFold}, exported so a consumer can test their `merge` against the
|
|
73
|
+
* arithmetic that will actually run rather than re-implementing it in the test file — the merge is
|
|
74
|
+
* where the honesty rules live (max vs mean vs a rate recomputed from summed parts), and a test
|
|
75
|
+
* that groups differently from the kind is testing the wrong function.
|
|
76
|
+
*/
|
|
77
|
+
export function foldBands<T>(data: readonly T[], cap: number, merge: (group: T[]) => T): T[] {
|
|
78
|
+
if (cap <= 0) return []
|
|
79
|
+
if (data.length <= cap) return [...data]
|
|
80
|
+
const groupSize = Math.ceil(data.length / cap)
|
|
81
|
+
const out: T[] = []
|
|
82
|
+
for (let i = 0; i < data.length; i += groupSize) out.push(merge(data.slice(i, i + groupSize)))
|
|
83
|
+
return out
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Tooltip seams shared by `BandStrip` and `MirroredBars` — the `CartesianTooltipConfig` subset
|
|
87
|
+
* that means anything on a banded plot (there is no per-point `follow` distinction to make that
|
|
88
|
+
* `CartesianChart` doesn't already document identically). */
|
|
89
|
+
export type BandTooltipConfig<T> = {
|
|
90
|
+
/** Default true — the tooltip tracks the pointer. `false` anchors it to the crosshair at the
|
|
91
|
+
* plot's top edge, so a column of charts sharing one cursor lines every tooltip up on one x. */
|
|
92
|
+
follow?: boolean
|
|
93
|
+
/** Right-aligned badge in the tooltip header. */
|
|
94
|
+
label?: (d: T) => { text: string; color: string } | null
|
|
95
|
+
/** Overrides the header's date text — same seam, same reason, as
|
|
96
|
+
* `CartesianTooltipConfig.formatHeader`. */
|
|
97
|
+
formatHeader?: (key: string, d: T) => string
|
|
98
|
+
/** Rows rendered BEFORE the derived rows. `hidden` is the same legend-toggle set the marks
|
|
99
|
+
* draw from, so a hand-authored row cannot desync from it. */
|
|
100
|
+
prependRows?: (d: T, ctx: BandTooltipRowContext) => ReactNode
|
|
101
|
+
/** Rows appended after the derived rows. Same `ctx` as {@link prependRows}. */
|
|
102
|
+
extraRows?: (d: T, ctx: BandTooltipRowContext) => ReactNode
|
|
103
|
+
/**
|
|
104
|
+
* Render this chart's tooltip when it is a cursor FOLLOWER, not only when it is the SOURCE.
|
|
105
|
+
* Default false. A follower has no pointer under it, so it always anchors to the crosshair
|
|
106
|
+
* regardless of `follow`; only the SOURCE is `aria-live`. Costs one `getBoundingClientRect`
|
|
107
|
+
* per hovered frame per opted-in chart — see `CartesianTooltipConfig.onFollow`.
|
|
108
|
+
*/
|
|
109
|
+
onFollow?: boolean
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type BandTooltipRowContext = { hidden: ReadonlySet<string> }
|
|
113
|
+
|
|
114
|
+
export type UseBandPlotInput<T> = {
|
|
115
|
+
data: readonly T[]
|
|
116
|
+
chartId: string
|
|
117
|
+
getX: (d: T) => string
|
|
118
|
+
formatX: (key: string) => string
|
|
119
|
+
/** The plot rect's width, as handed down by `ChartFrame`. */
|
|
120
|
+
width: number
|
|
121
|
+
fold?: BandFold<T>
|
|
122
|
+
cursorResolution?: CursorResolution
|
|
123
|
+
/**
|
|
124
|
+
* Which of the (possibly folded) keys get a tick. Default `smartTicks`. The seam exists because
|
|
125
|
+
* `smartTicks` appends the final value unconditionally, which on a dense time axis puts the last
|
|
126
|
+
* two labels on top of each other — a chart whose x labels are richer than `DD.MM` needs its own
|
|
127
|
+
* rule.
|
|
128
|
+
*/
|
|
129
|
+
xTickValues?: (keys: readonly string[], plotWidth: number) => readonly string[]
|
|
130
|
+
/** Left-axis tick labels, already formatted. Empty for a plot with no left axis. */
|
|
131
|
+
leftLabels?: readonly string[]
|
|
132
|
+
/** Per-side margin overrides — applied last, so the escape hatch always wins. */
|
|
133
|
+
margin?: Partial<ChartMargin>
|
|
134
|
+
tooltip?: BandTooltipConfig<T> | false
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type BandPlot<T> = {
|
|
138
|
+
/** The data actually drawn — `data` when it fits, the folded slots when it does not. */
|
|
139
|
+
bands: T[]
|
|
140
|
+
keys: string[]
|
|
141
|
+
margin: ChartMargin
|
|
142
|
+
plotWidth: number
|
|
143
|
+
scale: BandScale
|
|
144
|
+
cursor: ChartCursor<T>
|
|
145
|
+
point: T | null
|
|
146
|
+
/** Plot-local x of the crosshair, or null when no point resolves. */
|
|
147
|
+
crosshairX: number | null
|
|
148
|
+
/** Pitch between adjacent band origins. */
|
|
149
|
+
step: number
|
|
150
|
+
/** Drawn width of one band — the pitch minus a 1px separator, floored at 1. */
|
|
151
|
+
bandWidth: number
|
|
152
|
+
tickValues: string[]
|
|
153
|
+
svgRef: RefObject<SVGSVGElement | null>
|
|
154
|
+
tooltipAnchor: CursorAnchor | null
|
|
155
|
+
showTooltip: boolean
|
|
156
|
+
/** Only the cursor SOURCE announces — N followers on one page would fire N live-region updates
|
|
157
|
+
* per pointer move. */
|
|
158
|
+
ariaLive: boolean
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Wire a banded plot into the framework's measured margins, band scale, shared cursor and tooltip
|
|
163
|
+
* positioning. Returns everything except the marks.
|
|
164
|
+
*/
|
|
165
|
+
export function useBandPlot<T>(input: UseBandPlotInput<T>): BandPlot<T> {
|
|
166
|
+
const {
|
|
167
|
+
data,
|
|
168
|
+
chartId,
|
|
169
|
+
getX,
|
|
170
|
+
formatX,
|
|
171
|
+
width,
|
|
172
|
+
fold,
|
|
173
|
+
cursorResolution,
|
|
174
|
+
xTickValues,
|
|
175
|
+
leftLabels,
|
|
176
|
+
margin: marginOverride,
|
|
177
|
+
tooltip,
|
|
178
|
+
} = input
|
|
179
|
+
|
|
180
|
+
const hasLeftAxis = leftLabels !== undefined && leftLabels.length > 0
|
|
181
|
+
const xLabelsAll = useMemo(() => data.map((d) => formatX(getX(d))), [data, getX, formatX])
|
|
182
|
+
|
|
183
|
+
// ── Pass 1: gutters, from the labels that will actually be painted ──────────────────────────
|
|
184
|
+
const margin = useMemo<ChartMargin>(() => {
|
|
185
|
+
const measured = autoMargin({
|
|
186
|
+
...(leftLabels !== undefined && { left: leftLabels }),
|
|
187
|
+
bottom: xLabelsAll,
|
|
188
|
+
...(marginOverride !== undefined && { override: marginOverride }),
|
|
189
|
+
})
|
|
190
|
+
if (marginOverride?.left !== undefined && marginOverride.right !== undefined) return measured
|
|
191
|
+
|
|
192
|
+
const halfXLabel = Math.ceil(maxTextWidth(xLabelsAll, VX.axisFont) / 2)
|
|
193
|
+
// A band axis puts its FIRST tick on the plot's left edge, so the left gutter needs the same
|
|
194
|
+
// half-label law `autoMargin` already applies on the right. `autoMargin` cannot know that —
|
|
195
|
+
// its left law measures a left AXIS, which a band plot may not have.
|
|
196
|
+
const left = hasLeftAxis ? measured.left : Math.max(measured.left, halfXLabel)
|
|
197
|
+
return {
|
|
198
|
+
...measured,
|
|
199
|
+
...(marginOverride?.left === undefined && {
|
|
200
|
+
left: hasLeftAxis
|
|
201
|
+
? left
|
|
202
|
+
: capTerminalGutter(left, width, TERMINAL_GUTTER_MAX_FRACTION.left, VX.margin.left),
|
|
203
|
+
}),
|
|
204
|
+
...(marginOverride?.right === undefined && {
|
|
205
|
+
right: capTerminalGutter(
|
|
206
|
+
measured.right,
|
|
207
|
+
width,
|
|
208
|
+
TERMINAL_GUTTER_MAX_FRACTION.right,
|
|
209
|
+
VX.margin.right,
|
|
210
|
+
),
|
|
211
|
+
}),
|
|
212
|
+
}
|
|
213
|
+
}, [leftLabels, xLabelsAll, marginOverride, hasLeftAxis, width])
|
|
214
|
+
|
|
215
|
+
const plotWidth = Math.max(width - margin.left - margin.right, 0)
|
|
216
|
+
|
|
217
|
+
// ── Pass 2: the fold, BEFORE the scale — so the domain, and therefore the cursor's key space,
|
|
218
|
+
// is the grid actually drawn rather than the raw one ────────────────────────────────────────
|
|
219
|
+
const minBandPx = fold?.minBandPx ?? DEFAULT_MIN_BAND_PX
|
|
220
|
+
const merge = fold?.merge
|
|
221
|
+
const bands = useMemo(
|
|
222
|
+
() =>
|
|
223
|
+
merge === undefined ? [...data] : foldBands(data, Math.floor(plotWidth / minBandPx), merge),
|
|
224
|
+
[data, plotWidth, minBandPx, merge],
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
const keys = useMemo(() => bands.map(getX), [bands, getX])
|
|
228
|
+
const scale = useMemo(
|
|
229
|
+
() => scaleBand<string>({ domain: keys, range: [0, plotWidth] }),
|
|
230
|
+
[keys, plotWidth],
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
// `+ bandwidth()/2` is mandatory: `useChartCursor`'s nearest-point loop compares the pointer
|
|
234
|
+
// against `xScale(getKey(d))`, and `scaleBand` returns the band's LEFT edge. Passing `scale`
|
|
235
|
+
// raw biases every snap by half a band — at 288 bands, a systematic one-slot-early cursor.
|
|
236
|
+
const bandCenter = useCallback(
|
|
237
|
+
(key: string) => {
|
|
238
|
+
const v = scale(key)
|
|
239
|
+
return v === undefined ? undefined : v + scale.bandwidth() / 2
|
|
240
|
+
},
|
|
241
|
+
[scale],
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
const cursor = useChartCursor<T>({
|
|
245
|
+
data: bands,
|
|
246
|
+
chartId,
|
|
247
|
+
getKey: getX,
|
|
248
|
+
xScale: bandCenter,
|
|
249
|
+
marginLeft: margin.left,
|
|
250
|
+
...(cursorResolution !== undefined && { resolution: cursorResolution }),
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
const tickValues = useMemo(
|
|
254
|
+
() => [
|
|
255
|
+
...(xTickValues === undefined ? smartTicks(keys, plotWidth) : xTickValues(keys, plotWidth)),
|
|
256
|
+
],
|
|
257
|
+
[keys, plotWidth, xTickValues],
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
const svgRef = useRef<SVGSVGElement>(null)
|
|
261
|
+
const point = cursor.point
|
|
262
|
+
const crosshairX = point === null ? null : (bandCenter(getX(point)) ?? null)
|
|
263
|
+
|
|
264
|
+
const cfg = tooltip === false ? undefined : tooltip
|
|
265
|
+
// Identical seam to `CartesianChart`: SOURCE always, FOLLOWER only when opted in. A follower has
|
|
266
|
+
// no pointer under it, so it is always positioned via the anchored (crosshair) path.
|
|
267
|
+
const isFollowerRender = !cursor.isSource && cfg?.onFollow === true
|
|
268
|
+
const showTooltip = tooltip !== false && point !== null && (cursor.isSource || isFollowerRender)
|
|
269
|
+
const useAnchored = cfg?.follow === false || isFollowerRender
|
|
270
|
+
const anchorX = useAnchored ? crosshairX : null
|
|
271
|
+
const svgRect = anchorX === null ? undefined : svgRef.current?.getBoundingClientRect()
|
|
272
|
+
const tooltipAnchor =
|
|
273
|
+
svgRect === undefined || anchorX === null
|
|
274
|
+
? cursor.anchor
|
|
275
|
+
: { x: svgRect.left + margin.left + anchorX, y: svgRect.top + margin.top }
|
|
276
|
+
|
|
277
|
+
const step = bands.length > 0 ? plotWidth / bands.length : 0
|
|
278
|
+
const bandWidth = Math.max(step - 1, 1)
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
bands,
|
|
282
|
+
keys,
|
|
283
|
+
margin,
|
|
284
|
+
plotWidth,
|
|
285
|
+
scale,
|
|
286
|
+
cursor,
|
|
287
|
+
point,
|
|
288
|
+
crosshairX,
|
|
289
|
+
step,
|
|
290
|
+
bandWidth,
|
|
291
|
+
tickValues,
|
|
292
|
+
svgRef,
|
|
293
|
+
tooltipAnchor,
|
|
294
|
+
showTooltip,
|
|
295
|
+
ariaLive: cursor.isSource,
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Shrink a half-label gutter on a narrow plot, but never below the token floor. */
|
|
300
|
+
function capTerminalGutter(gutter: number, width: number, fraction: number, floor: number): number {
|
|
301
|
+
return Math.min(gutter, Math.max(floor, Math.round(width * fraction)))
|
|
302
|
+
}
|
package/src/charts/index.ts
CHANGED
|
@@ -75,6 +75,7 @@ export {
|
|
|
75
75
|
type ChartPendingProps,
|
|
76
76
|
} from './primitives/ChartPending'
|
|
77
77
|
export { Crosshair, SeriesDot } from './primitives/Crosshair'
|
|
78
|
+
export { HatchPattern, hatchFill, hatchSizeFor } from './primitives/HatchPattern'
|
|
78
79
|
export { ChartCard } from './primitives/ChartCard'
|
|
79
80
|
export { ChartLegend, type LegendEntry } from './primitives/ChartLegend'
|
|
80
81
|
export {
|
|
@@ -91,6 +92,12 @@ export { AreaGradient, areaFillUrl } from './primitives/AreaGradient'
|
|
|
91
92
|
|
|
92
93
|
// ── Hooks ────────────────────────────────────────────────────────────────
|
|
93
94
|
export { useChartCursor, type ChartCursor, type CursorAnchor } from './hooks/useChartCursor'
|
|
95
|
+
export {
|
|
96
|
+
foldBands,
|
|
97
|
+
type BandFold,
|
|
98
|
+
type BandTooltipConfig,
|
|
99
|
+
type BandTooltipRowContext,
|
|
100
|
+
} from './hooks/useBandPlot'
|
|
94
101
|
export type { CursorResolution } from './cursor/resolve'
|
|
95
102
|
export { useChartSize, type UseChartSizeResult, type ChartSize } from './hooks/useChartSize'
|
|
96
103
|
|
|
@@ -124,6 +131,13 @@ export { Donut, type DonutProps, type DonutDatum } from './kinds/Donut'
|
|
|
124
131
|
export { MultiLine, type MultiLineProps } from './kinds/MultiLine'
|
|
125
132
|
export { DualPanel, type DualPanelProps } from './kinds/DualPanel'
|
|
126
133
|
export { Heatmap, type HeatmapProps } from './kinds/Heatmap'
|
|
134
|
+
export {
|
|
135
|
+
BandStrip,
|
|
136
|
+
type BandStripProps,
|
|
137
|
+
type BandStripSeries,
|
|
138
|
+
type BandSpan,
|
|
139
|
+
} from './kinds/BandStrip'
|
|
140
|
+
export { MirroredBars, type MirroredBarsProps, type MirroredBarPane } from './kinds/MirroredBars'
|
|
127
141
|
|
|
128
142
|
// ── Sparklines ───────────────────────────────────────────────────────────
|
|
129
143
|
export { LineSparkline, BarSparkline } from './sparklines'
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `BandStrip` — the four properties that make it a KIND rather than a rect loop: no left axis at
|
|
3
|
+
* all (the reason `CartesianChart` cannot host it), the absence split a fold owes the reader, the
|
|
4
|
+
* derived tooltip row, and the fold arithmetic itself.
|
|
5
|
+
*
|
|
6
|
+
* Under this DOM harness there is no ResizeObserver, so `ChartFrame` falls back to its `minWidth`
|
|
7
|
+
* floor — `plot.width` is a fixed 200, which is what makes the fold/geometry assertions below
|
|
8
|
+
* deterministic.
|
|
9
|
+
*/
|
|
10
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
11
|
+
import { describe, expect, test } from 'bun:test'
|
|
12
|
+
import { ChartCursorScope } from '../cursor/scope'
|
|
13
|
+
import { BandStrip } from './BandStrip'
|
|
14
|
+
import type { BandStripSeries } from './BandStrip'
|
|
15
|
+
|
|
16
|
+
type Slot = { key: string; loss: number | null; folded: number; unmeasured: number }
|
|
17
|
+
|
|
18
|
+
const slot = (key: string, loss: number | null): Slot => ({
|
|
19
|
+
key,
|
|
20
|
+
loss,
|
|
21
|
+
folded: 1,
|
|
22
|
+
unmeasured: loss === null ? 1 : 0,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const SERIES: BandStripSeries<Slot>[] = [
|
|
26
|
+
{ key: 'clean', label: 'No loss', color: '#0a0', mark: 'bar', fillOpacity: 0.14 },
|
|
27
|
+
{
|
|
28
|
+
key: 'loss',
|
|
29
|
+
label: 'Packet loss',
|
|
30
|
+
color: '#a00',
|
|
31
|
+
mark: 'bar',
|
|
32
|
+
formatValue: (d) => `${d.loss?.toFixed(1)}%`,
|
|
33
|
+
},
|
|
34
|
+
{ key: 'absent', label: 'Not measured', color: '#888', mark: 'bar', fillOpacity: 0.5 },
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
const getBand = (d: Slot) => ({
|
|
38
|
+
state: d.loss === null ? 'absent' : d.loss > 0 ? 'loss' : 'clean',
|
|
39
|
+
...(d.unmeasured > 0 && d.loss !== null && { absentFraction: d.unmeasured / d.folded }),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
function renderStrip(props: Partial<Parameters<typeof BandStrip<Slot>>[0]> = {}) {
|
|
43
|
+
const data = props.data ?? [
|
|
44
|
+
slot('2026-08-01', 0),
|
|
45
|
+
slot('2026-08-02', 2.5),
|
|
46
|
+
slot('2026-08-03', null),
|
|
47
|
+
]
|
|
48
|
+
return render(
|
|
49
|
+
<ChartCursorScope>
|
|
50
|
+
<BandStrip<Slot>
|
|
51
|
+
data={data}
|
|
52
|
+
chartId="strip"
|
|
53
|
+
getX={(d) => d.key}
|
|
54
|
+
series={SERIES}
|
|
55
|
+
getBand={getBand}
|
|
56
|
+
height={120}
|
|
57
|
+
{...props}
|
|
58
|
+
/>
|
|
59
|
+
</ChartCursorScope>,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe('BandStrip — no numeric y axis', () => {
|
|
64
|
+
test('renders a bottom axis and NO left axis — the one thing CartesianChart cannot express', () => {
|
|
65
|
+
const { container } = renderStrip()
|
|
66
|
+
expect(container.querySelector('.visx-axis-bottom')).not.toBeNull()
|
|
67
|
+
expect(container.querySelector('.visx-axis-left')).toBeNull()
|
|
68
|
+
expect(container.querySelector('.visx-axis-right')).toBeNull()
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('BandStrip — fills derive from series', () => {
|
|
73
|
+
test('a band takes its state entry colour + fillOpacity; an explicit fill overrides only that band', () => {
|
|
74
|
+
const { container } = renderStrip({
|
|
75
|
+
getBand: (d: Slot) =>
|
|
76
|
+
d.loss !== null && d.loss > 0
|
|
77
|
+
? { state: 'loss', fill: 'color-mix(in srgb, #a00 62%, transparent)' }
|
|
78
|
+
: getBand(d),
|
|
79
|
+
})
|
|
80
|
+
const fills = [...container.querySelectorAll('rect[fill]')]
|
|
81
|
+
.map((r) => r.getAttribute('fill'))
|
|
82
|
+
.filter((f): f is string => f !== null && f !== 'transparent')
|
|
83
|
+
|
|
84
|
+
// Derived: `alpha(color, fillOpacity)` for clean (0.14) and absent (0.5).
|
|
85
|
+
expect(fills).toContain('color-mix(in srgb, #0a0 14%, transparent)')
|
|
86
|
+
expect(fills).toContain('color-mix(in srgb, #888 50%, transparent)')
|
|
87
|
+
// Overridden: the ramp value, not the series default.
|
|
88
|
+
expect(fills).toContain('color-mix(in srgb, #a00 62%, transparent)')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('a state key absent from `series` draws nothing rather than an unlegended mark', () => {
|
|
92
|
+
const { container } = renderStrip({ getBand: () => ({ state: 'nope' }) })
|
|
93
|
+
const painted = [...container.querySelectorAll('rect[fill]')].filter(
|
|
94
|
+
(r) => r.getAttribute('fill') !== 'transparent',
|
|
95
|
+
)
|
|
96
|
+
expect(painted).toHaveLength(0)
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
describe('BandStrip — absenceFraction splits a folded band', () => {
|
|
101
|
+
test('a part-measured band draws a measured rect + a hatch rect in proportion', () => {
|
|
102
|
+
const partial: Slot = { key: '2026-08-01', loss: 1, folded: 4, unmeasured: 3 }
|
|
103
|
+
const { container } = renderStrip({ data: [partial] })
|
|
104
|
+
const painted = [...container.querySelectorAll('rect[fill]')].filter(
|
|
105
|
+
(r) => r.getAttribute('fill') !== 'transparent',
|
|
106
|
+
)
|
|
107
|
+
expect(painted).toHaveLength(2)
|
|
108
|
+
const measured = Number(painted[0]?.getAttribute('width'))
|
|
109
|
+
const hatched = Number(painted[1]?.getAttribute('width'))
|
|
110
|
+
// 3 of 4 members unmeasured → the hatch owns three quarters of the band.
|
|
111
|
+
expect(hatched / (measured + hatched)).toBeCloseTo(0.75, 5)
|
|
112
|
+
expect(painted[1]?.getAttribute('fill')).toBe('url(#strip-band-absent)')
|
|
113
|
+
// The hatch starts where the measured share ends — not overlaid on it.
|
|
114
|
+
expect(Number(painted[1]?.getAttribute('x'))).toBeCloseTo(
|
|
115
|
+
Number(painted[0]?.getAttribute('x')) + measured,
|
|
116
|
+
5,
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('a non-finite share (a 0/0 fold count) draws a full band, never a NaN width', () => {
|
|
121
|
+
const degenerate: Slot = { key: '2026-08-01', loss: 1, folded: 0, unmeasured: 0 }
|
|
122
|
+
const { container } = renderStrip({ data: [degenerate] })
|
|
123
|
+
expect(container.innerHTML).not.toContain('NaN')
|
|
124
|
+
const painted = [...container.querySelectorAll('rect[fill]')].filter(
|
|
125
|
+
(r) => r.getAttribute('fill') !== 'transparent',
|
|
126
|
+
)
|
|
127
|
+
expect(painted).toHaveLength(1)
|
|
128
|
+
expect(Number(painted[0]?.getAttribute('width'))).toBeGreaterThan(0)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('a fully absent band is all hatch and no fill', () => {
|
|
132
|
+
const gone: Slot = { key: '2026-08-01', loss: 2, folded: 3, unmeasured: 3 }
|
|
133
|
+
const { container } = renderStrip({ data: [gone] })
|
|
134
|
+
const painted = [...container.querySelectorAll('rect[fill]')].filter(
|
|
135
|
+
(r) => r.getAttribute('fill') !== 'transparent',
|
|
136
|
+
)
|
|
137
|
+
expect(painted).toHaveLength(1)
|
|
138
|
+
expect(painted[0]?.getAttribute('fill')).toBe('url(#strip-band-absent)')
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
describe('BandStrip — fold', () => {
|
|
143
|
+
test('folds to the slots the measured width can draw, and the merge is the consumer’s', () => {
|
|
144
|
+
const many = Array.from({ length: 240 }, (_, i) =>
|
|
145
|
+
slot(`2026-08-${String((i % 28) + 1).padStart(2, '0')}T${String(i).padStart(3, '0')}`, i),
|
|
146
|
+
)
|
|
147
|
+
const merged: Slot[][] = []
|
|
148
|
+
const { container } = renderStrip({
|
|
149
|
+
data: many,
|
|
150
|
+
fold: {
|
|
151
|
+
merge: (group: Slot[]) => {
|
|
152
|
+
merged.push(group)
|
|
153
|
+
return {
|
|
154
|
+
...group[0]!,
|
|
155
|
+
loss: Math.max(...group.map((g) => g.loss ?? 0)),
|
|
156
|
+
folded: group.length,
|
|
157
|
+
unmeasured: group.filter((g) => g.loss === null).length,
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
// plot.width 200 → plotWidth ~132 → cap = floor(132 / 3) = 44 slots.
|
|
163
|
+
const drawn = container.querySelectorAll('.visx-group rect[fill]:not([fill="transparent"])')
|
|
164
|
+
expect(drawn.length).toBeGreaterThan(0)
|
|
165
|
+
expect(drawn.length).toBeLessThan(many.length)
|
|
166
|
+
expect(merged.length).toBe(drawn.length)
|
|
167
|
+
// MAX, not mean — the merge the consumer wrote is the one that ran.
|
|
168
|
+
expect(merged[0]!.length).toBeGreaterThan(1)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('data that already fits is drawn unfolded and `merge` is never called', () => {
|
|
172
|
+
let calls = 0
|
|
173
|
+
renderStrip({ fold: { merge: (g: Slot[]) => (calls++, g[0]!) } })
|
|
174
|
+
expect(calls).toBe(0)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
describe('BandStrip — the tooltip row is DERIVED from the hovered band’s state', () => {
|
|
179
|
+
test('shows that state’s label + formatValue, and never a state it is not drawing', async () => {
|
|
180
|
+
renderStrip({ ariaLabel: 'Availability' })
|
|
181
|
+
const slider = screen.getByRole('slider')
|
|
182
|
+
fireEvent.keyDown(slider, { key: 'ArrowRight' })
|
|
183
|
+
|
|
184
|
+
const tip = await screen.findByRole('tooltip')
|
|
185
|
+
expect(tip.textContent).toContain('No loss')
|
|
186
|
+
expect(tip.textContent).not.toContain('Packet loss')
|
|
187
|
+
expect(tip.textContent).not.toContain('Not measured')
|
|
188
|
+
|
|
189
|
+
fireEvent.keyDown(slider, { key: 'ArrowRight' })
|
|
190
|
+
expect(await screen.findByText('2.5%')).toBeTruthy()
|
|
191
|
+
const tip2 = screen.getByRole('tooltip')
|
|
192
|
+
expect(tip2.textContent).toContain('Packet loss')
|
|
193
|
+
expect(tip2.textContent).not.toContain('No loss')
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('the focusable overlay carries the chart’s own aria-label, not a generic one', () => {
|
|
197
|
+
renderStrip({ ariaLabel: 'Availability per bucket' })
|
|
198
|
+
expect(screen.getByRole('slider').getAttribute('aria-label')).toBe('Availability per bucket')
|
|
199
|
+
})
|
|
200
|
+
})
|