basalt-ui 2.21.0 → 2.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/CHANGELOG.md +60 -0
- package/MIGRATING.md +452 -12
- package/agent/agents/basalt-design-reviewer.md +1 -1
- package/agent/rules/basalt-tokens.md +1 -1
- package/agent/skills/basalt-design/SKILL.md +1 -1
- package/agent/skills/basalt-design/references/anti-defaults.md +1 -1
- package/agent/skills/basalt-design/references/catalogue.md +1 -0
- package/dist/charts/BandStrip.d.ts.map +1 -1
- package/dist/charts/BandStrip.js +73 -3
- package/dist/charts/Bars.d.ts.map +1 -1
- package/dist/charts/Bars.js +6 -0
- package/dist/charts/CartesianChart.d.ts +10 -0
- package/dist/charts/CartesianChart.d.ts.map +1 -1
- package/dist/charts/CartesianChart.js +8 -5
- package/dist/charts/ChartCard.d.ts +12 -3
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +39 -8
- package/dist/charts/DualPanel.d.ts.map +1 -1
- package/dist/charts/DualPanel.js +5 -8
- package/dist/charts/Heatmap.d.ts +12 -1
- package/dist/charts/Heatmap.d.ts.map +1 -1
- package/dist/charts/Heatmap.js +34 -5
- package/dist/charts/Lines.d.ts +3 -0
- package/dist/charts/Lines.d.ts.map +1 -1
- package/dist/charts/Lines.js +29 -6
- package/dist/charts/MirroredBars.d.ts.map +1 -1
- package/dist/charts/MirroredBars.js +4 -6
- package/dist/charts/Sparkline.d.ts +14 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +101 -22
- package/dist/charts/StackedArea.d.ts +4 -1
- package/dist/charts/StackedArea.d.ts.map +1 -1
- package/dist/charts/StackedArea.js +9 -3
- package/dist/charts/band-fold.d.ts +38 -1
- package/dist/charts/band-fold.d.ts.map +1 -1
- package/dist/charts/band-fold.js +18 -1
- package/dist/charts/chart-box.module.css +19 -0
- package/dist/charts/chart-card.module.css +25 -0
- 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 +7 -0
- package/dist/charts/compile.d.ts +25 -6
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +152 -72
- 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 +21 -6
- package/dist/charts/labels.d.ts.map +1 -1
- package/dist/charts/labels.js +52 -17
- package/dist/charts/legend.d.ts +2 -0
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +7 -21
- package/dist/charts/marks.js +1 -1
- package/dist/charts/panel.d.ts +0 -6
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +1 -10
- package/dist/charts/ref-label.d.ts +43 -0
- package/dist/charts/ref-label.d.ts.map +1 -0
- package/dist/charts/ref-label.js +75 -0
- package/dist/charts/scales.d.ts +4 -1
- package/dist/charts/scales.d.ts.map +1 -1
- package/dist/charts/scales.js +14 -3
- package/dist/charts/sparkline.module.css +13 -17
- package/dist/charts/sparkline.module.css.d.ts +1 -2
- package/dist/charts/state.d.ts +2 -0
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +5 -2
- package/dist/charts/types.d.ts +18 -8
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/cli/doctor-upgrade.d.ts +1 -0
- package/dist/cli/doctor-upgrade.d.ts.map +1 -1
- package/dist/cli/doctor-upgrade.js +1 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +68 -4
- package/dist/cli/shot/capture.d.ts +21 -2
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +202 -36
- package/dist/cli/shot/detect.d.ts.map +1 -1
- package/dist/cli/shot/detect.js +17 -1
- package/dist/cli/shot/playwright.d.ts +13 -2
- 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 +4 -0
- package/dist/cli/shot/types.d.ts.map +1 -1
- package/dist/cli/shot/types.js +2 -0
- 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/controls/controls.module.css +11 -1
- package/dist/controls/filter-pill.d.ts.map +1 -1
- package/dist/controls/filter-pill.js +1 -0
- package/dist/controls/sync-button.d.ts.map +1 -1
- package/dist/controls/sync-button.js +22 -3
- package/dist/controls/sync-button.module.css +34 -2
- package/dist/controls/sync-button.module.css.d.ts +1 -0
- 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 +15 -8
- package/dist/dashboard/bar-list.module.css +83 -0
- package/dist/dashboard/bar-list.module.css.d.ts +1 -0
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +2 -2
- package/dist/dashboard/index.d.ts +1 -0
- package/dist/dashboard/index.d.ts.map +1 -1
- package/dist/dashboard/index.js +2 -0
- package/dist/dashboard/meter-model.d.ts +84 -5
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +100 -15
- package/dist/dashboard/meter-parts.d.ts +9 -1
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +276 -74
- package/dist/dashboard/meter.d.ts +54 -65
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +21 -4
- package/dist/dashboard/meter.module.css +76 -0
- package/dist/dashboard/meter.module.css.d.ts +6 -0
- package/dist/dashboard/page-summary.d.ts +20 -0
- package/dist/dashboard/page-summary.d.ts.map +1 -0
- package/dist/dashboard/page-summary.js +28 -0
- package/dist/dashboard/page-summary.module.css +89 -0
- package/dist/dashboard/page-summary.module.css.d.ts +15 -0
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +1 -1
- package/dist/dashboard/section.d.ts +25 -2
- package/dist/dashboard/section.d.ts.map +1 -1
- package/dist/dashboard/section.js +10 -5
- package/dist/dashboard/section.module.css +23 -0
- package/dist/dashboard/section.module.css.d.ts +1 -0
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
- package/dist/dashboard/stat-card-parts.js +1 -1
- package/dist/dashboard/stat-card.d.ts +4 -2
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +7 -5
- package/dist/dashboard/stat-card.module.css +36 -0
- package/dist/dashboard/stat-group.module.css +7 -0
- package/dist/dashboard/stat-strip.d.ts +22 -6
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +104 -60
- package/dist/dashboard/stat-strip.module.css +122 -53
- package/dist/dashboard/stat-strip.module.css.d.ts +4 -0
- package/dist/dashboard/visual-slot.d.ts +7 -1
- package/dist/dashboard/visual-slot.d.ts.map +1 -1
- package/dist/dashboard/visual-slot.js +3 -1
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +0 -5
- package/dist/guard/vx-names.d.ts.map +1 -1
- package/dist/guard/vx-names.js +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/styles.css +43 -4
- package/dist/theme/chrome-vars.d.ts +20 -5
- package/dist/theme/chrome-vars.d.ts.map +1 -1
- package/dist/theme/chrome-vars.js +46 -3
- package/dist/theme/controls.module.css +37 -0
- package/dist/theme/controls.module.css.d.ts +2 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +18 -27
- package/dist/theme/status-verdict.d.ts.map +1 -1
- package/dist/theme/status-verdict.js +9 -1
- package/dist/tokens/core.d.ts +2 -1
- package/dist/tokens/core.d.ts.map +1 -1
- package/dist/tokens/core.js +6 -1
- package/dist/tokens/derive.d.ts +3 -1
- package/dist/tokens/derive.d.ts.map +1 -1
- package/dist/tokens/derive.js +16 -1
- package/dist/tokens/palette.d.ts +11 -10
- package/dist/tokens/palette.d.ts.map +1 -1
- package/dist/tokens/palette.js +11 -10
- package/dist/tokens/status-text.d.ts.map +1 -1
- package/dist/tokens/status-text.js +3 -1
- package/llms.txt +42 -22
- package/package.json +1 -1
- package/src/charts/BandStrip.tsx +83 -3
- package/src/charts/Bars.tsx +7 -0
- package/src/charts/CartesianChart.tsx +18 -3
- package/src/charts/ChartCard.tsx +54 -13
- package/src/charts/DualPanel.tsx +3 -6
- package/src/charts/Heatmap.tsx +54 -5
- package/src/charts/Lines.tsx +56 -10
- package/src/charts/MirroredBars.tsx +3 -5
- package/src/charts/Sparkline.tsx +135 -25
- package/src/charts/StackedArea.tsx +14 -4
- package/src/charts/band-fold.ts +50 -1
- package/src/charts/chart-box.module.css +19 -0
- package/src/charts/chart-card.module.css +25 -0
- package/src/charts/chart-dates.ts +30 -0
- package/src/charts/chart-legend.module.css +7 -0
- package/src/charts/compile.ts +253 -87
- package/src/charts/date-tick-format.ts +10 -13
- package/src/charts/labels.ts +81 -26
- package/src/charts/legend.tsx +10 -20
- package/src/charts/marks.ts +1 -1
- package/src/charts/panel.tsx +1 -16
- package/src/charts/ref-label.ts +131 -0
- package/src/charts/scales.ts +22 -2
- package/src/charts/sparkline.module.css +13 -17
- package/src/charts/sparkline.module.css.d.ts +1 -2
- package/src/charts/state.tsx +9 -4
- package/src/charts/types.ts +18 -8
- package/src/cli/doctor-upgrade.ts +1 -1
- package/src/cli/init.ts +100 -3
- package/src/cli/shot/capture.ts +266 -35
- package/src/cli/shot/detect.ts +18 -1
- package/src/cli/shot/playwright.ts +20 -4
- package/src/cli/shot/types.ts +5 -0
- package/src/cli/tokens-css.ts +18 -20
- package/src/controls/controls.module.css +11 -1
- package/src/controls/filter-pill.tsx +2 -0
- package/src/controls/sync-button.module.css +34 -2
- package/src/controls/sync-button.module.css.d.ts +1 -0
- package/src/controls/sync-button.tsx +40 -4
- package/src/dashboard/bar-list.module.css +83 -0
- package/src/dashboard/bar-list.module.css.d.ts +1 -0
- package/src/dashboard/bar-list.tsx +107 -69
- package/src/dashboard/delta-badge.tsx +6 -2
- package/src/dashboard/index.ts +1 -0
- package/src/dashboard/meter-model.ts +207 -24
- package/src/dashboard/meter-parts.tsx +349 -67
- package/src/dashboard/meter.module.css +76 -0
- package/src/dashboard/meter.module.css.d.ts +6 -0
- package/src/dashboard/meter.tsx +72 -18
- package/src/dashboard/page-summary.module.css +89 -0
- package/src/dashboard/page-summary.module.css.d.ts +15 -0
- package/src/dashboard/page-summary.tsx +95 -0
- package/src/dashboard/query-state.tsx +1 -1
- package/src/dashboard/section.module.css +23 -0
- package/src/dashboard/section.module.css.d.ts +1 -0
- package/src/dashboard/section.tsx +59 -11
- package/src/dashboard/stat-card-parts.tsx +5 -1
- package/src/dashboard/stat-card.module.css +36 -0
- package/src/dashboard/stat-card.tsx +12 -6
- package/src/dashboard/stat-group.module.css +7 -0
- package/src/dashboard/stat-strip.module.css +122 -53
- package/src/dashboard/stat-strip.module.css.d.ts +4 -0
- package/src/dashboard/stat-strip.tsx +176 -91
- package/src/dashboard/visual-slot.ts +12 -3
- package/src/guard/index.ts +0 -5
- package/src/guard/vx-names.ts +2 -0
- package/src/index.ts +2 -0
- package/src/styles.css +43 -4
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +200 -68
- package/src/theme/chrome-vars.ts +77 -8
- package/src/theme/controls.module.css +37 -0
- package/src/theme/controls.module.css.d.ts +2 -0
- package/src/theme/index.ts +22 -32
- package/src/theme/status-verdict.tsx +5 -1
- package/src/tokens/core.ts +4 -1
- package/src/tokens/derive.ts +32 -3
- package/src/tokens/palette.ts +11 -10
- package/src/tokens/status-text.ts +2 -0
package/src/charts/Sparkline.tsx
CHANGED
|
@@ -11,25 +11,31 @@ import { lineY } from '@tanstack/charts/line'
|
|
|
11
11
|
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
|
+
import { text } from '@tanstack/charts/text'
|
|
14
15
|
import { useId, useMemo, useState } from 'react'
|
|
15
|
-
import type { FocusEvent, KeyboardEvent, PointerEvent, ReactElement } from 'react'
|
|
16
|
+
import type { CSSProperties, FocusEvent, KeyboardEvent, PointerEvent, ReactElement } from 'react'
|
|
16
17
|
import { cx } from '../common/props'
|
|
17
18
|
import type { BasaltProps } from '../common/props'
|
|
18
|
-
import { VX } from '../tokens'
|
|
19
|
+
import { VX, alpha } from '../tokens'
|
|
19
20
|
import type { CartesianDefinition } from './compile'
|
|
20
21
|
import { chartCurve } from './curves'
|
|
21
22
|
import { barMark } from './marks'
|
|
22
|
-
import { defaultNumberFormat } from './compile'
|
|
23
|
+
import { bucketDay, dayGranular, defaultNumberFormat } from './compile'
|
|
24
|
+
import { referenceLabelPlacement } from './ref-label'
|
|
25
|
+
import { readoutClock, readoutDay } from './chart-dates'
|
|
26
|
+
import { toDate } from '../format/shared'
|
|
23
27
|
import { ChartEngine, idPrefixOf } from './host'
|
|
24
28
|
import classes from './sparkline.module.css'
|
|
25
29
|
import { ChartPlaceholder, resolveChartState } from './state'
|
|
26
30
|
import { CHART_THEME, DASH } from './theme'
|
|
27
|
-
import type { ChartState } from './types'
|
|
31
|
+
import type { ChartMarks, ChartState } from './types'
|
|
28
32
|
|
|
29
33
|
export type SparklineProps = BasaltProps & {
|
|
30
34
|
/** The accessible name. Required: a chart without one is an unlabeled image. */
|
|
31
35
|
ariaLabel: string
|
|
32
|
-
|
|
36
|
+
/** The values, or `{ at, value }` points: `at` (a Date, epoch ms or ISO string) names the point in
|
|
37
|
+
* the readout, as a day or, within one day, an hour, in the page's format locale. */
|
|
38
|
+
data: readonly number[] | readonly { at: Date | number | string; value: number }[]
|
|
33
39
|
/** Height in px. */
|
|
34
40
|
height: number
|
|
35
41
|
/** Width in px. Default: the container's, measured by the host. */
|
|
@@ -38,6 +44,8 @@ export type SparklineProps = BasaltProps & {
|
|
|
38
44
|
mark?: 'line' | 'bar' | undefined
|
|
39
45
|
/** A `VX.*` ref or a series token. Default `VX.faint`. */
|
|
40
46
|
color?: string | undefined
|
|
47
|
+
/** `'muted'`: the quiet trend line (a muted ink at low alpha). An explicit `color` wins. */
|
|
48
|
+
tone?: 'muted' | undefined
|
|
41
49
|
/** Paints the last point in `emphasisColor` — the one reading a trend beside a KPI is for.
|
|
42
50
|
* Default `true`; pass `false` for a bare line. */
|
|
43
51
|
emphasizeLast?: boolean | undefined
|
|
@@ -50,9 +58,14 @@ export type SparklineProps = BasaltProps & {
|
|
|
50
58
|
/** A quiet dashed hairline at a value, or at the mean of the plotted values (`'mean'`). The y
|
|
51
59
|
* domain widens to include it. Not a legend item, never a status colour. */
|
|
52
60
|
reference?: number | 'mean' | undefined
|
|
61
|
+
/** A short caption for the `reference` line (`PR`, `30 d mean`), drawn at the plot start just
|
|
62
|
+
* above the line. */
|
|
63
|
+
referenceLabel?: string | undefined
|
|
53
64
|
/** Formats the readout's value. Default: the compact number format. */
|
|
54
65
|
format?: ((value: number) => string) | undefined
|
|
55
|
-
/** The readout's x label per point, index-aligned with `data
|
|
66
|
+
/** The readout's x label per point, index-aligned with `data`: pass the point's day ("12 Oct"),
|
|
67
|
+
* and the readout reads `3 items · 12 Oct` beside the point. Omitted: the moment of an `{ at }`
|
|
68
|
+
* point, else the value alone. */
|
|
56
69
|
labels?: readonly string[] | undefined
|
|
57
70
|
}
|
|
58
71
|
|
|
@@ -61,14 +74,52 @@ type Point = { i: number; v: number | null }
|
|
|
61
74
|
const LINE_WIDTH = 1.5
|
|
62
75
|
const DOT_R = 2.5
|
|
63
76
|
const BAR_GAP = 0.15
|
|
77
|
+
/** Air between the hovered point and the readout chip beside it, px. */
|
|
78
|
+
const READOUT_GAP = 8
|
|
79
|
+
/** Air round a line's plot, px (the engine's plot margin): the end marker stays inside the svg box,
|
|
80
|
+
* so it clears the 2px focus outline round it. A bar plot is flush. */
|
|
81
|
+
const LINE_INSET = 3
|
|
82
|
+
/** The muted tone's line: the same alpha the consumers hand-built (`alpha(VX.muted, .35)`). */
|
|
83
|
+
const MUTED_LINE = alpha(VX.muted, 0.35)
|
|
84
|
+
/** Air between the plot's start and the reference label, so the focus outline never covers it. */
|
|
85
|
+
const REFERENCE_LABEL_DX = 3
|
|
64
86
|
// Pixel policy of the self-pooling (`poolSparkline`): the densest a series may draw.
|
|
65
87
|
const LINE_PX_PER_POINT = 1
|
|
66
88
|
const BAR_PX_PER_BAR = 2
|
|
67
89
|
/** First, last and one min/max pair: the fewest points a pooled line can hold. */
|
|
68
90
|
const LINE_POOL_MIN = 4
|
|
69
91
|
|
|
92
|
+
const DAY_MS = 86_400_000
|
|
93
|
+
|
|
70
94
|
const finite = (v: number): number | null => (Number.isFinite(v) ? v : null)
|
|
71
95
|
|
|
96
|
+
/** The readout's moment per point of `{ at }` data: the day when the points are a day or more apart,
|
|
97
|
+
* else the hour (with the day once they span several). `undefined` for plain values. */
|
|
98
|
+
function momentsOf(data: SparklineProps['data']): readonly (string | undefined)[] | undefined {
|
|
99
|
+
const dates: (Date | null)[] = []
|
|
100
|
+
for (const d of data) {
|
|
101
|
+
if (typeof d !== 'object' || d === null) return undefined
|
|
102
|
+
dates.push(toDate(d.at))
|
|
103
|
+
}
|
|
104
|
+
const known = dates.filter((d): d is Date => d !== null)
|
|
105
|
+
if (known.length === 0) return undefined
|
|
106
|
+
// By loop: a spread overflows the stack on a long series.
|
|
107
|
+
let lo = Number.POSITIVE_INFINITY
|
|
108
|
+
let hi = Number.NEGATIVE_INFINITY
|
|
109
|
+
for (const d of known) {
|
|
110
|
+
const day = Math.floor(d.getTime() / DAY_MS)
|
|
111
|
+
if (day < lo) lo = day
|
|
112
|
+
if (day > hi) hi = day
|
|
113
|
+
}
|
|
114
|
+
const oneDay = lo === hi
|
|
115
|
+
const byDay = dayGranular(known)
|
|
116
|
+
return dates.map((d) => {
|
|
117
|
+
if (d === null) return undefined
|
|
118
|
+
if (byDay) return readoutDay(bucketDay(d))
|
|
119
|
+
return oneDay ? readoutClock(d) : `${readoutDay(d)} ${readoutClock(d)}`
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
72
123
|
/** Min and max of the finite values, by loop (a spread overflows the stack on long series). */
|
|
73
124
|
function extent(rows: readonly Point[]): readonly [number, number] | null {
|
|
74
125
|
let lo = Number.POSITIVE_INFINITY
|
|
@@ -201,9 +252,10 @@ function compileSparkline(
|
|
|
201
252
|
color: string,
|
|
202
253
|
emphasisColor: string | null,
|
|
203
254
|
reference: number | null,
|
|
255
|
+
referenceLabel: string | undefined,
|
|
204
256
|
): CartesianDefinition {
|
|
205
257
|
const frame = frameOf(extentOf, mark, source.length, emphasisColor !== null)
|
|
206
|
-
const referenceMark = ():
|
|
258
|
+
const referenceMark = (height: number): ChartMarks =>
|
|
207
259
|
reference === null
|
|
208
260
|
? []
|
|
209
261
|
: [
|
|
@@ -214,15 +266,41 @@ function compileSparkline(
|
|
|
214
266
|
strokeWidth: 1,
|
|
215
267
|
strokeDasharray: DASH,
|
|
216
268
|
}),
|
|
269
|
+
...(referenceLabel === undefined
|
|
270
|
+
? []
|
|
271
|
+
: [
|
|
272
|
+
decorative(
|
|
273
|
+
text([reference], {
|
|
274
|
+
id: 'reference-label',
|
|
275
|
+
x: () => (mark === 'bar' ? 0 : frame.x[0]),
|
|
276
|
+
y: (v: number) => v,
|
|
277
|
+
text: () => referenceLabel,
|
|
278
|
+
fill: VX.ink2,
|
|
279
|
+
fontSize: VX.text.micro,
|
|
280
|
+
anchor: 'start',
|
|
281
|
+
dx: REFERENCE_LABEL_DX,
|
|
282
|
+
dy: referenceLabelPlacement({
|
|
283
|
+
value: reference,
|
|
284
|
+
domain: frame.y,
|
|
285
|
+
height,
|
|
286
|
+
inset: mark === 'bar' ? 0 : LINE_INSET,
|
|
287
|
+
fontSize: VX.text.micro,
|
|
288
|
+
}).dy,
|
|
289
|
+
}),
|
|
290
|
+
),
|
|
291
|
+
]),
|
|
217
292
|
]
|
|
218
293
|
return {
|
|
219
|
-
|
|
294
|
+
// A bar's hover is the bar and the readout chip: no engine point marker (bar chrome only, as
|
|
295
|
+
// `Bars`).
|
|
296
|
+
...(mark === 'bar' && { focusRing: false as const }),
|
|
297
|
+
chart: ({ width, height }) => {
|
|
220
298
|
const rows = poolSparkline(source, width, mark)
|
|
221
299
|
const last = rows.at(-1)
|
|
222
300
|
if (mark === 'bar') {
|
|
223
301
|
return {
|
|
224
302
|
marks: [
|
|
225
|
-
...referenceMark(),
|
|
303
|
+
...referenceMark(height),
|
|
226
304
|
barMark(rows, {
|
|
227
305
|
id: 'bars',
|
|
228
306
|
x: 'i',
|
|
@@ -250,7 +328,7 @@ function compileSparkline(
|
|
|
250
328
|
}
|
|
251
329
|
return {
|
|
252
330
|
marks: [
|
|
253
|
-
...referenceMark(),
|
|
331
|
+
...referenceMark(height),
|
|
254
332
|
lineY(rows, {
|
|
255
333
|
id: 'line',
|
|
256
334
|
x: 'i',
|
|
@@ -276,7 +354,7 @@ function compileSparkline(
|
|
|
276
354
|
},
|
|
277
355
|
theme: CHART_THEME,
|
|
278
356
|
guides: false,
|
|
279
|
-
margin:
|
|
357
|
+
margin: LINE_INSET,
|
|
280
358
|
}
|
|
281
359
|
},
|
|
282
360
|
}
|
|
@@ -315,11 +393,13 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
315
393
|
height,
|
|
316
394
|
width,
|
|
317
395
|
mark = 'line',
|
|
318
|
-
|
|
396
|
+
tone,
|
|
397
|
+
color = tone === 'muted' ? MUTED_LINE : VX.faint,
|
|
319
398
|
emphasizeLast = true,
|
|
320
399
|
emphasisColor = VX.line,
|
|
321
400
|
state,
|
|
322
401
|
reference,
|
|
402
|
+
referenceLabel,
|
|
323
403
|
format = defaultNumberFormat,
|
|
324
404
|
labels,
|
|
325
405
|
className,
|
|
@@ -331,7 +411,10 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
331
411
|
const [keyed, setKeyed] = useState<number | null>(null)
|
|
332
412
|
const active = hover ?? keyed
|
|
333
413
|
const model = useMemo(() => {
|
|
334
|
-
const rows: Point[] = data.map((
|
|
414
|
+
const rows: Point[] = data.map((d, i) => ({
|
|
415
|
+
i,
|
|
416
|
+
v: finite(typeof d === 'object' && d !== null ? d.value : d),
|
|
417
|
+
}))
|
|
335
418
|
const range = extent(rows)
|
|
336
419
|
// No finite value means nothing to scale: the empty render, never an Infinity domain.
|
|
337
420
|
if (range === null) return null
|
|
@@ -343,10 +426,11 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
343
426
|
const end = emphasizeLast ? emphasisColor : null
|
|
344
427
|
return {
|
|
345
428
|
rows,
|
|
429
|
+
moments: momentsOf(data),
|
|
346
430
|
frame: frameOf(full, mark, rows.length, end !== null),
|
|
347
|
-
definition: compileSparkline(rows, full, mark, color, end, ref),
|
|
431
|
+
definition: compileSparkline(rows, full, mark, color, end, ref, referenceLabel),
|
|
348
432
|
}
|
|
349
|
-
}, [data, mark, color, emphasizeLast, emphasisColor, reference])
|
|
433
|
+
}, [data, mark, color, emphasizeLast, emphasisColor, reference, referenceLabel])
|
|
350
434
|
const resolved = resolveChartState(state)
|
|
351
435
|
const rows = model?.rows
|
|
352
436
|
const interactive = resolved === null && model !== null
|
|
@@ -389,6 +473,8 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
389
473
|
setKeyed(lastFinite(rows))
|
|
390
474
|
}
|
|
391
475
|
|
|
476
|
+
const inset = mark === 'line' ? LINE_INSET : 0
|
|
477
|
+
const layerStyle: CSSProperties = { inset }
|
|
392
478
|
const value = active === null ? null : (rows?.[active]?.v ?? null)
|
|
393
479
|
const at =
|
|
394
480
|
interactive && active !== null && value !== null
|
|
@@ -399,9 +485,27 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
399
485
|
? (active + 0.5) / model.rows.length
|
|
400
486
|
: (active - model.frame.x[0]) / (model.frame.x[1] - model.frame.x[0]),
|
|
401
487
|
fy: (model.frame.y[1] - value) / (model.frame.y[1] - model.frame.y[0]),
|
|
402
|
-
label: labels?.[active],
|
|
488
|
+
label: labels?.[active] ?? model.moments?.[active],
|
|
403
489
|
}
|
|
404
490
|
: null
|
|
491
|
+
// Beside the point, on the roomier side, and on the half of the box away from it: the chip never
|
|
492
|
+
// covers the dot or the host's text, and stays inside the box whatever clips above it.
|
|
493
|
+
const beside = at === null ? null : at.fx > 0.5 ? 'left' : 'right'
|
|
494
|
+
const readoutStyle: CSSProperties | undefined =
|
|
495
|
+
at === null
|
|
496
|
+
? undefined
|
|
497
|
+
: {
|
|
498
|
+
...(beside === 'right'
|
|
499
|
+
? {
|
|
500
|
+
left: `calc(${at.fx * 100}% + ${READOUT_GAP}px)`,
|
|
501
|
+
maxWidth: `calc(${(1 - at.fx) * 100}% - ${READOUT_GAP}px)`,
|
|
502
|
+
}
|
|
503
|
+
: {
|
|
504
|
+
right: `calc(${(1 - at.fx) * 100}% + ${READOUT_GAP}px)`,
|
|
505
|
+
maxWidth: `calc(${at.fx * 100}% - ${READOUT_GAP}px)`,
|
|
506
|
+
}),
|
|
507
|
+
...(at.fy > 0.5 ? { top: 0 } : { bottom: 0 }),
|
|
508
|
+
}
|
|
405
509
|
return (
|
|
406
510
|
<div
|
|
407
511
|
data-basalt-sparkline=""
|
|
@@ -428,21 +532,27 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
428
532
|
/>
|
|
429
533
|
)}
|
|
430
534
|
{at !== null && (
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
535
|
+
// One layer on the plot rect (inside a line's margin): the dot and the readout take their
|
|
536
|
+
// fractions of the same box, so both land where the mark did. A bar has no dot: the bar
|
|
537
|
+
// is the marker.
|
|
538
|
+
<span aria-hidden="true" className={classes.layer} style={layerStyle}>
|
|
539
|
+
{mark === 'line' && (
|
|
540
|
+
<span
|
|
541
|
+
data-basalt-sparkline-dot=""
|
|
542
|
+
className={classes.dot}
|
|
543
|
+
style={{ left: `${at.fx * 100}%`, top: `${at.fy * 100}%` }}
|
|
544
|
+
/>
|
|
545
|
+
)}
|
|
437
546
|
<output
|
|
438
547
|
data-basalt-sparkline-readout=""
|
|
439
548
|
aria-live={hover === null ? 'polite' : 'off'}
|
|
440
|
-
className={
|
|
549
|
+
className={classes.readout}
|
|
550
|
+
style={readoutStyle}
|
|
441
551
|
>
|
|
442
|
-
{at.label !== undefined && <span className={classes.label}>{at.label} </span>}
|
|
443
552
|
{format(at.value)}
|
|
553
|
+
{at.label !== undefined && <span className={classes.label}> · {at.label}</span>}
|
|
444
554
|
</output>
|
|
445
|
-
|
|
555
|
+
</span>
|
|
446
556
|
)}
|
|
447
557
|
</div>
|
|
448
558
|
)
|
|
@@ -24,7 +24,7 @@ export type StackedAreaBand<T> = Pick<
|
|
|
24
24
|
ChartSeries<T>,
|
|
25
25
|
'key' | 'label' | 'color' | 'getValue' | 'formatValue' | 'tooltip' | 'legend' | 'role' | 'note'
|
|
26
26
|
> & {
|
|
27
|
-
/** Default 0.85. */
|
|
27
|
+
/** Default: the chart's `fillOpacity` (0.85). */
|
|
28
28
|
fillOpacity?: number | undefined
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -34,6 +34,9 @@ export type StackedAreaProps<T, X extends ChartX = ChartX> = Omit<
|
|
|
34
34
|
> & {
|
|
35
35
|
/** The bands, bottom to top. */
|
|
36
36
|
series: readonly StackedAreaBand<T>[]
|
|
37
|
+
/** Every band's fill opacity, 0–1 (default 0.85: full-opacity bands read saturated). A band's own
|
|
38
|
+
* `fillOpacity` wins. The legend swatch wears the same shade. */
|
|
39
|
+
fillOpacity?: number | undefined
|
|
37
40
|
/** No `scale`: a log axis has no additive zero to stack from. */
|
|
38
41
|
y?: Omit<NonNullable<CartesianChartProps<T, X>['y']>, 'scale'> | undefined
|
|
39
42
|
}
|
|
@@ -41,11 +44,18 @@ export type StackedAreaProps<T, X extends ChartX = ChartX> = Omit<
|
|
|
41
44
|
export function StackedArea<T, X extends ChartX = ChartX>(
|
|
42
45
|
props: StackedAreaProps<T, X>,
|
|
43
46
|
): ReactElement {
|
|
44
|
-
const { series: bands, ...rest } = props
|
|
47
|
+
const { series: bands, fillOpacity, ...rest } = props
|
|
45
48
|
// `mark: 'bar'` is what the rung sums for the auto domain, and the legend/tooltip swatch.
|
|
46
49
|
const series = useMemo<ChartSeries<T>[]>(
|
|
47
|
-
() =>
|
|
48
|
-
|
|
50
|
+
() =>
|
|
51
|
+
bands.map((s) => ({
|
|
52
|
+
...s,
|
|
53
|
+
mark: 'bar' as const,
|
|
54
|
+
...((s.fillOpacity ?? fillOpacity) !== undefined && {
|
|
55
|
+
fillOpacity: s.fillOpacity ?? fillOpacity,
|
|
56
|
+
}),
|
|
57
|
+
})),
|
|
58
|
+
[bands, fillOpacity],
|
|
49
59
|
)
|
|
50
60
|
|
|
51
61
|
const marks = useCallback((plot: PlotContext<T, X>): ChartMarks => {
|
package/src/charts/band-fold.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { oneOf } from '../common/errors'
|
|
2
|
+
import { VX } from '../tokens'
|
|
2
3
|
import type { ChartSeries } from './types'
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -9,7 +10,55 @@ import type { ChartSeries } from './types'
|
|
|
9
10
|
export type NamedChartState = Pick<
|
|
10
11
|
ChartSeries<never>,
|
|
11
12
|
'key' | 'label' | 'color' | 'tooltip' | 'legend' | 'note' | 'fillOpacity'
|
|
12
|
-
>
|
|
13
|
+
> & {
|
|
14
|
+
/**
|
|
15
|
+
* A short mark (`!`, `×`) drawn in the middle of each cell in this state, so a state is not told
|
|
16
|
+
* apart by hue alone; `''` opts out of the default. Default: `!` for a `warn` state and `×` for a
|
|
17
|
+
* `bad` one (by `tone`, or by a `color` that is `VX.status.warn` / `VX.status.bad`); none
|
|
18
|
+
* otherwise. Skipped on a cell too narrow to hold it.
|
|
19
|
+
*/
|
|
20
|
+
glyph?: string | undefined
|
|
21
|
+
/** The status this state means. `Heatmap` also fills from it; on `BandStrip` it only picks the
|
|
22
|
+
* default `glyph` (the fill is `color`). */
|
|
23
|
+
tone?: StatusTone | undefined
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The status inks a state can name: `VX.status.*`. */
|
|
27
|
+
export type StatusTone = 'good' | 'warn' | 'bad' | 'neutral'
|
|
28
|
+
|
|
29
|
+
/** `warn` and `bad` carry a glyph by default: a status state is not told apart by hue alone. */
|
|
30
|
+
const TONE_GLYPH: Partial<Record<StatusTone, string>> = { warn: '!', bad: '×' }
|
|
31
|
+
|
|
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
|
+
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)
|
|
44
|
+
return s.glyph ?? (tone === undefined ? undefined : TONE_GLYPH[tone])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A heat cell narrower than this (px) draws no glyph: it would not fit inside the fill. */
|
|
48
|
+
export const GLYPH_MIN_PX = 12
|
|
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
|
|
13
62
|
|
|
14
63
|
/** A run of adjacent slots drawn as one band; `items` are the source data it stands for. */
|
|
15
64
|
export type Group<T> = { start: number; items: T[] }
|
|
@@ -92,3 +92,22 @@
|
|
|
92
92
|
height: auto;
|
|
93
93
|
min-height: inherit;
|
|
94
94
|
}
|
|
95
|
+
|
|
96
|
+
/*
|
|
97
|
+
* The engine paints its tooltip rows as a grid of `.55rem | minmax(0, 1fr) | auto` with inline
|
|
98
|
+
* styles and `overflow-wrap: anywhere` on the card, so a narrow tooltip (a 390 viewport, a pointer
|
|
99
|
+
* near an edge) squeezes the name column to one character per line. The name keeps a floor and
|
|
100
|
+
* never wraps; a long name ends in an ellipsis, the value stays right-aligned. The tooltip is
|
|
101
|
+
* portalled out of the chart's tree, hence the global class names. `!important` because the
|
|
102
|
+
* engine's own values are inline.
|
|
103
|
+
*/
|
|
104
|
+
:global(.ts-chart-tooltip__row) {
|
|
105
|
+
grid-template-columns: 0.55rem minmax(6rem, max-content) auto !important;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
:global(.ts-chart-tooltip__row > span:nth-child(2)) {
|
|
109
|
+
max-width: 14rem;
|
|
110
|
+
overflow: hidden;
|
|
111
|
+
text-overflow: ellipsis;
|
|
112
|
+
white-space: nowrap;
|
|
113
|
+
}
|
|
@@ -70,3 +70,28 @@
|
|
|
70
70
|
.factsBasis {
|
|
71
71
|
margin: var(--vx-space-stack-xs) 0 0;
|
|
72
72
|
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* A fact's tone is INK only (the value and its trailing text), never a fill. The status solids are
|
|
76
|
+
* the text-safe ones (`status-text.ts`). `stale` is the strip cell's dimmed value, verbatim
|
|
77
|
+
* (`stat-strip.module.css` `.cell[data-stale]`).
|
|
78
|
+
*/
|
|
79
|
+
.fact[data-tone='good'] :is(.factValue, .factUnit, .factPeriod) {
|
|
80
|
+
color: var(--vx-status-good);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.fact[data-tone='warn'] :is(.factValue, .factUnit, .factPeriod) {
|
|
84
|
+
color: var(--vx-status-warn);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.fact[data-tone='bad'] :is(.factValue, .factUnit, .factPeriod) {
|
|
88
|
+
color: var(--vx-status-bad);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.fact[data-tone='muted'] :is(.factValue, .factUnit, .factPeriod) {
|
|
92
|
+
color: var(--vx-muted);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.factsRow[data-stale] {
|
|
96
|
+
opacity: 0.55;
|
|
97
|
+
}
|
|
@@ -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)
|