basalt-ui 2.21.0 → 2.22.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 +33 -0
- package/MIGRATING.md +277 -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 +27 -3
- package/dist/charts/Bars.d.ts.map +1 -1
- package/dist/charts/Bars.js +5 -0
- package/dist/charts/CartesianChart.d.ts +8 -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 +29 -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 +7 -1
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +39 -14
- 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 +20 -1
- package/dist/charts/band-fold.d.ts.map +1 -1
- package/dist/charts/band-fold.js +9 -0
- package/dist/charts/chart-box.module.css +19 -0
- package/dist/charts/chart-card.module.css +25 -0
- package/dist/charts/chart-legend.module.css +7 -0
- package/dist/charts/compile.d.ts +17 -6
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +145 -74
- package/dist/charts/labels.d.ts +15 -4
- package/dist/charts/labels.d.ts.map +1 -1
- package/dist/charts/labels.js +34 -10
- 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 +26 -0
- package/dist/charts/ref-label.d.ts.map +1 -0
- package/dist/charts/ref-label.js +63 -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 +10 -11
- 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 +2 -1
- package/dist/charts/types.d.ts +14 -5
- 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 +194 -32
- package/dist/cli/shot/detect.d.ts.map +1 -1
- package/dist/cli/shot/detect.js +14 -1
- package/dist/cli/shot/playwright.d.ts +5 -2
- package/dist/cli/shot/playwright.d.ts.map +1 -1
- package/dist/cli/shot/types.d.ts +2 -0
- package/dist/cli/shot/types.d.ts.map +1 -1
- package/dist/cli/shot/types.js +2 -0
- 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.map +1 -1
- package/dist/dashboard/bar-list.js +7 -8
- package/dist/dashboard/bar-list.module.css +53 -0
- package/dist/dashboard/bar-list.module.css.d.ts +1 -0
- 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 +77 -3
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +88 -15
- package/dist/dashboard/meter-parts.d.ts +8 -0
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +238 -71
- package/dist/dashboard/meter.d.ts +43 -60
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +20 -3
- package/dist/dashboard/meter.module.css +61 -0
- package/dist/dashboard/meter.module.css.d.ts +5 -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/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 +17 -0
- package/dist/dashboard/stat-strip.d.ts +7 -1
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +74 -46
- package/dist/dashboard/stat-strip.module.css +67 -20
- 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/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 +34 -2
- 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 +23 -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 +8 -20
- 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 +4 -0
- package/dist/tokens/palette.d.ts.map +1 -1
- package/dist/tokens/palette.js +4 -0
- package/dist/tokens/status-text.d.ts.map +1 -1
- package/dist/tokens/status-text.js +3 -1
- package/llms.txt +38 -20
- package/package.json +1 -1
- package/src/charts/BandStrip.tsx +33 -3
- package/src/charts/Bars.tsx +6 -0
- package/src/charts/CartesianChart.tsx +16 -3
- package/src/charts/ChartCard.tsx +34 -6
- 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 +50 -13
- package/src/charts/StackedArea.tsx +14 -4
- package/src/charts/band-fold.ts +31 -1
- package/src/charts/chart-box.module.css +19 -0
- package/src/charts/chart-card.module.css +25 -0
- package/src/charts/chart-legend.module.css +7 -0
- package/src/charts/compile.ts +210 -85
- package/src/charts/labels.ts +48 -14
- 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 +104 -0
- package/src/charts/scales.ts +22 -2
- package/src/charts/sparkline.module.css +10 -11
- package/src/charts/sparkline.module.css.d.ts +1 -2
- package/src/charts/state.tsx +3 -1
- package/src/charts/types.ts +14 -5
- package/src/cli/doctor-upgrade.ts +1 -1
- package/src/cli/init.ts +100 -3
- package/src/cli/shot/capture.ts +251 -32
- package/src/cli/shot/detect.ts +18 -1
- package/src/cli/shot/playwright.ts +2 -2
- package/src/cli/shot/types.ts +3 -0
- 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 +53 -0
- package/src/dashboard/bar-list.module.css.d.ts +1 -0
- package/src/dashboard/bar-list.tsx +78 -69
- package/src/dashboard/index.ts +1 -0
- package/src/dashboard/meter-model.ts +186 -20
- package/src/dashboard/meter-parts.tsx +285 -64
- package/src/dashboard/meter.module.css +61 -0
- package/src/dashboard/meter.module.css.d.ts +5 -0
- package/src/dashboard/meter.tsx +60 -10
- 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/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 +17 -0
- package/src/dashboard/stat-card.tsx +12 -6
- package/src/dashboard/stat-strip.module.css +67 -20
- package/src/dashboard/stat-strip.module.css.d.ts +4 -0
- package/src/dashboard/stat-strip.tsx +118 -72
- package/src/dashboard/visual-slot.ts +12 -3
- package/src/guard/vx-names.ts +2 -0
- package/src/index.ts +2 -0
- package/src/styles.css +34 -2
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +186 -60
- package/src/theme/chrome-vars.ts +77 -8
- package/src/theme/controls.module.css +23 -0
- package/src/theme/controls.module.css.d.ts +1 -0
- package/src/theme/index.ts +12 -25
- package/src/tokens/core.ts +4 -1
- package/src/tokens/derive.ts +32 -3
- package/src/tokens/palette.ts +4 -0
- package/src/tokens/status-text.ts +2 -0
|
@@ -5,11 +5,26 @@
|
|
|
5
5
|
import type { ReactNode } from 'react'
|
|
6
6
|
import { clamp01, toPct } from './scale'
|
|
7
7
|
|
|
8
|
-
export type MeterSegment = {
|
|
8
|
+
export type MeterSegment = {
|
|
9
|
+
value: number
|
|
10
|
+
label: string
|
|
11
|
+
color?: string
|
|
12
|
+
/**
|
|
13
|
+
* `'muted'`: the part takes a step of the muted ramp (a lightness step per muted part). A `color`
|
|
14
|
+
* wins. Steps stay ≥15 points apart up to five muted parts; more merge at the cap.
|
|
15
|
+
*/
|
|
16
|
+
tone?: 'muted'
|
|
17
|
+
forecast?: boolean
|
|
18
|
+
}
|
|
9
19
|
export type MeterZone = {
|
|
10
20
|
from: number
|
|
11
21
|
to: number
|
|
12
22
|
label: string
|
|
23
|
+
/**
|
|
24
|
+
* `'muted'`: the band takes its step of the muted ramp instead of the line ramp. A `color` wins.
|
|
25
|
+
* Steps stay ≥15 points apart up to six zones; more merge at the cap.
|
|
26
|
+
*/
|
|
27
|
+
tone?: 'muted'
|
|
13
28
|
/**
|
|
14
29
|
* A shorter name for a narrow track. The row tries `label`, then `short`, and only then hides the
|
|
15
30
|
* name; the holding zone always shows its full `label`. The described-by text keeps `label`.
|
|
@@ -20,6 +35,40 @@ export type MeterZone = {
|
|
|
20
35
|
/** A mark on the track at `at` (in the reading's units). A label is captioned beside it. */
|
|
21
36
|
export type MeterTick = { at: number; label?: string }
|
|
22
37
|
|
|
38
|
+
/** A colour of a `stops` gradient at `at` (in the reading's units); the colour is the consumer's. */
|
|
39
|
+
type MeterStop = { at: number; color: string }
|
|
40
|
+
|
|
41
|
+
/** A ramp step's strength in percent of its neutral: from `lo`, spread over `step` across the steps. */
|
|
42
|
+
export const RAMP_ZONE = { lo: 15, step: 45, solo: 40 }
|
|
43
|
+
export const RAMP_PART = { lo: 35, step: 50, solo: 60 }
|
|
44
|
+
/** Neighbouring steps stay at least this far apart (the contrast the zone ramp always had), up to `RAMP_MAX`. */
|
|
45
|
+
export const RAMP_MIN_STEP = 15
|
|
46
|
+
export const RAMP_MAX = 95
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Step `index` of `count`. The spread is floored at `RAMP_MIN_STEP` per step, so a ramp stays
|
|
50
|
+
* distinguishable by lightness up to six zones (five segments); past that the last steps clamp at
|
|
51
|
+
* `RAMP_MAX` and merge, which is the cue to group parts.
|
|
52
|
+
*/
|
|
53
|
+
export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZONE): number {
|
|
54
|
+
if (count <= 1) return ramp.solo
|
|
55
|
+
return Math.min(RAMP_MAX, ramp.lo + index * Math.max(RAMP_MIN_STEP, ramp.step / (count - 1)))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function rampFill(
|
|
59
|
+
base: 'line' | 'muted',
|
|
60
|
+
index: number,
|
|
61
|
+
count: number,
|
|
62
|
+
ramp: typeof RAMP_ZONE,
|
|
63
|
+
): string {
|
|
64
|
+
return `color-mix(in srgb, var(--vx-${base}) ${rampStrength(index, count, ramp)}%, transparent)`
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A zone's fill: its own colour, else its step of the line (or, `tone="muted"`, the muted) ramp. */
|
|
68
|
+
export function zoneFill(zone: MeterZone, index: number, count: number): string {
|
|
69
|
+
return zone.color ?? rampFill(zone.tone === 'muted' ? 'muted' : 'line', index, count, RAMP_ZONE)
|
|
70
|
+
}
|
|
71
|
+
|
|
23
72
|
/** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
|
|
24
73
|
export type TickMark = { key: string; pct: number; label: string | undefined; text: string }
|
|
25
74
|
|
|
@@ -35,7 +84,14 @@ export type Part = {
|
|
|
35
84
|
}
|
|
36
85
|
|
|
37
86
|
/** One categorical part's caption: its position key, name, formatted amount and fill colour (`undefined` = neutral). */
|
|
38
|
-
export type PartCaption = {
|
|
87
|
+
export type PartCaption = {
|
|
88
|
+
key: string
|
|
89
|
+
label: string
|
|
90
|
+
text: string
|
|
91
|
+
color: string | undefined
|
|
92
|
+
/** Where the part starts on the track, in percent: the caption sits under it when they all fit. */
|
|
93
|
+
startPct: number
|
|
94
|
+
}
|
|
39
95
|
|
|
40
96
|
/** One zone's name on the label row: its span's centre and width (percent) and whether it holds the reading. */
|
|
41
97
|
export type ZoneLabel = {
|
|
@@ -81,6 +137,36 @@ export function tickMarks(
|
|
|
81
137
|
)
|
|
82
138
|
}
|
|
83
139
|
|
|
140
|
+
/** A `stops` gradient's colour list, in order along the track; `at` outside `[min, max]` clamps. */
|
|
141
|
+
export function stopsGradient(stops: readonly MeterStop[], min: number, span: number): string {
|
|
142
|
+
return stops
|
|
143
|
+
.toSorted((a, b) => a.at - b.at)
|
|
144
|
+
.map((s) => `${s.color} ${toPct(clamp01((s.at - min) / span))}%`)
|
|
145
|
+
.join(', ')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const ringAngle = (pct: number) => Math.round(pct * 3.6 * 100) / 100
|
|
149
|
+
|
|
150
|
+
/** A ring's marks (target, ticks): the angle clockwise from the top, and the accessible text. */
|
|
151
|
+
export function ringMarks(
|
|
152
|
+
target: number | undefined,
|
|
153
|
+
ticks: readonly MeterTick[] | undefined,
|
|
154
|
+
min: number,
|
|
155
|
+
max: number,
|
|
156
|
+
format: Format,
|
|
157
|
+
): { angle: number; text: string }[] {
|
|
158
|
+
const { span } = spanOf(min, max)
|
|
159
|
+
const marks = tickMarks(ticks, min, max, format).map((t) => ({
|
|
160
|
+
angle: ringAngle(t.pct),
|
|
161
|
+
text: t.text,
|
|
162
|
+
}))
|
|
163
|
+
if (target === undefined || !Number.isFinite(target) || target < min || target > max) return marks
|
|
164
|
+
return [
|
|
165
|
+
{ angle: ringAngle(toPct((target - min) / span)), text: `Target ${format(target)}` },
|
|
166
|
+
...marks,
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
|
|
84
170
|
export const isReading = (n: number | null | undefined): n is number =>
|
|
85
171
|
typeof n === 'number' && Number.isFinite(n)
|
|
86
172
|
|
|
@@ -110,6 +196,9 @@ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegme
|
|
|
110
196
|
/** Segment amounts (already normalised) accumulate from `min`; a part never passes the track end. */
|
|
111
197
|
export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
|
|
112
198
|
let cursor = 0
|
|
199
|
+
const isMuted = (s: MeterSegment) => s.tone === 'muted' && s.color === undefined && !s.forecast
|
|
200
|
+
const mutedCount = segments.filter(isMuted).length
|
|
201
|
+
let muted = 0
|
|
113
202
|
return segments.map((s) => {
|
|
114
203
|
const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
|
|
115
204
|
const forecast = s.forecast === true
|
|
@@ -117,7 +206,11 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
|
|
|
117
206
|
const part = {
|
|
118
207
|
start: cursor,
|
|
119
208
|
size,
|
|
120
|
-
color: forecast
|
|
209
|
+
color: forecast
|
|
210
|
+
? undefined
|
|
211
|
+
: isMuted(s)
|
|
212
|
+
? rampFill('muted', muted++, mutedCount, RAMP_PART)
|
|
213
|
+
: s.color,
|
|
121
214
|
forecast,
|
|
122
215
|
label: s.label,
|
|
123
216
|
amount: s.value,
|
|
@@ -143,7 +236,14 @@ export type LabelBox = { left: number; right: number }
|
|
|
143
236
|
* A label on the shared row: its measured box and, for a zone name, the span it must stay inside.
|
|
144
237
|
* The holding zone's name never hides for width: it ignores its span and is shifted into the row.
|
|
145
238
|
*/
|
|
146
|
-
export type RowLabel = {
|
|
239
|
+
export type RowLabel = {
|
|
240
|
+
box: LabelBox
|
|
241
|
+
span?: LabelBox
|
|
242
|
+
holding?: boolean
|
|
243
|
+
/** A tick at the track's start or end: it yields to every zone name it would touch. */
|
|
244
|
+
end?: boolean
|
|
245
|
+
short?: LabelBox
|
|
246
|
+
}
|
|
147
247
|
|
|
148
248
|
/** The placement of the row's labels, index-aligned with the input. */
|
|
149
249
|
export type RowPlacement = {
|
|
@@ -157,33 +257,44 @@ export type RowPlacement = {
|
|
|
157
257
|
/**
|
|
158
258
|
* Which labels of the row show, in input order. The holding zone's name claims its place first (it
|
|
159
259
|
* is the reading's verdict, so it never hides for width: it may spill past its span, shifted inside
|
|
160
|
-
* the row); the
|
|
260
|
+
* the row); the other ticks go next; every other zone name then shows only
|
|
161
261
|
* when it lies inside the row and its own span and clears every label already kept, trying its full
|
|
162
|
-
* name and then its `short` box
|
|
262
|
+
* name and then its `short` box; the end ticks (at the track's start or end) go last, so they yield
|
|
263
|
+
* to any name they would touch. A tick keeps at least `gap` px (one gap step of the host's
|
|
264
|
+
* spacing) from every other label; zone names among themselves only must not touch. A hidden name is still named in the described-by text.
|
|
163
265
|
*/
|
|
164
|
-
export function placeRowLabels(
|
|
266
|
+
export function placeRowLabels(
|
|
267
|
+
labels: readonly RowLabel[],
|
|
268
|
+
rowWidth: number,
|
|
269
|
+
gap = 0,
|
|
270
|
+
): RowPlacement {
|
|
165
271
|
const shown = labels.map(() => false)
|
|
166
272
|
const short = labels.map(() => false)
|
|
167
273
|
const shift = labels.map(() => 0)
|
|
168
|
-
const kept: LabelBox[] = []
|
|
169
|
-
const rank = (l: RowLabel) => (l.holding ? 0 : l.span ? 2 : 1)
|
|
274
|
+
const kept: { box: LabelBox; tick: boolean }[] = []
|
|
275
|
+
const rank = (l: RowLabel) => (l.holding ? 0 : l.end ? 3 : l.span ? 2 : 1)
|
|
170
276
|
const order = labels
|
|
171
277
|
.map((_, i) => i)
|
|
172
278
|
.toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
|
|
173
|
-
|
|
174
|
-
|
|
279
|
+
// The gap step applies when either label of a pair is a tick; zone names keep to "not touching".
|
|
280
|
+
const clears = (b: LabelBox, tick: boolean) =>
|
|
281
|
+
!kept.some(({ box: k, tick: kTick }) => {
|
|
282
|
+
const pad = tick || kTick ? gap : 0
|
|
283
|
+
return b.left < k.right + pad - LABEL_SUBPIXEL && k.left < b.right + pad - LABEL_SUBPIXEL
|
|
284
|
+
})
|
|
175
285
|
for (const i of order) {
|
|
176
286
|
const label = labels[i] as RowLabel
|
|
177
287
|
const { span } = label
|
|
288
|
+
const tick = span === undefined && !label.holding
|
|
178
289
|
let box = label.box
|
|
179
290
|
if (label.holding) {
|
|
180
291
|
const width = box.right - box.left
|
|
181
292
|
const left = Math.max(0, Math.min(box.left, rowWidth - width))
|
|
182
293
|
shift[i] = left - box.left
|
|
183
294
|
box = { left, right: left + width }
|
|
184
|
-
if (!clears(box)) continue
|
|
295
|
+
if (!clears(box, false)) continue
|
|
185
296
|
shown[i] = true
|
|
186
|
-
kept.push(box)
|
|
297
|
+
kept.push({ box, tick: false })
|
|
187
298
|
continue
|
|
188
299
|
}
|
|
189
300
|
const fits = (b: LabelBox) =>
|
|
@@ -191,13 +302,13 @@ export function placeRowLabels(labels: readonly RowLabel[], rowWidth: number): R
|
|
|
191
302
|
b.right <= rowWidth + LABEL_SUBPIXEL &&
|
|
192
303
|
(span === undefined ||
|
|
193
304
|
(b.left >= span.left - LABEL_SUBPIXEL && b.right <= span.right + LABEL_SUBPIXEL)) &&
|
|
194
|
-
clears(b)
|
|
305
|
+
clears(b, tick)
|
|
195
306
|
const useShort = !fits(box) && label.short !== undefined && fits(label.short)
|
|
196
307
|
if (useShort) box = label.short as LabelBox
|
|
197
308
|
else if (!fits(box)) continue
|
|
198
309
|
shown[i] = true
|
|
199
310
|
short[i] = useShort
|
|
200
|
-
kept.push(box)
|
|
311
|
+
kept.push({ box, tick })
|
|
201
312
|
}
|
|
202
313
|
return { shown, short, shift }
|
|
203
314
|
}
|
|
@@ -219,6 +330,7 @@ export type BarInput = {
|
|
|
219
330
|
format: Format
|
|
220
331
|
segments?: readonly MeterSegment[] | undefined
|
|
221
332
|
zones?: readonly MeterZone[] | undefined
|
|
333
|
+
stops?: readonly MeterStop[] | undefined
|
|
222
334
|
target?: number | undefined
|
|
223
335
|
ticks?: readonly MeterTick[] | undefined
|
|
224
336
|
color?: string | undefined
|
|
@@ -235,6 +347,8 @@ export type BarModel = {
|
|
|
235
347
|
captionMode: CaptionMode
|
|
236
348
|
/** Only in `parts` mode: one caption per part, in order. Empty otherwise. */
|
|
237
349
|
partCaptions: readonly PartCaption[]
|
|
350
|
+
/** The `stops` gradient's colour list; absent without `stops`. */
|
|
351
|
+
stopsCss: string | undefined
|
|
238
352
|
/** Only on a zoned track: every zone's name, in zone order. Empty otherwise. */
|
|
239
353
|
zoneLabels: readonly ZoneLabel[]
|
|
240
354
|
ticks: readonly TickMark[]
|
|
@@ -252,8 +366,8 @@ function endCaptionText(reading: number, target: number | undefined, format: For
|
|
|
252
366
|
return `Target ${format(target)}`
|
|
253
367
|
}
|
|
254
368
|
|
|
255
|
-
function fillParts(input: BarInput, span: number,
|
|
256
|
-
if (
|
|
369
|
+
function fillParts(input: BarInput, span: number, marked: boolean): Part[] {
|
|
370
|
+
if (marked) return []
|
|
257
371
|
if (input.segments !== undefined) return layoutSegments(input.segments, span)
|
|
258
372
|
const fill = { value: input.reading - input.min, label: '' }
|
|
259
373
|
return layoutSegments([input.color === undefined ? fill : { ...fill, color: input.color }], span)
|
|
@@ -270,6 +384,7 @@ function partCaptionsOf(parts: readonly Part[], format: Format): PartCaption[] {
|
|
|
270
384
|
label: p.label,
|
|
271
385
|
text: format(p.amount),
|
|
272
386
|
color: p.color,
|
|
387
|
+
startPct: toPct(p.start),
|
|
273
388
|
}))
|
|
274
389
|
}
|
|
275
390
|
|
|
@@ -294,13 +409,14 @@ export function startCaptionText(model: BarModel, override: ReactNode | undefine
|
|
|
294
409
|
}
|
|
295
410
|
|
|
296
411
|
export function modelBar(input: BarInput): BarModel {
|
|
297
|
-
const { reading, min, max, format, segments, zones, target } = input
|
|
412
|
+
const { reading, min, max, format, segments, zones, target, stops } = input
|
|
298
413
|
const ticks = tickMarks(input.ticks, min, max, format)
|
|
299
414
|
const { span } = spanOf(min, max)
|
|
300
415
|
const zoned = zones !== undefined && zones.length > 0
|
|
301
416
|
const holding = zoned ? findHoldingZone(zones, reading) : undefined
|
|
302
417
|
|
|
303
|
-
const
|
|
418
|
+
const striped = !zoned && stops !== undefined && stops.length > 0
|
|
419
|
+
const parts = fillParts(input, span, zoned || striped)
|
|
304
420
|
const firstForecast = parts.findIndex((p) => p.forecast)
|
|
305
421
|
const forecastLabels =
|
|
306
422
|
segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
|
|
@@ -333,6 +449,7 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
333
449
|
: (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
|
|
334
450
|
captionMode,
|
|
335
451
|
partCaptions,
|
|
452
|
+
stopsCss: striped ? stopsGradient(stops, min, span) : undefined,
|
|
336
453
|
zoneLabels: !zoned
|
|
337
454
|
? []
|
|
338
455
|
: zones.map((z, i) => {
|
|
@@ -347,7 +464,7 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
347
464
|
}
|
|
348
465
|
}),
|
|
349
466
|
ticks,
|
|
350
|
-
markerPct: zoned ? toPct((reading - min) / span) : undefined,
|
|
467
|
+
markerPct: zoned || striped ? toPct((reading - min) / span) : undefined,
|
|
351
468
|
targetPct: target === undefined ? undefined : toPct((target - min) / span),
|
|
352
469
|
startText: format(reading),
|
|
353
470
|
endText: endCaptionText(reading, target, format),
|
|
@@ -358,3 +475,52 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
358
475
|
/** The ring's one arc, as a fraction of the circumference. */
|
|
359
476
|
export const ringFraction = (reading: number, min: number, max: number): number =>
|
|
360
477
|
clamp01((reading - min) / spanOf(min, max).span)
|
|
478
|
+
|
|
479
|
+
/** One caption on the marks row: its natural width and where it is anchored (`pct`), else its edge. */
|
|
480
|
+
export type MarkItem = { width: number; pct?: number; edge?: 'end' }
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
|
|
484
|
+
* anchored caption is centred on its mark and clamped inside the row, the others sit at the start (or,
|
|
485
|
+
* `edge: 'end'`, the end). `null` when nothing is anchored, the row is unmeasured, a caption is wider
|
|
486
|
+
* than the row, or two captions would sit closer than `gap`: the row then flows and wraps instead.
|
|
487
|
+
*/
|
|
488
|
+
export function placeMarkCaptions(
|
|
489
|
+
items: readonly MarkItem[],
|
|
490
|
+
rowWidth: number,
|
|
491
|
+
gap: number,
|
|
492
|
+
): number[] | null {
|
|
493
|
+
if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
|
|
494
|
+
if (items.some((i) => i.width > rowWidth)) return null
|
|
495
|
+
const lefts = items.map((i) => {
|
|
496
|
+
if (i.pct === undefined) return i.edge === 'end' ? rowWidth - i.width : 0
|
|
497
|
+
return Math.max(0, Math.min((rowWidth * i.pct) / 100 - i.width / 2, rowWidth - i.width))
|
|
498
|
+
})
|
|
499
|
+
const order = lefts.map((_, i) => i).toSorted((a, b) => (lefts[a] ?? 0) - (lefts[b] ?? 0))
|
|
500
|
+
const crowded = order.some((i, n) => {
|
|
501
|
+
const next = order[n + 1]
|
|
502
|
+
return (
|
|
503
|
+
next !== undefined &&
|
|
504
|
+
(lefts[next] ?? 0) < (lefts[i] ?? 0) + (items[i]?.width ?? 0) + gap - LABEL_SUBPIXEL
|
|
505
|
+
)
|
|
506
|
+
})
|
|
507
|
+
return crowded ? null : lefts
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Whether every categorical part's caption fits under its own segment: left-aligned at the part's
|
|
512
|
+
* start and ending before the next part's start (less `gap`) or the row's end. A zero-width part never fits.
|
|
513
|
+
*/
|
|
514
|
+
export function partsFitUnder(
|
|
515
|
+
parts: readonly { startPct: number; width: number }[],
|
|
516
|
+
rowWidth: number,
|
|
517
|
+
gap: number,
|
|
518
|
+
): boolean {
|
|
519
|
+
if (rowWidth <= 0) return false
|
|
520
|
+
return parts.every((p, i) => {
|
|
521
|
+
const left = (rowWidth * p.startPct) / 100
|
|
522
|
+
const next = parts[i + 1]
|
|
523
|
+
const limit = next === undefined ? rowWidth : (rowWidth * next.startPct) / 100 - gap
|
|
524
|
+
return left + p.width <= limit + LABEL_SUBPIXEL
|
|
525
|
+
})
|
|
526
|
+
}
|