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/Bars.tsx
CHANGED
|
@@ -10,6 +10,7 @@ import { stack } from '@tanstack/charts/stack'
|
|
|
10
10
|
import { useCallback, useMemo } from 'react'
|
|
11
11
|
import type { ReactElement } from 'react'
|
|
12
12
|
import { CartesianRung } from './CartesianChart'
|
|
13
|
+
import { VX } from '../tokens'
|
|
13
14
|
import { emphasis, lineMark } from './marks'
|
|
14
15
|
import { rowX } from './types'
|
|
15
16
|
import type {
|
|
@@ -62,6 +63,27 @@ export type BarsProps<T, X extends ChartX = ChartX> = Omit<
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
const DEFAULT_BAR_OPACITY = 0.85
|
|
66
|
+
/** The faintest shade told-apart-by-shade series step down to. */
|
|
67
|
+
const SHADE_FLOOR = 0.4
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Series that share one colour and set no opacity of their own would draw identical bars. They step
|
|
71
|
+
* down from full strength to `SHADE_FLOOR` in `bars` order (the first is the strongest), so the legend
|
|
72
|
+
* swatches and the plot tell them apart by more than the label. A bar that sets `fillOpacity`, or a
|
|
73
|
+
* chart with `barOpacity`, keeps its own shades.
|
|
74
|
+
*/
|
|
75
|
+
function shadeSharedColors<T>(
|
|
76
|
+
bars: readonly BarsBar<T>[],
|
|
77
|
+
barOpacity: BarsProps<T>['barOpacity'],
|
|
78
|
+
): readonly BarsBar<T>[] {
|
|
79
|
+
if (barOpacity !== undefined) return bars
|
|
80
|
+
return bars.map((b) => {
|
|
81
|
+
const group = bars.filter((o) => o.color === b.color)
|
|
82
|
+
if (group.length < 2 || group.some((g) => g.fillOpacity !== undefined)) return b
|
|
83
|
+
const step = (1 - SHADE_FLOOR) / (group.length - 1)
|
|
84
|
+
return { ...b, fillOpacity: 1 - group.indexOf(b) * step }
|
|
85
|
+
})
|
|
86
|
+
}
|
|
65
87
|
|
|
66
88
|
const paint = (color: string, opacity: number): string =>
|
|
67
89
|
opacity >= 1 ? color : `color-mix(in srgb, ${color} ${Math.round(opacity * 100)}%, transparent)`
|
|
@@ -80,7 +102,9 @@ function guardLogStack(layout: string, onLog: boolean): void {
|
|
|
80
102
|
}
|
|
81
103
|
|
|
82
104
|
export function Bars<T, X extends ChartX = ChartX>(props: BarsProps<T, X>): ReactElement {
|
|
83
|
-
const { bars, lines, layout = 'stacked', barOpacity, ...rest } = props
|
|
105
|
+
const { bars: given, lines, layout = 'stacked', barOpacity, ...rest } = props
|
|
106
|
+
const bars = useMemo(() => shadeSharedColors(given, barOpacity), [given, barOpacity])
|
|
107
|
+
const sharedColor = bars.some((b, i) => bars.findIndex((o) => o.color === b.color) !== i)
|
|
84
108
|
guardLogStack(layout, bars.length > 0 && rest.y?.scale === 'log')
|
|
85
109
|
const sample = rest.data[0]
|
|
86
110
|
const series = useMemo<ChartSeries<T>[]>(
|
|
@@ -125,8 +149,11 @@ export function Bars<T, X extends ChartX = ChartX>(props: BarsProps<T, X>): Reac
|
|
|
125
149
|
y: 'y',
|
|
126
150
|
z: 'series',
|
|
127
151
|
fill,
|
|
152
|
+
// Series of one colour differ by shade alone: a surface hairline keeps stacked segments apart.
|
|
153
|
+
...(sharedColor &&
|
|
154
|
+
layout !== 'grouped' && { stroke: VX.surface.panel, strokeWidth: 1 }),
|
|
128
155
|
// The hovered x is its own highlight: the bar reads at full strength (no dimming, no
|
|
129
|
-
// point marker, no crosshair rule: that is line-chart chrome).
|
|
156
|
+
// point marker, no crosshair rule: that is line-chart chrome; `focusRing={false}` on the rung).
|
|
130
157
|
states: [{ when: { focus: 'x' }, style: { fill: solid } }],
|
|
131
158
|
layout:
|
|
132
159
|
layout === 'grouped'
|
|
@@ -140,7 +167,7 @@ export function Bars<T, X extends ChartX = ChartX>(props: BarsProps<T, X>): Reac
|
|
|
140
167
|
}
|
|
141
168
|
return out
|
|
142
169
|
},
|
|
143
|
-
[bars, layout, barOpacity],
|
|
170
|
+
[bars, layout, barOpacity, sharedColor],
|
|
144
171
|
)
|
|
145
172
|
|
|
146
173
|
return (
|
|
@@ -150,6 +177,7 @@ export function Bars<T, X extends ChartX = ChartX>(props: BarsProps<T, X>): Reac
|
|
|
150
177
|
marks={marks}
|
|
151
178
|
band
|
|
152
179
|
crosshair={false}
|
|
180
|
+
focusRing={false}
|
|
153
181
|
reverseRows={layout !== 'grouped'}
|
|
154
182
|
{...(layout !== 'grouped' && { stack: layout })}
|
|
155
183
|
/>
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import { useCallback, useId, useMemo, useState } from 'react'
|
|
10
10
|
import type { CSSProperties, ReactElement, ReactNode } from 'react'
|
|
11
11
|
import { useInChartCard } from './card-context'
|
|
12
|
-
import { compileCartesian, refLegendSeries } from './compile'
|
|
12
|
+
import { compileCartesian, refLegendSeries, zoneLegendSeries } from './compile'
|
|
13
13
|
import type { CartesianDefinition } from './compile'
|
|
14
14
|
import { samePlots } from './plot-context'
|
|
15
15
|
import type { PlotSizes } from './plot-context'
|
|
@@ -31,6 +31,8 @@ export type RungOptions = {
|
|
|
31
31
|
tooltipBelow?: boolean
|
|
32
32
|
/** No vertical rule under the cursor (a bar kind's hover is the bar itself). */
|
|
33
33
|
crosshair?: boolean
|
|
34
|
+
/** No engine point marker on the focused datum (a bar kind's hover is the bar itself). */
|
|
35
|
+
focusRing?: boolean
|
|
34
36
|
/** The legend goes from the wide class up, where `Lines`' end labels name the series. */
|
|
35
37
|
hideLegendAtWide?: boolean
|
|
36
38
|
}
|
|
@@ -196,8 +198,12 @@ export function CartesianRung<T, X extends ChartX>(
|
|
|
196
198
|
)
|
|
197
199
|
// Reference lines that name themselves join the legend after the series (role `reference`).
|
|
198
200
|
const legendSeries = useMemo(
|
|
199
|
-
() => [
|
|
200
|
-
|
|
201
|
+
() => [
|
|
202
|
+
...stable.series,
|
|
203
|
+
...refLegendSeries<T>(stable.refLines),
|
|
204
|
+
...zoneLegendSeries<T>(stable.zones),
|
|
205
|
+
],
|
|
206
|
+
[stable.series, stable.refLines, stable.zones],
|
|
201
207
|
)
|
|
202
208
|
return (
|
|
203
209
|
<ChartShell
|
package/src/charts/ChartCard.tsx
CHANGED
|
@@ -87,6 +87,20 @@ const bodyClipStyle: CSSProperties = {
|
|
|
87
87
|
padding: '2px var(--vx-space-card-inset-x, 1rem) var(--vx-space-card-inset-y, 0.75rem)',
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
const PERIOD_SEPARATOR = '· '
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A period list joins with `·`; behind a unit it leads with one too, so a one-item list is
|
|
94
|
+
* separated from the unit as the items are from each other (an item that already leads with it
|
|
95
|
+
* is not doubled). A plain string is written as is.
|
|
96
|
+
*/
|
|
97
|
+
function periodText(period: string | readonly string[], afterUnit: boolean): string {
|
|
98
|
+
if (typeof period === 'string') return period
|
|
99
|
+
if (period.length === 0) return ''
|
|
100
|
+
const text = period.join(' · ')
|
|
101
|
+
return afterUnit && !text.startsWith(PERIOD_SEPARATOR.trim()) ? PERIOD_SEPARATOR + text : text
|
|
102
|
+
}
|
|
103
|
+
|
|
90
104
|
/** One headline figure: the value is the hero, then its unit, a comparison node and the period. */
|
|
91
105
|
export type ChartFact = {
|
|
92
106
|
value: ReactNode
|
|
@@ -94,7 +108,7 @@ export type ChartFact = {
|
|
|
94
108
|
/** Usually a `DeltaBadge`. */
|
|
95
109
|
comparison?: ReactNode
|
|
96
110
|
/** Plain text the figure covers, e.g. `last 30 d`. A list is joined by ` · ` (`['last entry 3 d
|
|
97
|
-
* ago', '12 entries']`). */
|
|
111
|
+
* ago', '12 entries']`) and, behind a `unit`, led by one. */
|
|
98
112
|
period?: string | readonly string[] | undefined
|
|
99
113
|
/** Ink for the whole fact (text only, never a fill): a stale or off-target figure reads `warn`. */
|
|
100
114
|
tone?: ChartFactTone | undefined
|
|
@@ -186,25 +200,24 @@ function Facts({
|
|
|
186
200
|
{...(stale && { 'data-stale': 'true' })}
|
|
187
201
|
className={classes.factsRow}
|
|
188
202
|
>
|
|
189
|
-
{list.map((f, i) =>
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
{f.comparison}
|
|
201
|
-
{f.period !== undefined && (
|
|
202
|
-
<span className={classes.factPeriod}>
|
|
203
|
-
{typeof f.period === 'string' ? f.period : f.period.join(' · ')}
|
|
203
|
+
{list.map((f, i) => {
|
|
204
|
+
const period = f.period === undefined ? '' : periodText(f.period, f.unit !== undefined)
|
|
205
|
+
return (
|
|
206
|
+
<span
|
|
207
|
+
key={i}
|
|
208
|
+
data-basalt-fact=""
|
|
209
|
+
{...(f.tone !== undefined && { 'data-tone': f.tone })}
|
|
210
|
+
className={classes.fact}
|
|
211
|
+
>
|
|
212
|
+
<span data-basalt-fact-value="" className={classes.factValue}>
|
|
213
|
+
{f.value}
|
|
204
214
|
</span>
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
215
|
+
{f.unit !== undefined && <span className={classes.factUnit}>{f.unit}</span>}
|
|
216
|
+
{f.comparison}
|
|
217
|
+
{period !== '' && <span className={classes.factPeriod}>{period}</span>}
|
|
218
|
+
</span>
|
|
219
|
+
)
|
|
220
|
+
})}
|
|
208
221
|
</div>
|
|
209
222
|
{basis !== undefined && (
|
|
210
223
|
<p data-basalt-facts-basis="" className={classes.factsBasis}>
|
package/src/charts/Sparkline.tsx
CHANGED
|
@@ -12,7 +12,7 @@ import { ruleY } from '@tanstack/charts/rule'
|
|
|
12
12
|
import { scaleBand } from '@tanstack/charts/scales/band'
|
|
13
13
|
import { scaleLinear } from '@tanstack/charts/scales/linear'
|
|
14
14
|
import { text } from '@tanstack/charts/text'
|
|
15
|
-
import { useId, useMemo, useState } from 'react'
|
|
15
|
+
import { useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
16
16
|
import type { CSSProperties, FocusEvent, KeyboardEvent, PointerEvent, ReactElement } from 'react'
|
|
17
17
|
import { cx } from '../common/props'
|
|
18
18
|
import type { BasaltProps } from '../common/props'
|
|
@@ -20,7 +20,11 @@ import { VX, alpha } from '../tokens'
|
|
|
20
20
|
import type { CartesianDefinition } from './compile'
|
|
21
21
|
import { chartCurve } from './curves'
|
|
22
22
|
import { barMark } from './marks'
|
|
23
|
-
import {
|
|
23
|
+
import { VisualSlotContext } from '../dashboard/visual-slot'
|
|
24
|
+
import { bucketDay, dayGranular, defaultNumberFormat } from './compile'
|
|
25
|
+
import { referenceLabelPlacement } from './ref-label'
|
|
26
|
+
import { readoutClock, readoutDay } from './chart-dates'
|
|
27
|
+
import { toDate } from '../format/shared'
|
|
24
28
|
import { ChartEngine, idPrefixOf } from './host'
|
|
25
29
|
import classes from './sparkline.module.css'
|
|
26
30
|
import { ChartPlaceholder, resolveChartState } from './state'
|
|
@@ -30,7 +34,9 @@ import type { ChartMarks, ChartState } from './types'
|
|
|
30
34
|
export type SparklineProps = BasaltProps & {
|
|
31
35
|
/** The accessible name. Required: a chart without one is an unlabeled image. */
|
|
32
36
|
ariaLabel: string
|
|
33
|
-
|
|
37
|
+
/** The values, or `{ at, value }` points: `at` (a Date, epoch ms or ISO string) names the point in
|
|
38
|
+
* the readout, as a day or, within one day, an hour, in the page's format locale. */
|
|
39
|
+
data: readonly number[] | readonly { at: Date | number | string; value: number }[]
|
|
34
40
|
/** Height in px. */
|
|
35
41
|
height: number
|
|
36
42
|
/** Width in px. Default: the container's, measured by the host. */
|
|
@@ -59,7 +65,8 @@ export type SparklineProps = BasaltProps & {
|
|
|
59
65
|
/** Formats the readout's value. Default: the compact number format. */
|
|
60
66
|
format?: ((value: number) => string) | undefined
|
|
61
67
|
/** The readout's x label per point, index-aligned with `data`: pass the point's day ("12 Oct"),
|
|
62
|
-
* and the readout reads `3 items · 12 Oct` beside the point. Omitted: the
|
|
68
|
+
* and the readout reads `3 items · 12 Oct` beside the point. Omitted: the moment of an `{ at }`
|
|
69
|
+
* point, else the value alone. */
|
|
63
70
|
labels?: readonly string[] | undefined
|
|
64
71
|
}
|
|
65
72
|
|
|
@@ -68,20 +75,54 @@ type Point = { i: number; v: number | null }
|
|
|
68
75
|
const LINE_WIDTH = 1.5
|
|
69
76
|
const DOT_R = 2.5
|
|
70
77
|
const BAR_GAP = 0.15
|
|
78
|
+
/** Air between the hovered point and a bare sparkline's readout chip beside it, px. */
|
|
79
|
+
const READOUT_GAP = 8
|
|
71
80
|
/** Air round a line's plot, px (the engine's plot margin): the end marker stays inside the svg box,
|
|
72
81
|
* so it clears the 2px focus outline round it. A bar plot is flush. */
|
|
73
82
|
const LINE_INSET = 3
|
|
74
83
|
/** The muted tone's line: the same alpha the consumers hand-built (`alpha(VX.muted, .35)`). */
|
|
75
84
|
const MUTED_LINE = alpha(VX.muted, 0.35)
|
|
76
|
-
|
|
85
|
+
/** Air between the plot's start and the reference label, so the focus outline never covers it. */
|
|
86
|
+
const REFERENCE_LABEL_DX = 3
|
|
77
87
|
// Pixel policy of the self-pooling (`poolSparkline`): the densest a series may draw.
|
|
78
88
|
const LINE_PX_PER_POINT = 1
|
|
79
89
|
const BAR_PX_PER_BAR = 2
|
|
80
90
|
/** First, last and one min/max pair: the fewest points a pooled line can hold. */
|
|
81
91
|
const LINE_POOL_MIN = 4
|
|
82
92
|
|
|
93
|
+
const DAY_MS = 86_400_000
|
|
94
|
+
|
|
83
95
|
const finite = (v: number): number | null => (Number.isFinite(v) ? v : null)
|
|
84
96
|
|
|
97
|
+
/** The readout's moment per point of `{ at }` data: the day when the points are a day or more apart,
|
|
98
|
+
* else the hour (with the day once they cross midnight). `undefined` for plain values. */
|
|
99
|
+
function momentsOf(data: SparklineProps['data']): readonly (string | undefined)[] | undefined {
|
|
100
|
+
const dates: (Date | null)[] = []
|
|
101
|
+
for (const d of data) {
|
|
102
|
+
if (typeof d !== 'object' || d === null) return undefined
|
|
103
|
+
dates.push(toDate(d.at))
|
|
104
|
+
}
|
|
105
|
+
const known = dates.filter((d): d is Date => d !== null)
|
|
106
|
+
if (known.length === 0) return undefined
|
|
107
|
+
// By loop: a spread overflows the stack on a long series.
|
|
108
|
+
let lo = Number.POSITIVE_INFINITY
|
|
109
|
+
let hi = Number.NEGATIVE_INFINITY
|
|
110
|
+
for (const d of known) {
|
|
111
|
+
const day = Math.floor(d.getTime() / DAY_MS)
|
|
112
|
+
if (day < lo) lo = day
|
|
113
|
+
if (day > hi) hi = day
|
|
114
|
+
}
|
|
115
|
+
// Hourly points inside one UTC day name the hour alone; a window that crosses midnight (24 points
|
|
116
|
+
// from 12:00 on, or 25 from 12:00 to 12:00) names the day with each hour, so no clock repeats bare.
|
|
117
|
+
const clockOnly = lo === hi
|
|
118
|
+
const byDay = dayGranular(known)
|
|
119
|
+
return dates.map((d) => {
|
|
120
|
+
if (d === null) return undefined
|
|
121
|
+
if (byDay) return readoutDay(bucketDay(d))
|
|
122
|
+
return clockOnly ? readoutClock(d) : `${readoutDay(d)} ${readoutClock(d)}`
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
85
126
|
/** Min and max of the finite values, by loop (a spread overflows the stack on long series). */
|
|
86
127
|
function extent(rows: readonly Point[]): readonly [number, number] | null {
|
|
87
128
|
let lo = Number.POSITIVE_INFINITY
|
|
@@ -217,7 +258,7 @@ function compileSparkline(
|
|
|
217
258
|
referenceLabel: string | undefined,
|
|
218
259
|
): CartesianDefinition {
|
|
219
260
|
const frame = frameOf(extentOf, mark, source.length, emphasisColor !== null)
|
|
220
|
-
const referenceMark = (): ChartMarks =>
|
|
261
|
+
const referenceMark = (height: number): ChartMarks =>
|
|
221
262
|
reference === null
|
|
222
263
|
? []
|
|
223
264
|
: [
|
|
@@ -240,19 +281,29 @@ function compileSparkline(
|
|
|
240
281
|
fill: VX.ink2,
|
|
241
282
|
fontSize: VX.text.micro,
|
|
242
283
|
anchor: 'start',
|
|
243
|
-
|
|
284
|
+
dx: REFERENCE_LABEL_DX,
|
|
285
|
+
dy: referenceLabelPlacement({
|
|
286
|
+
value: reference,
|
|
287
|
+
domain: frame.y,
|
|
288
|
+
height,
|
|
289
|
+
inset: mark === 'bar' ? 0 : LINE_INSET,
|
|
290
|
+
fontSize: VX.text.micro,
|
|
291
|
+
}).dy,
|
|
244
292
|
}),
|
|
245
293
|
),
|
|
246
294
|
]),
|
|
247
295
|
]
|
|
248
296
|
return {
|
|
249
|
-
|
|
297
|
+
// A bar's hover is the bar and the readout chip: no engine point marker (bar chrome only, as
|
|
298
|
+
// `Bars`).
|
|
299
|
+
...(mark === 'bar' && { focusRing: false as const }),
|
|
300
|
+
chart: ({ width, height }) => {
|
|
250
301
|
const rows = poolSparkline(source, width, mark)
|
|
251
302
|
const last = rows.at(-1)
|
|
252
303
|
if (mark === 'bar') {
|
|
253
304
|
return {
|
|
254
305
|
marks: [
|
|
255
|
-
...referenceMark(),
|
|
306
|
+
...referenceMark(height),
|
|
256
307
|
barMark(rows, {
|
|
257
308
|
id: 'bars',
|
|
258
309
|
x: 'i',
|
|
@@ -280,7 +331,7 @@ function compileSparkline(
|
|
|
280
331
|
}
|
|
281
332
|
return {
|
|
282
333
|
marks: [
|
|
283
|
-
...referenceMark(),
|
|
334
|
+
...referenceMark(height),
|
|
284
335
|
lineY(rows, {
|
|
285
336
|
id: 'line',
|
|
286
337
|
x: 'i',
|
|
@@ -338,6 +389,20 @@ function meanOf(rows: readonly Point[]): number {
|
|
|
338
389
|
return sum / n
|
|
339
390
|
}
|
|
340
391
|
|
|
392
|
+
/**
|
|
393
|
+
* A readout never loses its date to an ellipsis: when the one-line chip overflows its box it first
|
|
394
|
+
* tightens (`data-tight`: no air round the dot), then stacks (`data-stacked`: the date on its own
|
|
395
|
+
* line). Set on the node, so a measure never re-renders.
|
|
396
|
+
*/
|
|
397
|
+
function fitReadout(node: HTMLElement): void {
|
|
398
|
+
const overflows = (): boolean => node.scrollWidth > node.clientWidth
|
|
399
|
+
node.removeAttribute('data-tight')
|
|
400
|
+
node.removeAttribute('data-stacked')
|
|
401
|
+
if (!overflows()) return
|
|
402
|
+
node.setAttribute('data-tight', '')
|
|
403
|
+
if (overflows()) node.setAttribute('data-stacked', '')
|
|
404
|
+
}
|
|
405
|
+
|
|
341
406
|
export function Sparkline(props: SparklineProps): ReactElement {
|
|
342
407
|
const {
|
|
343
408
|
ariaLabel,
|
|
@@ -358,12 +423,17 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
358
423
|
style,
|
|
359
424
|
} = props
|
|
360
425
|
const id = useId()
|
|
426
|
+
const hosted = useContext(VisualSlotContext) !== null
|
|
361
427
|
// Two sources, so a pointer leaving a focused chart hands the readout back to the focused point.
|
|
362
428
|
const [hover, setHover] = useState<number | null>(null)
|
|
363
429
|
const [keyed, setKeyed] = useState<number | null>(null)
|
|
364
430
|
const active = hover ?? keyed
|
|
431
|
+
const readoutRef = useRef<HTMLOutputElement>(null)
|
|
365
432
|
const model = useMemo(() => {
|
|
366
|
-
const rows: Point[] = data.map((
|
|
433
|
+
const rows: Point[] = data.map((d, i) => ({
|
|
434
|
+
i,
|
|
435
|
+
v: finite(typeof d === 'object' && d !== null ? d.value : d),
|
|
436
|
+
}))
|
|
367
437
|
const range = extent(rows)
|
|
368
438
|
// No finite value means nothing to scale: the empty render, never an Infinity domain.
|
|
369
439
|
if (range === null) return null
|
|
@@ -375,6 +445,7 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
375
445
|
const end = emphasizeLast ? emphasisColor : null
|
|
376
446
|
return {
|
|
377
447
|
rows,
|
|
448
|
+
moments: momentsOf(data),
|
|
378
449
|
frame: frameOf(full, mark, rows.length, end !== null),
|
|
379
450
|
definition: compileSparkline(rows, full, mark, color, end, ref, referenceLabel),
|
|
380
451
|
}
|
|
@@ -422,7 +493,7 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
422
493
|
}
|
|
423
494
|
|
|
424
495
|
const inset = mark === 'line' ? LINE_INSET : 0
|
|
425
|
-
const layerStyle = { inset, '--basalt-spark-
|
|
496
|
+
const layerStyle: CSSProperties = { inset, ['--basalt-spark-inset' as string]: `${inset}px` }
|
|
426
497
|
const value = active === null ? null : (rows?.[active]?.v ?? null)
|
|
427
498
|
const at =
|
|
428
499
|
interactive && active !== null && value !== null
|
|
@@ -433,12 +504,50 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
433
504
|
? (active + 0.5) / model.rows.length
|
|
434
505
|
: (active - model.frame.x[0]) / (model.frame.x[1] - model.frame.x[0]),
|
|
435
506
|
fy: (model.frame.y[1] - value) / (model.frame.y[1] - model.frame.y[0]),
|
|
436
|
-
label: labels?.[active],
|
|
507
|
+
label: labels?.[active] ?? model.moments?.[active],
|
|
437
508
|
}
|
|
438
509
|
: null
|
|
510
|
+
// Hosted (a host that reserves a line above the plot): the chip slides with the point (`left` and
|
|
511
|
+
// `translate` take one fraction), inside the box at either edge whatever its width, and the
|
|
512
|
+
// stylesheet lifts it above the plot. Bare: beside the point, on the roomier side and on the half of
|
|
513
|
+
// the box away from it, so the chip covers neither the dot nor anything the host draws round it.
|
|
514
|
+
const beside = at === null ? null : at.fx > 0.5 ? 'left' : 'right'
|
|
515
|
+
// The chip's fit depends on its text and (bare) its side, not on the pointer's every tick.
|
|
516
|
+
const shown = at !== null
|
|
517
|
+
useLayoutEffect(() => {
|
|
518
|
+
if (readoutRef.current !== null) fitReadout(readoutRef.current)
|
|
519
|
+
}, [active, at?.label, at?.value, beside])
|
|
520
|
+
// ...and on the box, which a container resize changes without a hover.
|
|
521
|
+
useEffect(() => {
|
|
522
|
+
const layer = readoutRef.current?.parentElement
|
|
523
|
+
if (!shown || !layer || typeof ResizeObserver === 'undefined') return
|
|
524
|
+
const observer = new ResizeObserver(() => {
|
|
525
|
+
if (readoutRef.current !== null) fitReadout(readoutRef.current)
|
|
526
|
+
})
|
|
527
|
+
observer.observe(layer)
|
|
528
|
+
return () => observer.disconnect()
|
|
529
|
+
}, [shown])
|
|
530
|
+
const readoutStyle: CSSProperties | undefined =
|
|
531
|
+
at === null
|
|
532
|
+
? undefined
|
|
533
|
+
: hosted
|
|
534
|
+
? { left: `${at.fx * 100}%`, translate: `${-at.fx * 100}% 0`, maxWidth: '100%' }
|
|
535
|
+
: {
|
|
536
|
+
...(beside === 'right'
|
|
537
|
+
? {
|
|
538
|
+
left: `calc(${at.fx * 100}% + ${READOUT_GAP}px)`,
|
|
539
|
+
maxWidth: `calc(${(1 - at.fx) * 100}% - ${READOUT_GAP}px)`,
|
|
540
|
+
}
|
|
541
|
+
: {
|
|
542
|
+
right: `calc(${(1 - at.fx) * 100}% + ${READOUT_GAP}px)`,
|
|
543
|
+
maxWidth: `calc(${at.fx * 100}% - ${READOUT_GAP}px)`,
|
|
544
|
+
}),
|
|
545
|
+
...(at.fy > 0.5 ? { top: 0 } : { bottom: 0 }),
|
|
546
|
+
}
|
|
439
547
|
return (
|
|
440
548
|
<div
|
|
441
549
|
data-basalt-sparkline=""
|
|
550
|
+
{...(hosted && { 'data-basalt-hosted': '' })}
|
|
442
551
|
role="presentation"
|
|
443
552
|
className={cx(classes.root, className)}
|
|
444
553
|
style={{ width: width ?? '100%', height, minWidth: 0, ...style }}
|
|
@@ -463,21 +572,30 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
463
572
|
)}
|
|
464
573
|
{at !== null && (
|
|
465
574
|
// One layer on the plot rect (inside a line's margin): the dot and the readout take their
|
|
466
|
-
// fractions of the same box, so both land where the mark did.
|
|
575
|
+
// fractions of the same box, so both land where the mark did. A bar has no dot: the bar
|
|
576
|
+
// is the marker.
|
|
467
577
|
<span aria-hidden="true" className={classes.layer} style={layerStyle}>
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
578
|
+
{mark === 'line' && (
|
|
579
|
+
<span
|
|
580
|
+
data-basalt-sparkline-dot=""
|
|
581
|
+
className={classes.dot}
|
|
582
|
+
style={{ left: `${at.fx * 100}%`, top: `${at.fy * 100}%` }}
|
|
583
|
+
/>
|
|
584
|
+
)}
|
|
473
585
|
<output
|
|
586
|
+
ref={readoutRef}
|
|
474
587
|
data-basalt-sparkline-readout=""
|
|
475
588
|
aria-live={hover === null ? 'polite' : 'off'}
|
|
476
589
|
className={classes.readout}
|
|
477
|
-
style={
|
|
590
|
+
style={readoutStyle}
|
|
478
591
|
>
|
|
479
592
|
{format(at.value)}
|
|
480
|
-
{at.label !== undefined &&
|
|
593
|
+
{at.label !== undefined && (
|
|
594
|
+
<span className={classes.label}>
|
|
595
|
+
<span className={classes.sep}> · </span>
|
|
596
|
+
{at.label}
|
|
597
|
+
</span>
|
|
598
|
+
)}
|
|
481
599
|
</output>
|
|
482
600
|
</span>
|
|
483
601
|
)}
|
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
* set is the caller mapping its groups to entries. The y domain is the visible stack total, so a
|
|
5
5
|
* legend toggle shrinks the axis; the tooltip lists the top band first, matching the plot.
|
|
6
6
|
*
|
|
7
|
+
* A band whose every value is `0` is skipped (no band, no legend entry, no tooltip row); `null` is a
|
|
8
|
+
* gap, not a zero, and stays.
|
|
9
|
+
*
|
|
7
10
|
* A stack is only as dense as its sparsest band: a row where ANY visible band is `null` has no
|
|
8
11
|
* total, so every band gaps across it. Reading it as `0` would claim a measured zero.
|
|
9
12
|
*/
|
|
@@ -44,7 +47,15 @@ export type StackedAreaProps<T, X extends ChartX = ChartX> = Omit<
|
|
|
44
47
|
export function StackedArea<T, X extends ChartX = ChartX>(
|
|
45
48
|
props: StackedAreaProps<T, X>,
|
|
46
49
|
): ReactElement {
|
|
47
|
-
const { series:
|
|
50
|
+
const { series: given, fillOpacity, ...rest } = props
|
|
51
|
+
// A band whose every value is 0 draws nothing but a legend entry that names an empty layer: skip it.
|
|
52
|
+
const bands = useMemo(
|
|
53
|
+
() =>
|
|
54
|
+
rest.data.length === 0
|
|
55
|
+
? given
|
|
56
|
+
: given.filter((b) => !rest.data.every((d) => b.getValue(d) === 0)),
|
|
57
|
+
[given, rest.data],
|
|
58
|
+
)
|
|
48
59
|
// `mark: 'bar'` is what the rung sums for the auto domain, and the legend/tooltip swatch.
|
|
49
60
|
const series = useMemo<ChartSeries<T>[]>(
|
|
50
61
|
() =>
|
package/src/charts/band-fold.ts
CHANGED
|
@@ -29,18 +29,37 @@ export type StatusTone = 'good' | 'warn' | 'bad' | 'neutral'
|
|
|
29
29
|
/** `warn` and `bad` carry a glyph by default: a status state is not told apart by hue alone. */
|
|
30
30
|
const TONE_GLYPH: Partial<Record<StatusTone, string>> = { warn: '!', bad: '×' }
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
):
|
|
36
|
-
|
|
32
|
+
type ToneSource = Pick<NamedChartState, 'glyph' | 'tone'> & { color?: string | undefined }
|
|
33
|
+
|
|
34
|
+
/** A state's status tone: its own, else the one its status colour names. */
|
|
35
|
+
export function toneOf(s: ToneSource): StatusTone | undefined {
|
|
36
|
+
return (
|
|
37
37
|
s.tone ?? (s.color === VX.status.warn ? 'warn' : s.color === VX.status.bad ? 'bad' : undefined)
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The glyph a state draws: its own, else its status tone's default, else none. */
|
|
42
|
+
export function glyphOf(s: ToneSource): string | undefined {
|
|
43
|
+
const tone = toneOf(s)
|
|
38
44
|
return s.glyph ?? (tone === undefined ? undefined : TONE_GLYPH[tone])
|
|
39
45
|
}
|
|
40
46
|
|
|
41
|
-
/** A cell narrower than this (px) draws no glyph: it would not fit inside the fill. */
|
|
47
|
+
/** A heat cell narrower than this (px) draws no glyph: it would not fit inside the fill. */
|
|
42
48
|
export const GLYPH_MIN_PX = 12
|
|
43
49
|
|
|
50
|
+
/**
|
|
51
|
+
* A `BandStrip` cell narrower than this (px) draws no glyph: its fill is full-height, so the glyph
|
|
52
|
+
* is set at the xs size (a 16 px line), and a `!` or `×` is unreadable in less. Such a cell draws a
|
|
53
|
+
* notch instead, see {@link NOTCH_PX}. A `Heatmap` keeps {@link GLYPH_MIN_PX}.
|
|
54
|
+
*/
|
|
55
|
+
export const BAND_GLYPH_MIN_PX = 16
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The height (px) of the bottom notch a glyph state draws where the glyph does not fit: a warn
|
|
59
|
+
* notch is shorter than a bad one, so the state is never told by hue alone.
|
|
60
|
+
*/
|
|
61
|
+
export const NOTCH_PX = { warn: 3, bad: 6 } as const
|
|
62
|
+
|
|
44
63
|
/** A run of adjacent slots drawn as one band; `items` are the source data it stands for. */
|
|
45
64
|
export type Group<T> = { start: number; items: T[] }
|
|
46
65
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The chart layer's one date locale: the axis's day labels, the tooltip header and a sparkline's
|
|
3
|
+
* readout all read the page's `defineFormat` / `BasaltProvider` locale through here (the runtime's
|
|
4
|
+
* when none is set), so no card mixes two orders: `04.10` ticks under a `Sat, Oct 10, 2026` header
|
|
5
|
+
* under a `3 Oct` fact. Every read is UTC, like the time scale that placed the ticks.
|
|
6
|
+
*/
|
|
7
|
+
import { dateFormat } from '../format/shared'
|
|
8
|
+
|
|
9
|
+
const utcFormat = (options: Intl.DateTimeFormatOptions, d: Date): string =>
|
|
10
|
+
dateFormat(undefined, { ...options, timeZone: 'UTC' }).format(d)
|
|
11
|
+
|
|
12
|
+
/** An axis tick's day: `4 Oct` (the page locale's own order). */
|
|
13
|
+
export const axisDay = (d: Date): string => utcFormat({ day: 'numeric', month: 'short' }, d)
|
|
14
|
+
|
|
15
|
+
/** {@link axisDay} with a two-digit year. */
|
|
16
|
+
export const axisDayYear = (d: Date): string =>
|
|
17
|
+
utcFormat({ day: 'numeric', month: 'short', year: '2-digit' }, d)
|
|
18
|
+
|
|
19
|
+
export const monthName = (d: Date): string => utcFormat({ month: 'short' }, d)
|
|
20
|
+
|
|
21
|
+
/** The tooltip header's date: weekday, day, month and year. */
|
|
22
|
+
export const headerDay = (d: Date): string =>
|
|
23
|
+
utcFormat({ weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' }, d)
|
|
24
|
+
|
|
25
|
+
/** A day written out for a readout (`12 Oct`): the same format as an axis tick. */
|
|
26
|
+
export const readoutDay = axisDay
|
|
27
|
+
|
|
28
|
+
/** A clock time for a readout, `HH:MM` in the page locale's digits. */
|
|
29
|
+
export const readoutClock = (d: Date): string =>
|
|
30
|
+
utcFormat({ hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }, d)
|
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
@container (min-width: 240px) {
|
|
18
|
+
/* One row: `foldLegend` (legend.tsx) folds what does not fit into `+N`; zones lead the series. */
|
|
18
19
|
.auto {
|
|
20
|
+
flex-wrap: nowrap;
|
|
21
|
+
overflow: hidden;
|
|
19
22
|
display: flex;
|
|
20
23
|
column-gap: var(--bc-dots-gap);
|
|
21
24
|
--vx-hit-gap: var(--bc-dots-gap);
|
|
@@ -24,6 +27,8 @@
|
|
|
24
27
|
|
|
25
28
|
@container (min-width: 480px) {
|
|
26
29
|
.auto {
|
|
30
|
+
flex-wrap: wrap;
|
|
31
|
+
overflow: visible;
|
|
27
32
|
column-gap: var(--bc-legend-gap);
|
|
28
33
|
--vx-hit-gap: var(--bc-legend-gap);
|
|
29
34
|
}
|
|
@@ -43,3 +48,14 @@
|
|
|
43
48
|
display: none;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
51
|
+
|
|
52
|
+
/* The compact fold (`foldLegend`, which only folds while the row is `nowrap`); wide keeps every entry.
|
|
53
|
+
* `!important`: an entry's inline `all: unset` (legend.tsx) would otherwise reset `display`. */
|
|
54
|
+
.auto [data-legend-folded] {
|
|
55
|
+
display: none !important;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.auto [data-legend-more] {
|
|
59
|
+
order: 1;
|
|
60
|
+
flex: none;
|
|
61
|
+
}
|