basalt-ui 2.9.1 → 2.11.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 +61 -38
- package/MIGRATING.md +34 -12
- package/agent/rules/basalt-charts.md +2 -2
- package/agent/skills/basalt-charts/SKILL.md +16 -17
- package/dist/charts/BandStrip.d.ts +9 -6
- package/dist/charts/BandStrip.d.ts.map +1 -1
- package/dist/charts/BandStrip.js +8 -8
- package/dist/charts/CartesianChart.d.ts +3 -0
- package/dist/charts/CartesianChart.d.ts.map +1 -1
- package/dist/charts/CartesianChart.js +13 -6
- package/dist/charts/DualPanel.d.ts.map +1 -1
- package/dist/charts/DualPanel.js +20 -15
- package/dist/charts/Heatmap.d.ts +33 -3
- package/dist/charts/Heatmap.d.ts.map +1 -1
- package/dist/charts/Heatmap.js +111 -68
- package/dist/charts/MirroredBars.d.ts +16 -5
- package/dist/charts/MirroredBars.d.ts.map +1 -1
- package/dist/charts/MirroredBars.js +46 -9
- package/dist/charts/Sparkline.d.ts +12 -0
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +68 -5
- package/dist/charts/band-fold.d.ts +13 -0
- package/dist/charts/band-fold.d.ts.map +1 -1
- package/dist/charts/band-fold.js +11 -1
- package/dist/charts/compile.d.ts +4 -1
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +22 -14
- package/dist/charts/heatmap-fill.d.ts +15 -0
- package/dist/charts/heatmap-fill.d.ts.map +1 -0
- package/dist/charts/heatmap-fill.js +13 -0
- package/dist/charts/host.d.ts +5 -1
- package/dist/charts/host.d.ts.map +1 -1
- package/dist/charts/host.js +37 -1
- package/dist/charts/index.d.ts +1 -1
- package/dist/charts/index.d.ts.map +1 -1
- package/dist/charts/legend.d.ts +2 -0
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +14 -10
- package/dist/charts/marks.d.ts +1 -1
- package/dist/charts/marks.d.ts.map +1 -1
- package/dist/charts/marks.js +28 -13
- package/dist/charts/panel.d.ts +7 -1
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +9 -4
- package/dist/charts/plot-context.d.ts +42 -0
- package/dist/charts/plot-context.d.ts.map +1 -0
- package/dist/charts/plot-context.js +64 -0
- package/dist/charts/scales.d.ts +3 -1
- package/dist/charts/scales.d.ts.map +1 -1
- package/dist/charts/scales.js +23 -5
- package/dist/charts/types.d.ts +36 -4
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/controls/enum-filter.d.ts +4 -1
- package/dist/controls/enum-filter.d.ts.map +1 -1
- package/dist/controls/enum-filter.js +2 -1
- package/dist/controls/number-filter.d.ts.map +1 -1
- package/dist/controls/number-filter.js +2 -0
- package/dist/tokens/core.d.ts +6 -0
- package/dist/tokens/core.d.ts.map +1 -1
- package/dist/tokens/core.js +5 -3
- package/llms.txt +17 -8
- package/package.json +1 -1
- package/src/charts/BandStrip.tsx +19 -20
- package/src/charts/CartesianChart.tsx +16 -4
- package/src/charts/DualPanel.tsx +21 -16
- package/src/charts/Heatmap.tsx +181 -76
- package/src/charts/MirroredBars.tsx +71 -18
- package/src/charts/Sparkline.tsx +106 -5
- package/src/charts/band-fold.ts +32 -0
- package/src/charts/compile.ts +26 -14
- package/src/charts/heatmap-fill.ts +19 -0
- package/src/charts/host.tsx +59 -0
- package/src/charts/index.ts +1 -1
- package/src/charts/legend.tsx +21 -12
- package/src/charts/marks.ts +31 -14
- package/src/charts/panel.tsx +18 -5
- package/src/charts/plot-context.ts +109 -0
- package/src/charts/scales.ts +33 -5
- package/src/charts/types.ts +36 -4
- package/src/controls/enum-filter.tsx +5 -1
- package/src/controls/number-filter.tsx +6 -1
- package/src/tokens/core.ts +10 -3
package/src/charts/Sparkline.tsx
CHANGED
|
@@ -48,6 +48,11 @@ type Point = { i: number; v: number | null }
|
|
|
48
48
|
const LINE_WIDTH = 1.6
|
|
49
49
|
const DOT_R = 2.5
|
|
50
50
|
const BAR_GAP = 0.15
|
|
51
|
+
// Pixel policy of the self-pooling (`poolSparkline`): the densest a series may draw.
|
|
52
|
+
const LINE_PX_PER_POINT = 1
|
|
53
|
+
const BAR_PX_PER_BAR = 2
|
|
54
|
+
/** First, last and one min/max pair: the fewest points a pooled line can hold. */
|
|
55
|
+
const LINE_POOL_MIN = 4
|
|
51
56
|
|
|
52
57
|
const finite = (v: number): number | null => (Number.isFinite(v) ? v : null)
|
|
53
58
|
|
|
@@ -63,16 +68,112 @@ function extent(rows: readonly Point[]): readonly [number, number] | null {
|
|
|
63
68
|
return lo <= hi ? [lo, hi] : null
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
/** A row with its value proven finite, so no cast carries it through the pooling. */
|
|
72
|
+
type Sample = { row: Point; v: number }
|
|
73
|
+
const sampleOf = (row: Point): Sample | null => (row.v === null ? null : { row, v: row.v })
|
|
74
|
+
|
|
75
|
+
/** `rows` cut into `count` consecutive buckets of near-equal length (empty ones dropped). */
|
|
76
|
+
function buckets(rows: readonly Point[], count: number): Point[][] {
|
|
77
|
+
const size = rows.length / count
|
|
78
|
+
return Array.from({ length: count }, (_, b) =>
|
|
79
|
+
rows.slice(Math.round(b * size), Math.round((b + 1) * size)),
|
|
80
|
+
).filter((bucket) => bucket.length > 0)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One maximal stretch of finite rows inside a bucket. */
|
|
84
|
+
type Run = { first: Sample; lo: Sample; hi: Sample; last: Sample; afterGap: boolean }
|
|
85
|
+
|
|
86
|
+
/** A run's rows in index order: its min and max, plus the edge that touches a gap, so the gap
|
|
87
|
+
* stays exactly where the input had it. */
|
|
88
|
+
function runRows(run: Run, beforeGap: boolean): Point[] {
|
|
89
|
+
const keep = [
|
|
90
|
+
run.lo,
|
|
91
|
+
run.hi,
|
|
92
|
+
...(run.afterGap ? [run.first] : []),
|
|
93
|
+
...(beforeGap ? [run.last] : []),
|
|
94
|
+
]
|
|
95
|
+
const byIndex = new Map(keep.map((k) => [k.row.i, k.row]))
|
|
96
|
+
return [...byIndex.values()].toSorted((a, b) => a.i - b.i)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A line, pooled: the first and last rows, and each bucket's min and max between them. A null
|
|
101
|
+
* row is a gap and is never pooled across: a run is closed at every null, one null row (at the
|
|
102
|
+
* gap's first index) stands for the whole gap, and the finite rows beside it are kept.
|
|
103
|
+
*/
|
|
104
|
+
function poolLine(rows: readonly Point[], cap: number): readonly Point[] {
|
|
105
|
+
const first = rows[0]
|
|
106
|
+
const last = rows.at(-1)
|
|
107
|
+
if (first === undefined || last === undefined) return rows
|
|
108
|
+
if (cap < LINE_POOL_MIN) return [first, last]
|
|
109
|
+
const out: Point[] = [first]
|
|
110
|
+
const endsInGap = (): boolean => out.at(-1)?.v === null
|
|
111
|
+
for (const bucket of buckets(rows.slice(1, -1), Math.floor(cap / 2) - 1)) {
|
|
112
|
+
let run: Run | undefined
|
|
113
|
+
for (const row of bucket) {
|
|
114
|
+
const s = sampleOf(row)
|
|
115
|
+
if (s === null) {
|
|
116
|
+
if (run !== undefined) out.push(...runRows(run, true))
|
|
117
|
+
run = undefined
|
|
118
|
+
if (!endsInGap()) out.push(row)
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
run =
|
|
122
|
+
run === undefined
|
|
123
|
+
? { first: s, lo: s, hi: s, last: s, afterGap: endsInGap() }
|
|
124
|
+
: { ...run, lo: s.v < run.lo.v ? s : run.lo, hi: s.v > run.hi.v ? s : run.hi, last: s }
|
|
125
|
+
}
|
|
126
|
+
if (run !== undefined) out.push(...runRows(run, false))
|
|
127
|
+
}
|
|
128
|
+
return last.v === null && endsInGap() ? out : [...out, last]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Bars, pooled: the first and last rows and one bar per bucket between them, the value furthest
|
|
132
|
+
* from zero; a bucket of nulls is one null bar. Re-indexed 0..n-1 (a band x). */
|
|
133
|
+
function poolBar(rows: readonly Point[], cap: number): readonly Point[] {
|
|
134
|
+
const first = rows[0]
|
|
135
|
+
const last = rows.at(-1)
|
|
136
|
+
if (first === undefined || last === undefined) return rows
|
|
137
|
+
const furthest = (group: readonly Point[]): number | null =>
|
|
138
|
+
group.reduce<number | null>((best, r) => {
|
|
139
|
+
if (r.v === null) return best
|
|
140
|
+
return best === null || Math.abs(r.v) > Math.abs(best) ? r.v : best
|
|
141
|
+
}, null)
|
|
142
|
+
if (cap < 2) return [{ i: 0, v: furthest(rows) }]
|
|
143
|
+
const values =
|
|
144
|
+
cap === 2
|
|
145
|
+
? [first.v, last.v]
|
|
146
|
+
: [first.v, ...buckets(rows.slice(1, -1), cap - 2).map(furthest), last.v]
|
|
147
|
+
return values.map((v, i) => ({ i, v }))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Pools a series to what `width` px can show, so the caller never sizes it: at most one point
|
|
152
|
+
* per `LINE_PX_PER_POINT` (min/max-preserving) or one bar per `BAR_PX_PER_BAR`. A series at or
|
|
153
|
+
* under its cap is returned as is, so a short series draws exactly as before; `width <= 0`
|
|
154
|
+
* (unmeasured) pools nothing. A line is never pooled below its two endpoints.
|
|
155
|
+
*/
|
|
156
|
+
export function poolSparkline(
|
|
67
157
|
rows: readonly Point[],
|
|
158
|
+
width: number,
|
|
159
|
+
mark: 'line' | 'bar',
|
|
160
|
+
): readonly Point[] {
|
|
161
|
+
const cap = Math.floor(width / (mark === 'bar' ? BAR_PX_PER_BAR : LINE_PX_PER_POINT))
|
|
162
|
+
if (width <= 0 || rows.length <= Math.max(cap, mark === 'bar' ? 1 : 2)) return rows
|
|
163
|
+
return mark === 'bar' ? poolBar(rows, cap) : poolLine(rows, cap)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function compileSparkline(
|
|
167
|
+
source: readonly Point[],
|
|
68
168
|
[lo, hi]: readonly [number, number],
|
|
69
169
|
mark: 'line' | 'bar',
|
|
70
170
|
color: string,
|
|
71
171
|
emphasisColor: string | null,
|
|
72
172
|
): CartesianDefinition {
|
|
73
|
-
const last = rows.at(-1)
|
|
74
173
|
return {
|
|
75
|
-
chart: () => {
|
|
174
|
+
chart: ({ width }) => {
|
|
175
|
+
const rows = poolSparkline(source, width, mark)
|
|
176
|
+
const last = rows.at(-1)
|
|
76
177
|
if (mark === 'bar') {
|
|
77
178
|
return {
|
|
78
179
|
marks: [
|
|
@@ -104,7 +205,7 @@ function compileSparkline(
|
|
|
104
205
|
// A flat or single-point series needs an extent; 10% air keeps the stroke inside the box.
|
|
105
206
|
const pad = (hi - lo) * 0.1 || 1
|
|
106
207
|
// Half a dot of air at each end, in index units, so the last-point dot is not clipped.
|
|
107
|
-
const xPad = emphasisColor === null ? 0 : (
|
|
208
|
+
const xPad = emphasisColor === null ? 0 : (source.length - 1) * 0.04 || 0.5
|
|
108
209
|
return {
|
|
109
210
|
marks: [
|
|
110
211
|
lineY(rows, {
|
|
@@ -125,7 +226,7 @@ function compileSparkline(
|
|
|
125
226
|
],
|
|
126
227
|
scales: {
|
|
127
228
|
x: {
|
|
128
|
-
scale: scaleLinear().domain([-xPad, Math.max(
|
|
229
|
+
scale: scaleLinear().domain([-xPad, Math.max(source.length - 1, 1) + xPad]),
|
|
129
230
|
axis: false as const,
|
|
130
231
|
},
|
|
131
232
|
y: { scale: scaleLinear().domain([lo - pad, hi + pad]), axis: false as const },
|
package/src/charts/band-fold.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
import { oneOf } from '../common/errors'
|
|
2
|
+
import type { ChartSeries } from './types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The fields a named state carries wherever one is declared (`BandStrip`'s `states`, `Heatmap`'s):
|
|
6
|
+
* the key, its legend label and color, its fill opacity, and the legend / tooltip / note switches.
|
|
7
|
+
* One base so a field means the same thing on every kind.
|
|
8
|
+
*/
|
|
9
|
+
export type NamedChartState = Pick<
|
|
10
|
+
ChartSeries<never>,
|
|
11
|
+
'key' | 'label' | 'color' | 'tooltip' | 'legend' | 'note' | 'fillOpacity'
|
|
12
|
+
>
|
|
13
|
+
|
|
1
14
|
/** A run of adjacent slots drawn as one band; `items` are the source data it stands for. */
|
|
2
15
|
export type Group<T> = { start: number; items: T[] }
|
|
3
16
|
|
|
@@ -24,3 +37,22 @@ export function foldBands<T>(data: readonly T[], cap: number, merge: (group: T[]
|
|
|
24
37
|
if (data.length <= cap) return [...data]
|
|
25
38
|
return groupsOf(data, cap).map((g) => merge(g.items))
|
|
26
39
|
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Resolves a prop that names a `series` / `states` key (`absentState`): the key when it names an
|
|
43
|
+
* entry, `undefined` when it is unset. A key naming nothing throws in dev, naming the valid set;
|
|
44
|
+
* in production the chart degrades to the unset behaviour.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveNamedState<K extends string>(
|
|
47
|
+
component: string,
|
|
48
|
+
prop: string,
|
|
49
|
+
requested: K | undefined,
|
|
50
|
+
known: readonly string[],
|
|
51
|
+
): K | undefined {
|
|
52
|
+
if (requested === undefined) return undefined
|
|
53
|
+
if (known.includes(requested)) return requested
|
|
54
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
55
|
+
throw new Error(oneOf(component, prop, [...known], requested))
|
|
56
|
+
}
|
|
57
|
+
return undefined
|
|
58
|
+
}
|
package/src/charts/compile.ts
CHANGED
|
@@ -29,6 +29,8 @@ import { VX } from '../tokens'
|
|
|
29
29
|
import type { ContainerClass } from '../tokens/size-classes'
|
|
30
30
|
import { cursorBinding, sourceOnlyFocus } from './cursor'
|
|
31
31
|
import { cursorDots } from './marks'
|
|
32
|
+
import { MAIN_PANE, plotAccess, plotContext } from './plot-context'
|
|
33
|
+
import type { PlotSizes } from './plot-context'
|
|
32
34
|
import { resolveContainerClass } from '../tokens/size-classes'
|
|
33
35
|
import { resolveYDomain, xScaleFor, xZoneFractions, yScaleFor } from './scales'
|
|
34
36
|
import type { XScaleModel } from './scales'
|
|
@@ -62,6 +64,7 @@ export type CartesianInput<T, X extends ChartX> = Pick<
|
|
|
62
64
|
| 'y'
|
|
63
65
|
| 'y2'
|
|
64
66
|
| 'xTicks'
|
|
67
|
+
| 'xDomain'
|
|
65
68
|
| 'formatX'
|
|
66
69
|
| 'zones'
|
|
67
70
|
| 'xZones'
|
|
@@ -84,6 +87,8 @@ export type CartesianInput<T, X extends ChartX> = Pick<
|
|
|
84
87
|
reverseRows?: boolean
|
|
85
88
|
/** Bar kinds: how the left-axis `mark: 'bar'` series combine for the auto domain. */
|
|
86
89
|
stack?: 'stacked' | 'normalized'
|
|
90
|
+
/** The plot area the engine last laid out; `null` before the first layout (the frame stands in). */
|
|
91
|
+
plot?: PlotSizes | null
|
|
87
92
|
}
|
|
88
93
|
|
|
89
94
|
/** Font size and tick counts per container class (§ 9.6). */
|
|
@@ -755,7 +760,7 @@ function cursorDotMarks<T, X extends ChartX>(
|
|
|
755
760
|
? []
|
|
756
761
|
: [{ x: input.x(d), y, series: s.key, datum: d }]
|
|
757
762
|
})
|
|
758
|
-
return
|
|
763
|
+
return cursorDots(s, rows, axes.onRight(s) ? 'y2' : undefined)
|
|
759
764
|
})
|
|
760
765
|
}
|
|
761
766
|
|
|
@@ -827,7 +832,7 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
827
832
|
const visible = visibleSeries(input.series, input.hidden ?? new Set<string>())
|
|
828
833
|
const visibleKeys = new Set(visible.map((s) => s.key))
|
|
829
834
|
const keys = new Set(input.series.map((s) => s.key))
|
|
830
|
-
const model = xScaleFor(input.data.map(input.x), input.band === true)
|
|
835
|
+
const model = xScaleFor(input.data.map(input.x), input.band === true, input.xDomain)
|
|
831
836
|
const axes = resolveAxes(input, visible)
|
|
832
837
|
const under = underlayMarks(input, model, axes)
|
|
833
838
|
const over = refLineMarks(input, axes)
|
|
@@ -839,20 +844,26 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
839
844
|
s.area ? [seriesAreaGradient(s, i, input, axes)] : [],
|
|
840
845
|
)
|
|
841
846
|
|
|
842
|
-
|
|
847
|
+
const definition: CartesianDefinition = {
|
|
843
848
|
chart: ({ width, height }: ChartBuildContext): StoredChartSpec => {
|
|
844
849
|
const cls = resolveContainerClass(width)
|
|
845
|
-
const kindMarks = input.marks(
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
850
|
+
const kindMarks = input.marks(
|
|
851
|
+
plotContext(
|
|
852
|
+
{
|
|
853
|
+
rows,
|
|
854
|
+
visible,
|
|
855
|
+
highlighted: input.highlighted ?? null,
|
|
856
|
+
containerClass: cls,
|
|
857
|
+
width,
|
|
858
|
+
height,
|
|
859
|
+
data: input.data,
|
|
860
|
+
y2: axes.right ? 'y2' : undefined,
|
|
861
|
+
yDomain: axes.left.domain,
|
|
862
|
+
y2Domain: axes.right?.domain,
|
|
863
|
+
},
|
|
864
|
+
plotAccess(definition, input.plot, MAIN_PANE),
|
|
865
|
+
),
|
|
866
|
+
)
|
|
856
867
|
return {
|
|
857
868
|
marks: [...under, ...kindMarks, ...dots, ...over, crosshair({ x: CROSSHAIR, y: false })],
|
|
858
869
|
scales: buildScales({
|
|
@@ -888,4 +899,5 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
888
899
|
? false
|
|
889
900
|
: tooltipSpec({ input, axes, visible, keys, visibleKeys, formatX }),
|
|
890
901
|
}
|
|
902
|
+
return definition
|
|
891
903
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `Heatmap` fills as numbers: the ramp's floor and the flat absent fill are tuned TOGETHER,
|
|
3
|
+
* because a near-baseline measured hour must never read as an unmeasured one. `heatmap-fill.test.ts`
|
|
4
|
+
* holds the pair to a ΔE*ab floor against every ramp step in both schemes — retune here, not at a
|
|
5
|
+
* call site.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Intensity steps: quantized, so the gradient legend and the cells are the same ramp. */
|
|
9
|
+
export const RAMP_LEVELS = 8
|
|
10
|
+
/** Opacity of the lowest ramp step. */
|
|
11
|
+
export const RAMP_FLOOR = 0.28
|
|
12
|
+
/** Opacity of the flat neutral fill of an absent cell. */
|
|
13
|
+
export const ABSENT_OPACITY = 0.08
|
|
14
|
+
|
|
15
|
+
/** The ramp's opacities, lowest to full ink. */
|
|
16
|
+
export const RAMP_OPACITIES: readonly number[] = Array.from(
|
|
17
|
+
{ length: RAMP_LEVELS },
|
|
18
|
+
(_, i) => RAMP_FLOOR + ((1 - RAMP_FLOOR) * i) / (RAMP_LEVELS - 1),
|
|
19
|
+
)
|
package/src/charts/host.tsx
CHANGED
|
@@ -7,8 +7,13 @@
|
|
|
7
7
|
* box is final in the server HTML and hydration only redraws the svg inside it (W10).
|
|
8
8
|
*/
|
|
9
9
|
import { Chart } from '@tanstack/charts/react'
|
|
10
|
+
import type { ChartProps } from '@tanstack/charts/react'
|
|
11
|
+
import type { ChartValue } from '@tanstack/charts/types/core'
|
|
12
|
+
import { useEffect, useMemo } from 'react'
|
|
10
13
|
import type { CSSProperties, ReactElement } from 'react'
|
|
11
14
|
import type { CartesianDefinition } from './compile'
|
|
15
|
+
import { MAIN_PANE, measuredPlots, readsPlot, recordPlots, watchPlotReads } from './plot-context'
|
|
16
|
+
import type { PlotSize, PlotSizes } from './plot-context'
|
|
12
17
|
import { ChartBox } from './box'
|
|
13
18
|
import type { ChartBoxHeight } from './box'
|
|
14
19
|
import { INITIAL_WIDTH } from './sizing'
|
|
@@ -22,8 +27,14 @@ export type ChartEngineProps = {
|
|
|
22
27
|
idPrefix: string
|
|
23
28
|
/** `touch-action: pan-y`: a vertical swipe across the chart still scrolls the page. */
|
|
24
29
|
coarse: boolean
|
|
30
|
+
/** The laid-out plot area of every pane after each layout, in px; fires only for a definition
|
|
31
|
+
* whose `marks` read it (and once more for a read that comes after the measurement). */
|
|
32
|
+
onPlot?: (sizes: PlotSizes) => void
|
|
25
33
|
}
|
|
26
34
|
|
|
35
|
+
/** The engine props of a basalt definition (`CartesianDefinition`'s datum and value types). */
|
|
36
|
+
type EngineProps = ChartProps<unknown, ChartValue, number>
|
|
37
|
+
|
|
27
38
|
/** `currentColor` is the axis ink. The font is pinned to mono (the 1.x tick look) and inherited by
|
|
28
39
|
* the svg — TanStack measures guides with the host's computed font. */
|
|
29
40
|
const HOST_STYLE: CSSProperties = {
|
|
@@ -40,7 +51,30 @@ export function ChartEngine({
|
|
|
40
51
|
height,
|
|
41
52
|
idPrefix,
|
|
42
53
|
coarse,
|
|
54
|
+
onPlot,
|
|
43
55
|
}: ChartEngineProps): ReactElement {
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!onPlot) return
|
|
58
|
+
watchPlotReads(definition, () => {
|
|
59
|
+
// A microtask: the read happens inside the engine's build, which may sit in a React render.
|
|
60
|
+
queueMicrotask(() => {
|
|
61
|
+
const sizes = measuredPlots(definition)
|
|
62
|
+
if (sizes) onPlot(sizes)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
return () => watchPlotReads(definition, undefined)
|
|
66
|
+
}, [definition, onPlot])
|
|
67
|
+
// Stable, so the engine does not re-register its render hook on every chart render.
|
|
68
|
+
const onRender = useMemo<EngineProps['onRender']>(
|
|
69
|
+
() =>
|
|
70
|
+
onPlot &&
|
|
71
|
+
((context) => {
|
|
72
|
+
const sizes = measurePlots(context)
|
|
73
|
+
recordPlots(definition, sizes)
|
|
74
|
+
if (readsPlot(definition)) onPlot(sizes)
|
|
75
|
+
}),
|
|
76
|
+
[onPlot, definition],
|
|
77
|
+
)
|
|
44
78
|
return (
|
|
45
79
|
<ChartBox height={height}>
|
|
46
80
|
<Chart
|
|
@@ -49,6 +83,7 @@ export function ChartEngine({
|
|
|
49
83
|
initialWidth={INITIAL_WIDTH}
|
|
50
84
|
idPrefix={idPrefix}
|
|
51
85
|
className="basalt-chart"
|
|
86
|
+
{...(onRender && { onRender })}
|
|
52
87
|
style={coarse ? COARSE_STYLE : HOST_STYLE}
|
|
53
88
|
/>
|
|
54
89
|
</ChartBox>
|
|
@@ -58,3 +93,27 @@ export function ChartEngine({
|
|
|
58
93
|
/** Scopes the engine's SVG resource ids per chart instance (`useId` output carries colons). */
|
|
59
94
|
export const idPrefixOf = (id: string, prefix = 'chart'): string =>
|
|
60
95
|
`basalt-${prefix}-${id.replaceAll(/[^\w-]/g, '')}`
|
|
96
|
+
|
|
97
|
+
type RenderContext = Parameters<NonNullable<EngineProps['onRender']>>[0]
|
|
98
|
+
|
|
99
|
+
const sizeOf = (width: number, height: number): PlotSize => ({ width, height })
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The laid-out plot area of each pane. A single chart is the scene's own `chart` bounds. A composed
|
|
103
|
+
* chart's panes are laid out inside the engine's view composition and not exposed on the scene, so
|
|
104
|
+
* each pane's plot is the rect of its marks clip (`<owner>:<pane>:marks:clip-defs`), which the
|
|
105
|
+
* rung's `clip: true` makes the plot exactly.
|
|
106
|
+
*/
|
|
107
|
+
function measurePlots({ scene, svg }: RenderContext): PlotSizes {
|
|
108
|
+
const views = svg.querySelectorAll('g.ts-chart__view')
|
|
109
|
+
if (views.length === 0) return { [MAIN_PANE]: sizeOf(scene.chart.width, scene.chart.height) }
|
|
110
|
+
const sizes: Record<string, PlotSize> = {}
|
|
111
|
+
for (const view of views) {
|
|
112
|
+
const base = view.getAttribute('data-ts-key')?.replace(/view$/, '')
|
|
113
|
+
const clip = base && view.querySelector(`[data-ts-key="${base}marks:clip-defs"] rect`)
|
|
114
|
+
const pane = base?.split(':').at(-2)
|
|
115
|
+
if (!pane || !clip) continue
|
|
116
|
+
sizes[pane] = sizeOf(Number(clip.getAttribute('width')), Number(clip.getAttribute('height')))
|
|
117
|
+
}
|
|
118
|
+
return sizes
|
|
119
|
+
}
|
package/src/charts/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ export {
|
|
|
19
19
|
} from './BandStrip'
|
|
20
20
|
export { foldBands } from './band-fold'
|
|
21
21
|
export { Donut, type DonutProps, type DonutDatum } from './Donut'
|
|
22
|
-
export { Heatmap, type HeatmapProps } from './Heatmap'
|
|
22
|
+
export { Heatmap, type HeatmapProps, type HeatmapState } from './Heatmap'
|
|
23
23
|
export { DualPanel, type DualPanelProps, type DualPanelLine, type DualPanelBar } from './DualPanel'
|
|
24
24
|
export {
|
|
25
25
|
MirroredBars,
|
package/src/charts/legend.tsx
CHANGED
|
@@ -58,6 +58,21 @@ const autoRowStyle: LegendVars = {
|
|
|
58
58
|
'--bc-legend-gap': `${VX.legendGap}px`,
|
|
59
59
|
'--bc-dots-gap': `${DOTS_GAP}px`,
|
|
60
60
|
}
|
|
61
|
+
/** The row's layout; a concealed legend keeps it (and so its height) but is not visible. */
|
|
62
|
+
function rowLayout(mode: LegendMode, concealed: boolean): CSSProperties {
|
|
63
|
+
// Hidden, not removed: the data arriving must not shift the chart below it.
|
|
64
|
+
const hide: CSSProperties = concealed ? { visibility: 'hidden' } : {}
|
|
65
|
+
// `display` and the gaps are the stylesheet's in `auto`: the container picks them by width.
|
|
66
|
+
if (mode === 'auto') return { ...autoRowStyle, ...hide }
|
|
67
|
+
const gap = mode === 'dots' ? DOTS_GAP : VX.legendGap
|
|
68
|
+
return {
|
|
69
|
+
...rowStyle,
|
|
70
|
+
columnGap: gap,
|
|
71
|
+
// Caps each entry's `[data-basalt-hit]` overlay at the real gap between entries.
|
|
72
|
+
...({ '--vx-hit-gap': `${gap}px` } as CSSProperties),
|
|
73
|
+
...hide,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
61
76
|
const dividerStyle: CSSProperties = { width: 1, alignSelf: 'stretch', background: VX.divider }
|
|
62
77
|
const buttonStyle: CSSProperties = {
|
|
63
78
|
all: 'unset',
|
|
@@ -155,29 +170,23 @@ export type ChartLegendProps<T> = {
|
|
|
155
170
|
toggle: boolean
|
|
156
171
|
onToggle: (key: string) => void
|
|
157
172
|
onHighlight: (key: string | null) => void
|
|
173
|
+
/** Keeps the legend's box but shows and offers nothing: a chart with no marks names no series. */
|
|
174
|
+
concealed?: boolean
|
|
158
175
|
}
|
|
159
176
|
|
|
160
177
|
export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null {
|
|
161
178
|
const groups = legendEntries(props.series, props.groups)
|
|
162
179
|
if (groups.length === 0) return null
|
|
163
180
|
const auto = props.mode === 'auto'
|
|
164
|
-
const gap = props.mode === 'dots' ? DOTS_GAP : VX.legendGap
|
|
165
181
|
return (
|
|
166
182
|
<fieldset
|
|
167
183
|
aria-label="Legend"
|
|
168
184
|
data-basalt-chart-legend={props.mode}
|
|
185
|
+
// `visibility: hidden` is the real guard (nothing is announced, focused or clickable);
|
|
186
|
+
// `aria-hidden` is belt-and-braces for a tree that ignores computed visibility.
|
|
187
|
+
{...(props.concealed && { 'aria-hidden': true })}
|
|
169
188
|
{...(auto && { className: classes.auto })}
|
|
170
|
-
style={
|
|
171
|
-
auto
|
|
172
|
-
? // `display` and the gaps are the stylesheet's: the container picks them by width.
|
|
173
|
-
autoRowStyle
|
|
174
|
-
: {
|
|
175
|
-
...rowStyle,
|
|
176
|
-
columnGap: gap,
|
|
177
|
-
// Caps each entry's `[data-basalt-hit]` overlay at the real gap between entries.
|
|
178
|
-
...({ '--vx-hit-gap': `${gap}px` } as CSSProperties),
|
|
179
|
-
}
|
|
180
|
-
}
|
|
189
|
+
style={rowLayout(props.mode, props.concealed === true)}
|
|
181
190
|
>
|
|
182
191
|
{groups.map((group, gi) => [
|
|
183
192
|
gi > 0 && <span key={`divider-${gi}`} aria-hidden style={dividerStyle} />,
|
package/src/charts/marks.ts
CHANGED
|
@@ -114,22 +114,39 @@ export function cursorDots<T, X extends ChartX>(
|
|
|
114
114
|
s: ChartSeries<T>,
|
|
115
115
|
rows: readonly PlotRow<T, X>[],
|
|
116
116
|
yScale: 'y2' | undefined,
|
|
117
|
-
): ChartMarks
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
117
|
+
): ChartMarks {
|
|
118
|
+
// `dot.fill` is constant, so a series with `getMarker` gets one mark per distinct datum colour.
|
|
119
|
+
const byColor = new Map<string, PlotRow<T, X>[]>([[s.color, []]])
|
|
120
|
+
for (const row of rows) {
|
|
121
|
+
const color = s.getMarker?.(row.datum)?.color ?? s.color
|
|
122
|
+
const group = byColor.get(color)
|
|
123
|
+
if (group) group.push(row)
|
|
124
|
+
else byColor.set(color, [row])
|
|
125
|
+
}
|
|
126
|
+
return [...byColor].flatMap(([color, group]) =>
|
|
127
|
+
// The series colour keeps `<key>-cursor` even when no datum uses it; others are keyed by colour.
|
|
128
|
+
group.length === 0 && color !== s.color
|
|
129
|
+
? []
|
|
130
|
+
: [
|
|
131
|
+
dot(group, {
|
|
132
|
+
id: color === s.color ? `${s.key}-cursor` : `${s.key}-cursor-${colorSlug(color)}`,
|
|
133
|
+
x: rowX,
|
|
134
|
+
y: 'y',
|
|
135
|
+
r: VX.dotR,
|
|
136
|
+
fill: color,
|
|
137
|
+
fillOpacity: 0,
|
|
138
|
+
stroke: VX.surface.panel,
|
|
139
|
+
strokeWidth: 1.5,
|
|
140
|
+
strokeOpacity: 0,
|
|
141
|
+
states: [{ when: { focus: 'x' }, style: { fillOpacity: 1, strokeOpacity: 1 } }],
|
|
142
|
+
...(yScale !== undefined && { yScale }),
|
|
143
|
+
}),
|
|
144
|
+
],
|
|
145
|
+
)
|
|
131
146
|
}
|
|
132
147
|
|
|
148
|
+
const colorSlug = (color: string): string => color.replaceAll(/[^\w-]+/g, '_')
|
|
149
|
+
|
|
133
150
|
/**
|
|
134
151
|
* A single-series `barY`. The engine stacks every `barY` that has no explicit extent, and a row with
|
|
135
152
|
* no `z`/`color` channel is its own series: an N-by-N matrix (a 5k-row `DualPanel` took 10 s). So
|
package/src/charts/panel.tsx
CHANGED
|
@@ -24,6 +24,8 @@ import type { ChartShellProps, ShellContext } from './CartesianChart'
|
|
|
24
24
|
import { defaultFormatX, engineRow, utcHeaderDate, visibleSeries, xGuide, yGuide } from './compile'
|
|
25
25
|
import type { CartesianDefinition } from './compile'
|
|
26
26
|
import { cursorBinding, sourceOnlyFocus } from './cursor'
|
|
27
|
+
import { plotAccess } from './plot-context'
|
|
28
|
+
import type { PlotAccess } from './plot-context'
|
|
27
29
|
import type { XScaleModel } from './scales'
|
|
28
30
|
import { CHART_THEME, CROSSHAIR } from './theme'
|
|
29
31
|
import type { ContainerClass } from '../tokens/size-classes'
|
|
@@ -36,8 +38,12 @@ import type {
|
|
|
36
38
|
PlotRow,
|
|
37
39
|
} from './types'
|
|
38
40
|
|
|
41
|
+
/** What a pane builder is given: the engine's frame for the pane and its plot-size access, to build
|
|
42
|
+
* a `PlotContext` with `plotContext()` (`plot-context.ts`). */
|
|
43
|
+
export type PaneContext = ChartBuildContext & { plot: PlotAccess }
|
|
44
|
+
|
|
39
45
|
/** One pane, built per container class: its marks and its scales (x, y, and any helper scale). */
|
|
40
|
-
export type PaneBuilder = (ctx:
|
|
46
|
+
export type PaneBuilder = (ctx: PaneContext) => {
|
|
41
47
|
marks: ChartMarks
|
|
42
48
|
scales: StoredChartSpec['scales']
|
|
43
49
|
}
|
|
@@ -135,10 +141,13 @@ export function rowsOf<T, X extends ChartX>(
|
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
/** A pane as a composable view. The crosshair is per pane: together they span both. */
|
|
138
|
-
function paneView(
|
|
144
|
+
function paneView(
|
|
145
|
+
build: PaneBuilder,
|
|
146
|
+
access: () => PlotAccess,
|
|
147
|
+
): ComposableResponsiveChartDefinition {
|
|
139
148
|
return {
|
|
140
149
|
chart: (ctx) => {
|
|
141
|
-
const { marks, scales } = build(ctx)
|
|
150
|
+
const { marks, scales } = build({ ...ctx, plot: access() })
|
|
142
151
|
return {
|
|
143
152
|
marks: [...marks, crosshair({ x: CROSSHAIR, y: false })],
|
|
144
153
|
scales,
|
|
@@ -176,7 +185,10 @@ function panelDefinition<T, X extends ChartX>(
|
|
|
176
185
|
? Math.min(Math.max(plan.topFraction, MIN_PANE_SHARE), 1 - MIN_PANE_SHARE)
|
|
177
186
|
: 0.5
|
|
178
187
|
const composed = composeViews({
|
|
179
|
-
views: {
|
|
188
|
+
views: {
|
|
189
|
+
top: paneView(plan.top, () => plotAccess(definition, ctx.plot, 'top')),
|
|
190
|
+
bottom: paneView(plan.bottom, () => plotAccess(definition, ctx.plot, 'bottom')),
|
|
191
|
+
},
|
|
180
192
|
layout: grid({
|
|
181
193
|
rows: [
|
|
182
194
|
{ id: 'top', grow: top },
|
|
@@ -196,7 +208,7 @@ function panelDefinition<T, X extends ChartX>(
|
|
|
196
208
|
if (v instanceof Date && !formatX) return utcHeaderDate(v)
|
|
197
209
|
return formatX ? formatX(v as X) : defaultFormatX(v)
|
|
198
210
|
}
|
|
199
|
-
|
|
211
|
+
const definition: CartesianDefinition = {
|
|
200
212
|
chart: () => composed,
|
|
201
213
|
focus: tooltip?.onFollow ? 'group-x' : sourceOnlyFocus(cursorGroup, chartId),
|
|
202
214
|
maxFocusDistance: Number.POSITIVE_INFINITY,
|
|
@@ -224,6 +236,7 @@ function panelDefinition<T, X extends ChartX>(
|
|
|
224
236
|
},
|
|
225
237
|
},
|
|
226
238
|
}
|
|
239
|
+
return definition
|
|
227
240
|
}
|
|
228
241
|
|
|
229
242
|
export function PanelRung<T, X extends ChartX>(props: PanelRungProps<T, X>): ReactElement {
|