basalt-ui 2.16.0 → 2.18.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 +165 -124
- package/MIGRATING.md +133 -15
- package/agent/agents/basalt-design-reviewer.md +2 -2
- package/agent/skills/basalt-design/SKILL.md +3 -3
- package/configs/oxlint-plugin.js +2 -2
- package/dist/charts/Donut.d.ts.map +1 -1
- package/dist/charts/Donut.js +2 -2
- package/dist/charts/DualPanel.js +2 -2
- package/dist/charts/Heatmap.d.ts.map +1 -1
- package/dist/charts/Heatmap.js +2 -2
- package/dist/charts/MirroredBars.js +2 -2
- package/dist/charts/Sparkline.d.ts +4 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +3 -3
- package/dist/charts/compile.d.ts +8 -3
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +19 -9
- package/dist/charts/cursor.d.ts +14 -1
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +29 -3
- package/dist/charts/date-tick-format.d.ts +17 -0
- package/dist/charts/date-tick-format.d.ts.map +1 -0
- package/dist/charts/date-tick-format.js +81 -0
- package/dist/charts/panel.d.ts +2 -1
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +3 -2
- package/dist/charts/types.d.ts +5 -1
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/cli/help.d.ts +1 -0
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +8 -2
- package/dist/cli/shot/capture.d.ts +28 -1
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +96 -13
- package/dist/cli/shot/index.d.ts +10 -1
- package/dist/cli/shot/index.d.ts.map +1 -1
- package/dist/cli/shot/index.js +40 -5
- package/dist/cli/shot/playwright.d.ts +4 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -1
- package/dist/cli/shot/real-data.d.ts +35 -0
- package/dist/cli/shot/real-data.d.ts.map +1 -0
- package/dist/cli/shot/real-data.js +120 -0
- package/dist/cli/shot/serve.d.ts +2 -2
- package/dist/cli/shot/serve.d.ts.map +1 -1
- package/dist/cli/shot/serve.js +16 -2
- package/dist/dashboard/bar-list.d.ts +29 -0
- package/dist/dashboard/bar-list.d.ts.map +1 -0
- package/dist/dashboard/bar-list.js +52 -0
- package/dist/dashboard/bar-list.module.css +73 -0
- package/dist/dashboard/bar-list.module.css.d.ts +16 -0
- package/dist/dashboard/delta-badge.d.ts +2 -2
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +12 -14
- package/dist/dashboard/index.d.ts +2 -0
- package/dist/dashboard/index.d.ts.map +1 -1
- package/dist/dashboard/index.js +4 -0
- package/dist/dashboard/meter-model.d.ts +78 -0
- package/dist/dashboard/meter-model.d.ts.map +1 -0
- package/dist/dashboard/meter-model.js +91 -0
- package/dist/dashboard/meter-parts.d.ts +42 -0
- package/dist/dashboard/meter-parts.d.ts.map +1 -0
- package/dist/dashboard/meter-parts.js +163 -0
- package/dist/dashboard/meter.d.ts +112 -0
- package/dist/dashboard/meter.d.ts.map +1 -0
- package/dist/dashboard/meter.js +43 -0
- package/dist/dashboard/meter.module.css +150 -0
- package/dist/dashboard/meter.module.css.d.ts +28 -0
- package/dist/dashboard/query-state.d.ts +35 -0
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +12 -1
- package/dist/dashboard/scale.d.ts +9 -0
- package/dist/dashboard/scale.d.ts.map +1 -0
- package/dist/dashboard/scale.js +10 -0
- package/dist/dashboard/stat-card-parts.d.ts +40 -0
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -0
- package/dist/dashboard/stat-card-parts.js +55 -0
- package/dist/dashboard/stat-card.d.ts +46 -34
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +148 -79
- package/dist/dashboard/stat-card.module.css +220 -7
- package/dist/dashboard/stat-card.module.css.d.ts +20 -0
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +73 -9
- package/dist/dashboard/widget-header.module.css +25 -17
- package/dist/guard/index.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/theme/chrome-vars.d.ts +82 -0
- package/dist/theme/chrome-vars.d.ts.map +1 -0
- package/dist/theme/chrome-vars.js +36 -0
- package/dist/theme/controls.module.css +20 -1
- package/dist/theme/controls.module.css.d.ts +1 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +30 -33
- package/dist/tokens/derive.d.ts +11 -1
- package/dist/tokens/derive.d.ts.map +1 -1
- package/dist/tokens/derive.js +9 -24
- package/dist/tokens/status-text.d.ts +26 -0
- package/dist/tokens/status-text.d.ts.map +1 -0
- package/dist/tokens/status-text.js +68 -0
- package/llms.txt +36 -4
- package/package.json +1 -1
- package/src/charts/Donut.tsx +2 -2
- package/src/charts/DualPanel.tsx +2 -2
- package/src/charts/Heatmap.tsx +2 -2
- package/src/charts/MirroredBars.tsx +2 -2
- package/src/charts/Sparkline.tsx +7 -5
- package/src/charts/compile.ts +31 -11
- package/src/charts/cursor.ts +48 -3
- package/src/charts/date-tick-format.ts +132 -0
- package/src/charts/panel.tsx +5 -3
- package/src/charts/types.ts +5 -1
- package/src/cli/help.ts +22 -2
- package/src/cli/shot/capture.ts +133 -12
- package/src/cli/shot/index.ts +50 -4
- package/src/cli/shot/playwright.ts +1 -1
- package/src/cli/shot/real-data.ts +165 -0
- package/src/cli/shot/serve.ts +28 -3
- package/src/dashboard/bar-list.module.css +73 -0
- package/src/dashboard/bar-list.module.css.d.ts +16 -0
- package/src/dashboard/bar-list.tsx +112 -0
- package/src/dashboard/delta-badge.tsx +24 -18
- package/src/dashboard/index.ts +2 -0
- package/src/dashboard/meter-model.ts +161 -0
- package/src/dashboard/meter-parts.tsx +223 -0
- package/src/dashboard/meter.module.css +150 -0
- package/src/dashboard/meter.module.css.d.ts +28 -0
- package/src/dashboard/meter.tsx +162 -0
- package/src/dashboard/query-state.tsx +38 -0
- package/src/dashboard/scale.ts +13 -0
- package/src/dashboard/stat-card-parts.tsx +108 -0
- package/src/dashboard/stat-card.module.css +220 -7
- package/src/dashboard/stat-card.module.css.d.ts +20 -0
- package/src/dashboard/stat-card.tsx +282 -174
- package/src/dashboard/widget-header.module.css +25 -17
- package/src/dashboard/widget-header.tsx +103 -7
- package/src/guard/index.ts +2 -2
- package/src/index.ts +4 -0
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +128 -40
- package/src/theme/chrome-vars.ts +111 -0
- package/src/theme/controls.module.css +20 -1
- package/src/theme/controls.module.css.d.ts +1 -0
- package/src/theme/index.ts +39 -101
- package/src/tokens/derive.ts +19 -26
- package/src/tokens/status-text.ts +85 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status text solver — the `--vx-status-good|warn|bad` solids are painted as TEXT (`DeltaBadge`,
|
|
3
|
+
* a toned breakdown row), so each must clear WCAG AA on every text surface of the palette it ships
|
|
4
|
+
* in. The calibrated stops below are kept unchanged when they already clear every surface (the
|
|
5
|
+
* default config); otherwise each walks along its lightness axis only — light darkens, dark
|
|
6
|
+
* lightens, hue preserved (chroma may clip at the gamut edge) — to the first tone that does.
|
|
7
|
+
*/
|
|
8
|
+
import type { ColorPair } from './core.js';
|
|
9
|
+
import type { StatusKey, SurfaceKey } from './derive.js';
|
|
10
|
+
type Side = 'light' | 'dark';
|
|
11
|
+
type Stop = {
|
|
12
|
+
readonly tone: number;
|
|
13
|
+
readonly chroma: number;
|
|
14
|
+
};
|
|
15
|
+
export declare const STATUS_HUE: Readonly<Record<StatusKey, number>>;
|
|
16
|
+
export declare const STATUS_STOP: Readonly<Record<StatusKey, Readonly<Record<Side, Stop>>>>;
|
|
17
|
+
/** The surfaces status text is painted on — exhaustive, so a new `SurfaceKey` must pick a side. */
|
|
18
|
+
export declare const STATUS_TEXT_SURFACES: Readonly<Record<SurfaceKey, boolean>>;
|
|
19
|
+
export declare const STATUS_TEXT_CONTRAST_MIN = 4.5;
|
|
20
|
+
export declare const STATUS_TONE_STEP = 0.25;
|
|
21
|
+
/** The hex at the `step`-th tone from `stop` along the side's lightness axis (0 = the stop). */
|
|
22
|
+
export declare function statusHexAtStep(key: StatusKey, side: Side, step: number): string;
|
|
23
|
+
/** The three status solids for a derived palette's surfaces. */
|
|
24
|
+
export declare function deriveStatusHexes(surfaces: Record<SurfaceKey, ColorPair>): Record<StatusKey, ColorPair>;
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=status-text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"status-text.d.ts","sourceRoot":"","sources":["../../src/tokens/status-text.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAGrD,KAAK,IAAI,GAAG,OAAO,GAAG,MAAM,CAAA;AAC5B,KAAK,IAAI,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAE9D,eAAO,MAAM,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAI1D,CAAA;AAED,eAAO,MAAM,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAIjF,CAAA;AAED,mGAAmG;AACnG,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAStE,CAAA;AAED,eAAO,MAAM,wBAAwB,MAAM,CAAA;AAC3C,eAAO,MAAM,gBAAgB,OAAO,CAAA;AAOpC,gGAAgG;AAChG,wBAAgB,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAQhF;AAoBD,gEAAgE;AAChE,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,MAAM,CAAC,UAAU,EAAE,SAAS,CAAC,GACtC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAM9B"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { contrastRatio, hctToHex } from "./hct.js";
|
|
2
|
+
const STATUS_HUE = {
|
|
3
|
+
good: 151.975,
|
|
4
|
+
warn: 76.64,
|
|
5
|
+
bad: 29.905
|
|
6
|
+
};
|
|
7
|
+
const STATUS_STOP = {
|
|
8
|
+
good: { light: { tone: 45.75, chroma: 37.75 }, dark: { tone: 70.23, chroma: 53.23 } },
|
|
9
|
+
warn: { light: { tone: 46, chroma: 61.12 }, dark: { tone: 72.34, chroma: 62.55 } },
|
|
10
|
+
bad: { light: { tone: 43.86, chroma: 54.46 }, dark: { tone: 66.5, chroma: 53.79 } }
|
|
11
|
+
};
|
|
12
|
+
const STATUS_TEXT_SURFACES = {
|
|
13
|
+
bg: true,
|
|
14
|
+
panel: true,
|
|
15
|
+
panelHover: true,
|
|
16
|
+
elevated: true,
|
|
17
|
+
subtle: true,
|
|
18
|
+
field: true,
|
|
19
|
+
border: false,
|
|
20
|
+
hairline: false
|
|
21
|
+
};
|
|
22
|
+
const STATUS_TEXT_CONTRAST_MIN = 4.5;
|
|
23
|
+
const STATUS_TONE_STEP = 0.25;
|
|
24
|
+
const TEXT_SURFACE_KEYS = Object.keys(STATUS_TEXT_SURFACES).filter(
|
|
25
|
+
(key) => STATUS_TEXT_SURFACES[key]
|
|
26
|
+
);
|
|
27
|
+
const STATUS_KEYS = Object.keys(STATUS_HUE);
|
|
28
|
+
function statusHexAtStep(key, side, step) {
|
|
29
|
+
const stop = STATUS_STOP[key][side];
|
|
30
|
+
const direction = side === "light" ? -1 : 1;
|
|
31
|
+
return hctToHex({
|
|
32
|
+
hue: STATUS_HUE[key],
|
|
33
|
+
chroma: stop.chroma,
|
|
34
|
+
tone: stop.tone + direction * step * STATUS_TONE_STEP
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function clearsEverySurface(hex, side, surfaces) {
|
|
38
|
+
return TEXT_SURFACE_KEYS.every(
|
|
39
|
+
(key) => contrastRatio(hex, surfaces[key][side]) >= STATUS_TEXT_CONTRAST_MIN
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
function solve(key, side, surfaces) {
|
|
43
|
+
const { tone } = STATUS_STOP[key][side];
|
|
44
|
+
const maxSteps = Math.floor((side === "light" ? tone : 100 - tone) / STATUS_TONE_STEP);
|
|
45
|
+
for (let step = 0; step <= maxSteps; step++) {
|
|
46
|
+
const hex = statusHexAtStep(key, side, step);
|
|
47
|
+
if (clearsEverySurface(hex, side, surfaces)) return hex;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(
|
|
50
|
+
`status text: no ${side} ${key} tone reaches ${STATUS_TEXT_CONTRAST_MIN}:1 on every surface`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
function deriveStatusHexes(surfaces) {
|
|
54
|
+
const status = {};
|
|
55
|
+
for (const key of STATUS_KEYS) {
|
|
56
|
+
status[key] = { light: solve(key, "light", surfaces), dark: solve(key, "dark", surfaces) };
|
|
57
|
+
}
|
|
58
|
+
return status;
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
STATUS_HUE,
|
|
62
|
+
STATUS_STOP,
|
|
63
|
+
STATUS_TEXT_CONTRAST_MIN,
|
|
64
|
+
STATUS_TEXT_SURFACES,
|
|
65
|
+
STATUS_TONE_STEP,
|
|
66
|
+
deriveStatusHexes,
|
|
67
|
+
statusHexAtStep
|
|
68
|
+
};
|
package/llms.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# basalt-ui v2.
|
|
1
|
+
# basalt-ui v2.18.0
|
|
2
2
|
# Opinionated framework for Mantine-based React apps — theme, app shell, TanStack Charts, toolchain presets
|
|
3
3
|
#
|
|
4
4
|
# Entry points: use the exact import specifier below; do not guess.
|
|
@@ -13,13 +13,24 @@
|
|
|
13
13
|
# A subpath's `Install:` line is its required peers; `OptionalPeers:` are import()-only, install on use.
|
|
14
14
|
|
|
15
15
|
Package: basalt-ui
|
|
16
|
-
Version: 2.
|
|
16
|
+
Version: 2.18.0
|
|
17
17
|
|
|
18
18
|
## basalt-ui
|
|
19
19
|
Import: import { ... } from 'basalt-ui'
|
|
20
20
|
Description: Wiring (`BasaltProvider`, `createBasaltTheme`, `createBasaltRouter`, `unwrap`), shell + nav (`BasaltShell`, `PageBar`, `PageAside`, `PageTitle`, `ThemeToggle`, `defineNav`/`navGroup`/`navTarget`/`flattenNav`, `useBasaltNav`, `useSizeClass`, `useCoarsePointer`), dashboard (`Section`, `WidgetHeader`, `StatCard`, `StatGroup`, `DeltaBadge`, `EmptyState`, `SettingsSection`/`SettingsRow`/`DangerZone`). Reaches no subpath-required peer.
|
|
21
21
|
Layer: mantine-coupled
|
|
22
22
|
Rule: basalt-mantine
|
|
23
|
+
Signature: BarList(props: BarListProps): JSX.Element
|
|
24
|
+
className?: string
|
|
25
|
+
style?: CSSProperties
|
|
26
|
+
label: string
|
|
27
|
+
items: readonly BarListItem[]
|
|
28
|
+
format?: (value: number) => string
|
|
29
|
+
max?: number
|
|
30
|
+
accentLeader?: boolean
|
|
31
|
+
loading?: boolean
|
|
32
|
+
loadingRows?: number
|
|
33
|
+
emptyText?: string
|
|
23
34
|
Signature: BasaltProvider(props: BasaltProviderProps): JSX.Element
|
|
24
35
|
one of: router | children
|
|
25
36
|
theme?: PartialObjectDeep<MantineTheme, { recurseIntoArrays: false;…
|
|
@@ -80,6 +91,24 @@ Signature: EmptyState(props: EmptyStateProps): JSX.Element
|
|
|
80
91
|
description?: string
|
|
81
92
|
action?: ReactNode
|
|
82
93
|
tier?: "section" | "page"
|
|
94
|
+
Signature: Meter(props: MeterProps): JSX.Element
|
|
95
|
+
className?: string
|
|
96
|
+
style?: CSSProperties
|
|
97
|
+
label: string
|
|
98
|
+
min?: number
|
|
99
|
+
max?: number
|
|
100
|
+
format?: (value: number) => string
|
|
101
|
+
loading?: boolean
|
|
102
|
+
emptyText?: string
|
|
103
|
+
shape?: "bar" | "ring"
|
|
104
|
+
target?: number [with segments | zones]
|
|
105
|
+
startCaption?: ReactNode [with segments | zones]
|
|
106
|
+
endCaption?: ReactNode [with segments | zones]
|
|
107
|
+
value?: number | null [with zones]
|
|
108
|
+
color?: string
|
|
109
|
+
segments: readonly MeterSegment[]
|
|
110
|
+
zones: readonly MeterZone[]
|
|
111
|
+
forecastCaption?: string [with segments]
|
|
83
112
|
Signature: PageAside(props: PageAsideProps): ReactNode
|
|
84
113
|
className?: string
|
|
85
114
|
style?: CSSProperties
|
|
@@ -159,7 +188,6 @@ Signature: StatCard(props: StatCardProps): JSX.Element
|
|
|
159
188
|
classNames?: Partial<Record<StatCardSlot, string>>
|
|
160
189
|
icon?: ReactNode
|
|
161
190
|
title: string
|
|
162
|
-
subtitle?: string
|
|
163
191
|
info?: string
|
|
164
192
|
unit?: string
|
|
165
193
|
delta?: number
|
|
@@ -168,6 +196,10 @@ Signature: StatCard(props: StatCardProps): JSX.Element
|
|
|
168
196
|
deltaFormat?: (delta: number) => string
|
|
169
197
|
deltaGlyph?: boolean
|
|
170
198
|
value: string
|
|
199
|
+
qualifier?: string
|
|
200
|
+
subtitle?: string
|
|
201
|
+
loading?: boolean
|
|
202
|
+
emptyText?: string
|
|
171
203
|
breakdown?: readonly StatCardBreakdownRow[]
|
|
172
204
|
query?: QueryStateLike<unknown>
|
|
173
205
|
sparkline?: ReactNode | ((size: StatCardSparklineSize) => ReactNode)
|
|
@@ -423,7 +455,7 @@ Signature: Sparkline(props: SparklineProps): ReactElement<unknown, string | JSXE
|
|
|
423
455
|
data: readonly number[]
|
|
424
456
|
height: number
|
|
425
457
|
width?: number
|
|
426
|
-
mark?: "
|
|
458
|
+
mark?: "bar" | "line"
|
|
427
459
|
color?: string
|
|
428
460
|
emphasizeLast?: boolean
|
|
429
461
|
emphasisColor?: string
|
package/package.json
CHANGED
package/src/charts/Donut.tsx
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* container or wider, below it otherwise (`donut.module.css`, decided in CSS so the server HTML is
|
|
7
7
|
* the hydrated layout). No cursor: a ring has no shared axis.
|
|
8
8
|
*/
|
|
9
|
-
import { tooltip as tooltipHost } from '@tanstack/charts/tooltip'
|
|
10
9
|
import { portal } from '@tanstack/charts/tooltip/portal'
|
|
11
10
|
import { pie, polar, radialArc } from '@tanstack/charts/polar'
|
|
12
11
|
import type { ChartPoint, StoredChartSpec } from '@tanstack/charts/types/core'
|
|
@@ -15,6 +14,7 @@ import type { CSSProperties, ReactElement } from 'react'
|
|
|
15
14
|
import type { BasaltProps } from '../common/props'
|
|
16
15
|
import { alpha, VX } from '../tokens'
|
|
17
16
|
import type { CartesianDefinition } from './compile'
|
|
17
|
+
import { engineTooltip } from './cursor'
|
|
18
18
|
import { ChartEngine, idPrefixOf } from './host'
|
|
19
19
|
import { ChartLegend } from './legend'
|
|
20
20
|
import { useCoarsePointer } from '../common/use-media-query'
|
|
@@ -184,7 +184,7 @@ export function Donut<K extends string = string>(props: DonutProps<K>): ReactEle
|
|
|
184
184
|
theme: CHART_THEME,
|
|
185
185
|
}),
|
|
186
186
|
tooltip: {
|
|
187
|
-
use:
|
|
187
|
+
use: engineTooltip,
|
|
188
188
|
portal,
|
|
189
189
|
content: (points: readonly ChartPoint[]) => {
|
|
190
190
|
const slice = points[0]?.datum as Slice | undefined
|
package/src/charts/DualPanel.tsx
CHANGED
|
@@ -14,7 +14,7 @@ import { useCallback, useMemo } from 'react'
|
|
|
14
14
|
import type { ReactElement } from 'react'
|
|
15
15
|
import { VX, alpha } from '../tokens'
|
|
16
16
|
import { resolveContainerClass } from '../tokens/size-classes'
|
|
17
|
-
import {
|
|
17
|
+
import { defaultNumberFormat, engineRow, seriesRow, visibleSeries } from './compile'
|
|
18
18
|
import { barMark, emphasis, lineMark, markersOf, thinFill } from './marks'
|
|
19
19
|
import { PanelRung, chartHeightOf, paneX, paneY, rowsOf } from './panel'
|
|
20
20
|
import type { PaneBuilder, PanelInput, PanelPlan } from './panel'
|
|
@@ -182,7 +182,7 @@ export function DualPanel<T, X extends Date | number = Date | number>(
|
|
|
182
182
|
// A row's x as a fraction of the plot (the domain is padded half a step each side).
|
|
183
183
|
const fillAt = (v: Date | number): number =>
|
|
184
184
|
((v instanceof Date ? v.getTime() : v) - (lo - pad)) / (hi - lo + 2 * pad)
|
|
185
|
-
const format = (v: ChartValue): string =>
|
|
185
|
+
const format = formatX ? (v: ChartValue): string => formatX(v as X) : undefined
|
|
186
186
|
|
|
187
187
|
const visible = visibleSeries(lines, hidden)
|
|
188
188
|
const barOn = !hidden.has(BAR_KEY)
|
package/src/charts/Heatmap.tsx
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
import { colorGradientLegend } from '@tanstack/charts/legend'
|
|
11
11
|
import { cell } from '@tanstack/charts/rect'
|
|
12
12
|
import { scaleBand } from '@tanstack/charts/scales/band'
|
|
13
|
-
import { tooltip as tooltipHost } from '@tanstack/charts/tooltip'
|
|
14
13
|
import { portal } from '@tanstack/charts/tooltip/portal'
|
|
15
14
|
import type { ChartBuildContext, ChartPoint, StoredChartSpec } from '@tanstack/charts/types/core'
|
|
16
15
|
import { scaleQuantize } from 'd3-scale'
|
|
@@ -24,6 +23,7 @@ import type { CartesianDefinition } from './compile'
|
|
|
24
23
|
import { resolveNamedState } from './band-fold'
|
|
25
24
|
import type { NamedChartState } from './band-fold'
|
|
26
25
|
import { ABSENT_OPACITY, RAMP_LEVELS, RAMP_OPACITIES } from './heatmap-fill'
|
|
26
|
+
import { engineTooltip } from './cursor'
|
|
27
27
|
import { ChartEngine, idPrefixOf } from './host'
|
|
28
28
|
import { ChartLegend } from './legend'
|
|
29
29
|
import { useCoarsePointer } from '../common/use-media-query'
|
|
@@ -239,7 +239,7 @@ export function Heatmap<T, K extends string = string>(props: HeatmapProps<T, K>)
|
|
|
239
239
|
cell(group, { id, x: 'col', y: 'row', inset: CELL_INSET, key: 'id', ...paint })
|
|
240
240
|
const tooltipOptions = showTooltip
|
|
241
241
|
? {
|
|
242
|
-
use:
|
|
242
|
+
use: engineTooltip,
|
|
243
243
|
portal,
|
|
244
244
|
content: (points: readonly ChartPoint[]) => {
|
|
245
245
|
const point = points[0]
|
|
@@ -21,7 +21,7 @@ import { useCallback, useMemo } from 'react'
|
|
|
21
21
|
import type { ReactElement } from 'react'
|
|
22
22
|
import { VX, alpha } from '../tokens'
|
|
23
23
|
import { resolveContainerClass } from '../tokens/size-classes'
|
|
24
|
-
import {
|
|
24
|
+
import { defaultNumberFormat, seriesRow, visibleSeries } from './compile'
|
|
25
25
|
import { barMark, emphasis } from './marks'
|
|
26
26
|
import { PanelRung, chartHeightOf, paneX, paneY, rowsOf } from './panel'
|
|
27
27
|
import type { PaneBuilder, PanelInput, PanelPlan } from './panel'
|
|
@@ -191,7 +191,7 @@ export function MirroredBars<T, X extends ChartX = ChartX, K extends string = st
|
|
|
191
191
|
: VX.neutral
|
|
192
192
|
const upOn = visible.includes(upSeries)
|
|
193
193
|
const downOn = visible.includes(downSeries)
|
|
194
|
-
const format = (v: ChartValue): string =>
|
|
194
|
+
const format = formatX ? (v: ChartValue): string => formatX(v as X) : undefined
|
|
195
195
|
const upMax = paneMax(up, upSeries, drawn, upOn)
|
|
196
196
|
const downMax = paneMax(down, downSeries, drawn, downOn)
|
|
197
197
|
const bandWidth = 2 * model.halfStep * BAND_FILL
|
package/src/charts/Sparkline.tsx
CHANGED
|
@@ -34,9 +34,11 @@ export type SparklineProps = BasaltProps & {
|
|
|
34
34
|
mark?: 'line' | 'bar' | undefined
|
|
35
35
|
/** A `VX.*` ref or a series token. Default `VX.faint`. */
|
|
36
36
|
color?: string | undefined
|
|
37
|
-
/** Paints the last point in `emphasisColor` — the one reading a trend beside a KPI is for.
|
|
37
|
+
/** Paints the last point in `emphasisColor` — the one reading a trend beside a KPI is for.
|
|
38
|
+
* Default `true`; pass `false` for a bare line. */
|
|
38
39
|
emphasizeLast?: boolean | undefined
|
|
39
|
-
/** Default `VX.
|
|
40
|
+
/** Default `VX.line`, the line's own neutral, so a single metric stays neutral. Pass a status or
|
|
41
|
+
* trend colour to make the end dot carry a verdict. */
|
|
40
42
|
emphasisColor?: string | undefined
|
|
41
43
|
/** Pending / error / empty, the same contract as every other kind. The placeholder fills the
|
|
42
44
|
* sparkline's own `width` × `height`, so a loading tile does not shift when the trend lands. */
|
|
@@ -45,7 +47,7 @@ export type SparklineProps = BasaltProps & {
|
|
|
45
47
|
|
|
46
48
|
type Point = { i: number; v: number | null }
|
|
47
49
|
|
|
48
|
-
const LINE_WIDTH = 1.
|
|
50
|
+
const LINE_WIDTH = 1.5
|
|
49
51
|
const DOT_R = 2.5
|
|
50
52
|
const BAR_GAP = 0.15
|
|
51
53
|
// Pixel policy of the self-pooling (`poolSparkline`): the densest a series may draw.
|
|
@@ -249,8 +251,8 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
249
251
|
width,
|
|
250
252
|
mark = 'line',
|
|
251
253
|
color = VX.faint,
|
|
252
|
-
emphasizeLast =
|
|
253
|
-
emphasisColor = VX.
|
|
254
|
+
emphasizeLast = true,
|
|
255
|
+
emphasisColor = VX.line,
|
|
254
256
|
state,
|
|
255
257
|
className,
|
|
256
258
|
style,
|
package/src/charts/compile.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* binding, the tooltip content, and the container-class guide ladder.
|
|
6
6
|
* TanStack owns margins (measured), layout, focus and keyboard, the tooltip surface and SSR.
|
|
7
7
|
*/
|
|
8
|
+
import { dateTickFormat, monthName, pad2, utcAxisDate } from './date-tick-format'
|
|
8
9
|
import { crosshair } from '@tanstack/charts/crosshair'
|
|
9
10
|
import { decorative } from '@tanstack/charts/mark/decorative'
|
|
10
11
|
import { rect } from '@tanstack/charts/rect'
|
|
@@ -122,18 +123,11 @@ export function defaultNumberFormat(v: number): string {
|
|
|
122
123
|
return compact(v, { digits: Math.abs(v) >= 1000 ? 1 : 2 })
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
126
126
|
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
|
127
|
-
const pad2 = (n: number): string => String(n).padStart(2, '0')
|
|
128
|
-
|
|
129
|
-
/** `DD.MM`, read in UTC like the time scale that placed it (a local read shifts a day west of UTC). */
|
|
130
|
-
export function utcAxisDate(d: Date): string {
|
|
131
|
-
return `${pad2(d.getUTCDate())}.${pad2(d.getUTCMonth() + 1)}`
|
|
132
|
-
}
|
|
133
127
|
|
|
134
128
|
/** `Mon Sep 1 2026`, in UTC; a non-midnight instant adds `HH:MM`. */
|
|
135
129
|
export function utcHeaderDate(d: Date): string {
|
|
136
|
-
const date = `${DAYS[d.getUTCDay()]} ${
|
|
130
|
+
const date = `${DAYS[d.getUTCDay()]} ${monthName(d)} ${d.getUTCDate()} ${d.getUTCFullYear()}`
|
|
137
131
|
const ms = d.getTime() % DAY_MS
|
|
138
132
|
return ms === 0 ? date : `${date} ${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}`
|
|
139
133
|
}
|
|
@@ -319,18 +313,38 @@ export function strideTicks<V>(values: readonly V[], cls: ContainerClass): V[] {
|
|
|
319
313
|
return strideEvery(values, LADDER[cls].xTicks)
|
|
320
314
|
}
|
|
321
315
|
|
|
316
|
+
/**
|
|
317
|
+
* The one decision between a consumer's x format and the default: the consumer's wins; else dates
|
|
318
|
+
* read span-aware from the given tick values; else the plain default (numbers, strings, and a lone
|
|
319
|
+
* Date with no ticks to read a span from).
|
|
320
|
+
*/
|
|
321
|
+
export function xTickFormat(
|
|
322
|
+
consumer: ((v: ChartValue) => string) | undefined,
|
|
323
|
+
tickValues: readonly ChartValue[],
|
|
324
|
+
): (v: ChartValue) => string {
|
|
325
|
+
if (consumer) return consumer
|
|
326
|
+
if (tickValues.length === 0 || !tickValues.every((v): v is Date => v instanceof Date))
|
|
327
|
+
return defaultFormatX
|
|
328
|
+
const dateFormat = dateTickFormat(tickValues)
|
|
329
|
+
return (v) => (v instanceof Date ? dateFormat(v) : defaultFormatX(v))
|
|
330
|
+
}
|
|
331
|
+
|
|
322
332
|
export function xGuide<X extends ChartX>(opts: {
|
|
323
333
|
model: XScaleModel<X>
|
|
324
334
|
xTicks: number | readonly X[] | undefined
|
|
325
|
-
|
|
335
|
+
/** A consumer's formatter; absent, Date ticks get the span-aware {@link xTickFormat}. */
|
|
336
|
+
format?: ((v: ChartValue) => string) | undefined
|
|
326
337
|
cls: ContainerClass
|
|
327
338
|
/** The whole chart's height: decides whether the left margin is locked to zero. */
|
|
328
339
|
height: number
|
|
329
340
|
}): Omit<ChartPositionScaleOptions<ChartValue>, 'scale'> {
|
|
330
|
-
const { model, xTicks,
|
|
341
|
+
const { model, xTicks, cls, height } = opts
|
|
331
342
|
const ladder = LADDER[cls]
|
|
332
343
|
const inside = labelsInside(cls, height)
|
|
333
344
|
const ticks = xTickPolicy(model, xTicks ?? ladder.xTicks)
|
|
345
|
+
// A Date axis always resolves to `values` (the time scale has an extent; a band axis lists its
|
|
346
|
+
// buckets), so `{ count }` is only ever a number axis and never needs the date formatter.
|
|
347
|
+
const format = xTickFormat(opts.format, 'values' in ticks ? ticks.values : [])
|
|
334
348
|
// A continuous scale's domain start and span; a band scale pads its first slot, so none applies.
|
|
335
349
|
const [leftEdgeX, spanX] =
|
|
336
350
|
!model.band && model.extent
|
|
@@ -703,7 +717,13 @@ function buildScales<T, X extends ChartX>(opts: {
|
|
|
703
717
|
return {
|
|
704
718
|
x: {
|
|
705
719
|
scale: model.scale,
|
|
706
|
-
...xGuide({
|
|
720
|
+
...xGuide({
|
|
721
|
+
model,
|
|
722
|
+
xTicks: input.xTicks,
|
|
723
|
+
format: input.formatX ? formatX : undefined,
|
|
724
|
+
cls,
|
|
725
|
+
height,
|
|
726
|
+
}),
|
|
707
727
|
},
|
|
708
728
|
y: {
|
|
709
729
|
scale: yScaleFor(input.y, left.domain),
|
package/src/charts/cursor.ts
CHANGED
|
@@ -294,10 +294,55 @@ export function followerOptions(
|
|
|
294
294
|
return onFollow ? {} : ({ follower: { group, chartId } } satisfies TooltipFollowerOptions)
|
|
295
295
|
}
|
|
296
296
|
|
|
297
|
+
/** The engine host's hover readout element. */
|
|
298
|
+
export const TOOLTIP_READOUT = '.ts-chart-tooltip'
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The engine host stamps `role="status"` on its readout on every paint; a hover readout is a
|
|
302
|
+
* tooltip. `aria-live="polite"` stays, so a keyboard user still hears the reading. The host hands
|
|
303
|
+
* out no element: the readout is in the chart's container, except in the portal's fallback, which
|
|
304
|
+
* parks it on `<body>` — only then is the owner document searched (and every chart's readout
|
|
305
|
+
* stamped, which is right for each). Idempotent; a pinned custom body's `role="dialog"` is left
|
|
306
|
+
* alone.
|
|
307
|
+
*/
|
|
308
|
+
function stampTooltipRole(container: Element): void {
|
|
309
|
+
const own = container.querySelectorAll(TOOLTIP_READOUT)
|
|
310
|
+
const readouts = own.length > 0 ? own : container.ownerDocument.querySelectorAll(TOOLTIP_READOUT)
|
|
311
|
+
for (const el of readouts)
|
|
312
|
+
if (el.getAttribute('role') === 'status') el.setAttribute('role', 'tooltip')
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Decorates a tooltip host so every `paint` is followed by {@link stampTooltipRole}. The one
|
|
317
|
+
* stamping seam: `quietWhileFollowing` is built on it, and so is any chart that wires the engine host
|
|
318
|
+
* directly (Donut, Heatmap).
|
|
319
|
+
*/
|
|
320
|
+
export function withTooltipRole(extension: ChartTooltipExtension): ChartTooltipExtension {
|
|
321
|
+
return {
|
|
322
|
+
...extension,
|
|
323
|
+
create: (context) => {
|
|
324
|
+
const host = extension.create(context)
|
|
325
|
+
return {
|
|
326
|
+
...host,
|
|
327
|
+
paint: (paint) => {
|
|
328
|
+
host.paint(paint)
|
|
329
|
+
stampTooltipRole(context.container)
|
|
330
|
+
},
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The engine's tooltip host with its readout stamped, built once: a chart passes this as
|
|
338
|
+
* `tooltip.use`, so the extension keeps one identity across memo rebuilds.
|
|
339
|
+
*/
|
|
340
|
+
export const engineTooltip = withTooltipRole(tooltip)
|
|
341
|
+
|
|
297
342
|
/**
|
|
298
343
|
* The engine's DOM tooltip host, silent while this chart FOLLOWS another's cursor. A follower
|
|
299
344
|
* resolves the leader's focus group (so the engine draws the same dots at the shared x); the host
|
|
300
|
-
* would then paint a tooltip and fill its
|
|
345
|
+
* would then paint a tooltip and fill its live region. Whenever `isForeign` holds,
|
|
301
346
|
* this wrapper calls the host's `hide()` instead of `paint()`: a follower that has never led never
|
|
302
347
|
* creates the element, and one that has led keeps it in the DOM, hidden. Otherwise (leader, no
|
|
303
348
|
* `follower` option, or after the leader clears) it defers to the host.
|
|
@@ -309,9 +354,9 @@ export function followerOptions(
|
|
|
309
354
|
* re-check this wrapper, the live region included, on every `@tanstack/charts` upgrade.
|
|
310
355
|
*/
|
|
311
356
|
export const quietWhileFollowing: ChartTooltipExtension = {
|
|
312
|
-
...
|
|
357
|
+
...engineTooltip,
|
|
313
358
|
create: (context) => {
|
|
314
|
-
const host =
|
|
359
|
+
const host = engineTooltip.create(context)
|
|
315
360
|
let configured = false
|
|
316
361
|
let follower: TooltipFollowerOptions['follower']
|
|
317
362
|
return {
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default Date x tick labels: chosen from the TICK step (not the data step), read in UTC like
|
|
3
|
+
* the scale that placed them. A label may recur on one axis only behind a MARKER label (a day or a
|
|
4
|
+
* year) — `12:00, 18:00, 18.10, 06:00, 12:00` is fine, `17.10, 17.10, 18.10` is not. Each span has
|
|
5
|
+
* a ladder of levels, plainest first; the first level that holds the invariant wins. The last rung
|
|
6
|
+
* of every ladder names the whole date (or instant) of its tick, so the invariant always holds.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
10
|
+
export const pad2 = (n: number): string => String(n).padStart(2, '0')
|
|
11
|
+
|
|
12
|
+
export const monthName = (d: Date): string => MONTHS[d.getUTCMonth()]!
|
|
13
|
+
|
|
14
|
+
const DAY_MS = 86_400_000
|
|
15
|
+
const HOUR_MS = 3_600_000
|
|
16
|
+
/**
|
|
17
|
+
* Month ticks are 28–31 days apart; 27 days leaves slack for a consumer's uneven ticks while
|
|
18
|
+
* keeping a 2-week step on the day ladder.
|
|
19
|
+
*/
|
|
20
|
+
const MONTH_STEP_MS = 27 * DAY_MS
|
|
21
|
+
|
|
22
|
+
/** `DD.MM`, read in UTC like the time scale (a local read shifts a day west of UTC). */
|
|
23
|
+
export function utcAxisDate(d: Date): string {
|
|
24
|
+
return `${pad2(d.getUTCDate())}.${pad2(d.getUTCMonth() + 1)}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const utcClock = (d: Date): string => `${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}`
|
|
28
|
+
const utcClockSeconds = (d: Date): string => `${utcClock(d)}:${pad2(d.getUTCSeconds())}`
|
|
29
|
+
const utcClockMillis = (d: Date): string =>
|
|
30
|
+
`${utcClockSeconds(d)}.${String(d.getUTCMilliseconds()).padStart(3, '0')}`
|
|
31
|
+
/** Two digits: a widened day label stays 8 characters and still reads as the `DD.MM` family. */
|
|
32
|
+
const utcAxisDateYear = (d: Date): string =>
|
|
33
|
+
`${utcAxisDate(d)}.${String(d.getUTCFullYear()).slice(2)}`
|
|
34
|
+
|
|
35
|
+
const isMidnight = (t: number): boolean => t % DAY_MS === 0
|
|
36
|
+
const isJanuary = (d: Date): boolean => d.getUTCMonth() === 0
|
|
37
|
+
const dayOf = (t: number): number => Math.floor(t / DAY_MS)
|
|
38
|
+
const startsNewDay = (t: number, prev: number | undefined): boolean =>
|
|
39
|
+
prev === undefined || dayOf(t) !== dayOf(prev)
|
|
40
|
+
|
|
41
|
+
type Label = { text: string; marker: boolean }
|
|
42
|
+
/** One label per tick time (ascending), as a whole: a label may depend on its predecessor. */
|
|
43
|
+
type Level = (times: readonly number[]) => Label[]
|
|
44
|
+
|
|
45
|
+
const marker = (text: string): Label => ({ text, marker: true })
|
|
46
|
+
const plain = (text: string): Label => ({ text, marker: false })
|
|
47
|
+
const each =
|
|
48
|
+
(label: (t: number, d: Date, prev: number | undefined) => Label): Level =>
|
|
49
|
+
(times) =>
|
|
50
|
+
times.map((t, i) => label(t, new Date(t), times[i - 1]))
|
|
51
|
+
|
|
52
|
+
/** Under a day: `HH:MM`, a midnight tick carries the day. */
|
|
53
|
+
const subDayLevels: Level[] = [
|
|
54
|
+
each((t, d) => (isMidnight(t) ? marker(utcAxisDate(d)) : plain(utcClock(d)))),
|
|
55
|
+
// No midnight in reach: the first tick of each day carries its date instead.
|
|
56
|
+
each((t, d, prev) =>
|
|
57
|
+
isMidnight(t)
|
|
58
|
+
? marker(utcAxisDate(d))
|
|
59
|
+
: startsNewDay(t, prev)
|
|
60
|
+
? marker(`${utcAxisDate(d)} ${utcClock(d)}`)
|
|
61
|
+
: plain(utcClock(d)),
|
|
62
|
+
),
|
|
63
|
+
each((_, d) => marker(`${utcAxisDate(d)} ${utcClock(d)}`)),
|
|
64
|
+
// Sub-minute steps: the seconds, then (sub-second steps) the milliseconds. Every label names its
|
|
65
|
+
// whole instant to the millisecond, and `dateTickFormat` dedupes ticks to the millisecond, so
|
|
66
|
+
// the last rung cannot repeat.
|
|
67
|
+
each((_, d) => marker(`${utcAxisDate(d)} ${utcClockSeconds(d)}`)),
|
|
68
|
+
each((_, d) => marker(`${utcAxisDate(d)} ${utcClockMillis(d)}`)),
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A day or more: `DD.MM`. Ticks are at least a day apart here, so they fall on different UTC days
|
|
73
|
+
* and the last rung, which names the full date, cannot repeat.
|
|
74
|
+
*/
|
|
75
|
+
const dayLevels: Level[] = [
|
|
76
|
+
each((_, d) => marker(utcAxisDate(d))),
|
|
77
|
+
each((_, d) => marker(utcAxisDateYear(d))),
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
/** A month or more: the month, a January tick carries the year. */
|
|
81
|
+
const monthLevels: Level[] = [
|
|
82
|
+
each((_, d) => (isJanuary(d) ? marker(String(d.getUTCFullYear())) : plain(monthName(d)))),
|
|
83
|
+
each((_, d) => marker(`${monthName(d)} ${d.getUTCFullYear()}`)),
|
|
84
|
+
// Ticks a month apart in name only (Jan 1 and Jan 31): fall to the day ladder (ticks here are
|
|
85
|
+
// >= 27 days apart, so its full-date last rung is unique).
|
|
86
|
+
...dayLevels,
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
/** A marker label is unique on the axis; any other label repeats only after a marker. */
|
|
90
|
+
function holdsInvariant(labels: readonly Label[]): boolean {
|
|
91
|
+
const markers = new Set<string>()
|
|
92
|
+
const since = new Set<string>()
|
|
93
|
+
for (const { text, marker: isMarker } of labels) {
|
|
94
|
+
if (isMarker ? markers.has(text) : since.has(text)) return false
|
|
95
|
+
if (isMarker) {
|
|
96
|
+
markers.add(text)
|
|
97
|
+
since.clear()
|
|
98
|
+
} else since.add(text)
|
|
99
|
+
}
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The first level whose labels hold the invariant, else the last (see each ladder's last rung). */
|
|
104
|
+
function pickLabels(levels: readonly Level[], times: readonly number[]): Label[] {
|
|
105
|
+
let labels: Label[] = []
|
|
106
|
+
for (const level of levels) {
|
|
107
|
+
labels = level(times)
|
|
108
|
+
if (holdsInvariant(labels)) return labels
|
|
109
|
+
}
|
|
110
|
+
return labels
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The tick step: the smallest gap between ticks. One tick has no gap, so it takes the span its own
|
|
115
|
+
* instant implies: a midnight is a day, anything else is read as a clock time.
|
|
116
|
+
*/
|
|
117
|
+
function stepOf(times: readonly number[]): number {
|
|
118
|
+
if (times.length === 1) return isMidnight(times[0]!) ? DAY_MS : HOUR_MS
|
|
119
|
+
return times.reduce((min, t, i) => (i === 0 ? min : Math.min(min, t - times[i - 1]!)), Infinity)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A formatter for one tick set. A Date that is not among the ticks (a hover, a consumer reading
|
|
124
|
+
* the axis format) reads as plain `DD.MM`: its position in the set is undefined.
|
|
125
|
+
*/
|
|
126
|
+
export function dateTickFormat(ticks: readonly Date[]): (d: Date) => string {
|
|
127
|
+
const times = [...new Set(ticks.map((t) => t.getTime()))].toSorted((a, b) => a - b)
|
|
128
|
+
const step = stepOf(times)
|
|
129
|
+
const levels = step < DAY_MS ? subDayLevels : step < MONTH_STEP_MS ? dayLevels : monthLevels
|
|
130
|
+
const text = new Map(pickLabels(levels, times).map((l, i) => [times[i]!, l.text]))
|
|
131
|
+
return (d) => text.get(d.getTime()) ?? utcAxisDate(d)
|
|
132
|
+
}
|
package/src/charts/panel.tsx
CHANGED
|
@@ -20,7 +20,7 @@ import { useCallback } from 'react'
|
|
|
20
20
|
import type { ReactElement } from 'react'
|
|
21
21
|
import { ChartShell } from './CartesianChart'
|
|
22
22
|
import type { ChartShellProps, ShellContext } from './CartesianChart'
|
|
23
|
-
import {
|
|
23
|
+
import { engineRow, utcHeaderDate, visibleSeries, xGuide, xTickFormat, yGuide } from './compile'
|
|
24
24
|
import type { CartesianDefinition } from './compile'
|
|
25
25
|
import { cursorBinding, followerOptions, quietWhileFollowing } from './cursor'
|
|
26
26
|
import { plotAccess } from './plot-context'
|
|
@@ -110,7 +110,8 @@ type ChartScale = NonNullable<StoredChartSpec['scales'][string]>['scale']
|
|
|
110
110
|
export const paneX = <X extends ChartX>(opts: {
|
|
111
111
|
scale: ChartScale
|
|
112
112
|
model: XScaleModel<X>
|
|
113
|
-
format
|
|
113
|
+
/** A consumer's x format; absent, the span-aware default ({@link xTickFormat}). */
|
|
114
|
+
format?: ((v: ChartValue) => string) | undefined
|
|
114
115
|
cls: ContainerClass
|
|
115
116
|
/** The whole chart's height ({@link chartHeightOf}). */
|
|
116
117
|
height: number
|
|
@@ -204,10 +205,11 @@ function panelDefinition<T, X extends ChartX>(
|
|
|
204
205
|
const owned = new Set(series.map((s) => s.key))
|
|
205
206
|
const visible = visibleSeries(series, hidden)
|
|
206
207
|
const hiddenSeries = series.filter((s) => !visible.includes(s))
|
|
208
|
+
const headerFormat = xTickFormat(formatX ? (v: ChartValue) => formatX(v as X) : undefined, [])
|
|
207
209
|
const header = (v: ChartValue, d: T): string => {
|
|
208
210
|
if (tooltip?.formatHeader) return tooltip.formatHeader(v as X, d)
|
|
209
211
|
if (v instanceof Date && !formatX) return utcHeaderDate(v)
|
|
210
|
-
return
|
|
212
|
+
return headerFormat(v)
|
|
211
213
|
}
|
|
212
214
|
const definition: CartesianDefinition = {
|
|
213
215
|
chart: () => composed,
|
package/src/charts/types.ts
CHANGED
|
@@ -303,7 +303,11 @@ export type CartesianChartProps<T, X extends ChartX = ChartX> = BasaltProps & {
|
|
|
303
303
|
* extent (and warns in dev). A no-op on a string or band x (`Bars`, `MirroredBars`, `BandStrip`).
|
|
304
304
|
*/
|
|
305
305
|
xDomain?: readonly [X, X] | undefined
|
|
306
|
-
/**
|
|
306
|
+
/**
|
|
307
|
+
* Tick label for an x value. Default: compact numbers, strings verbatim, and dates by tick step
|
|
308
|
+
* (`HH:MM` with the day on midnight, `DD.MM`, a month with the year on January; a label recurs
|
|
309
|
+
* only behind a day or year label).
|
|
310
|
+
*/
|
|
307
311
|
formatX?: ((x: X) => string) | undefined
|
|
308
312
|
zones?: readonly ZoneSpec[] | undefined
|
|
309
313
|
xZones?: readonly XZoneSpec<X>[] | undefined
|