@micha.bigler/ui-core-micha 2.42.2 → 3.0.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/WORK_ORDERS.md +3 -1
- package/dev/entries.jsx +6 -0
- package/dist/components/charts/BarChart.js +23 -12
- package/dist/components/charts/ChartFrame.js +23 -5
- package/dist/components/charts/LineChart.js +23 -12
- package/dist/components/charts/ScatterChart.js +29 -22
- package/dist/components/charts/TimeSeriesChart.js +6 -1
- package/dist/components/charts/chartDefaults.js +290 -110
- package/docs/CHART-LAYOUT.md +129 -0
- package/package.json +1 -1
- package/src/components/charts/BarChart.jsx +30 -28
- package/src/components/charts/ChartFrame.jsx +24 -5
- package/src/components/charts/LineChart.jsx +31 -29
- package/src/components/charts/ScatterChart.jsx +37 -29
- package/src/components/charts/TimeSeriesChart.jsx +6 -1
- package/src/components/charts/chartDefaults.js +320 -111
- package/tests/BarChart.test.jsx +27 -39
- package/tests/LineChart.test.jsx +21 -24
- package/tests/ScatterChart.test.jsx +21 -24
- package/tests/chartDefaults.test.js +313 -186
- package/work-orders/UCM-CHART-11.md +132 -0
- package/work-orders/UCM-CHART-12.md +348 -0
|
@@ -50,8 +50,86 @@ function cssLengthToPixels(value) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
const MUI_LABELLED_X_AXIS_HEIGHT = 45;
|
|
53
|
+
// Still used by `sizeYAxisForContent` (THEME-9, y-axis width) below -- unrelated to CHART-11,
|
|
54
|
+
// which only replaces how the ROTATED x-axis path (`rotatedTickMetrics`) estimates width.
|
|
53
55
|
const AVERAGE_GLYPH_WIDTH_EM = 0.6;
|
|
54
56
|
|
|
57
|
+
// CHART-11: per-character width table (em units), the ESTIMATE fallback used only when no DOM is
|
|
58
|
+
// available to actually measure a tick label's rendered extent (SSR, or a test environment where
|
|
59
|
+
// `measureTickTextWidthPx` returns null -- jsdom does not implement real SVG text layout). A
|
|
60
|
+
// single flat multiplier (the pre-CHART-11 constant, `AVERAGE_GLYPH_WIDTH_EM = 0.6`) systematically
|
|
61
|
+
// overestimated real ward-name-shaped strings: they mix spaces, apostrophes and narrow lowercase
|
|
62
|
+
// letters ("Mang'ula", "Msolwa Station") with a few wide/uppercase ones, and a flat multiplier
|
|
63
|
+
// prices every character at the wide end. Three tiers, not a full per-glyph font metrics table --
|
|
64
|
+
// proportionate to "an estimate", not a replacement for real measurement.
|
|
65
|
+
const NARROW_GLYPH_WIDTH_EM = 0.28; // i l j I ' . , : ; ! | and space
|
|
66
|
+
const WIDE_GLYPH_WIDTH_EM = 0.78; // uppercase (except I), plus lowercase m/w
|
|
67
|
+
const DEFAULT_GLYPH_WIDTH_EM = 0.52; // everything else -- most lowercase letters and digits
|
|
68
|
+
const NARROW_GLYPH_CHARS = new Set(["i", "l", "j", "I", "'", ".", ",", ":", ";", "!", "|", " "]);
|
|
69
|
+
const WIDE_GLYPH_CHARS = new Set(["m", "w"]);
|
|
70
|
+
|
|
71
|
+
function glyphWidthEm(char) {
|
|
72
|
+
if (NARROW_GLYPH_CHARS.has(char)) return NARROW_GLYPH_WIDTH_EM;
|
|
73
|
+
if (WIDE_GLYPH_CHARS.has(char) || (char >= 'A' && char <= 'Z')) return WIDE_GLYPH_WIDTH_EM;
|
|
74
|
+
return DEFAULT_GLYPH_WIDTH_EM;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* CHART-11 ESTIMATE fallback (declared explicitly, not dressed up as a measurement): sums a
|
|
79
|
+
* per-character width table instead of applying one flat multiplier to the whole string. Only
|
|
80
|
+
* reached when `measureTickTextWidthPx` returns null.
|
|
81
|
+
*/
|
|
82
|
+
function estimateTextWidthPx(text, fontSizePx) {
|
|
83
|
+
let widthEm = 0;
|
|
84
|
+
for (const char of text) widthEm += glyphWidthEm(char);
|
|
85
|
+
return widthEm * fontSizePx;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let hiddenMeasurementTextNode;
|
|
89
|
+
|
|
90
|
+
function getHiddenMeasurementTextNode() {
|
|
91
|
+
if (typeof document === 'undefined') return null;
|
|
92
|
+
if (hiddenMeasurementTextNode && hiddenMeasurementTextNode.isConnected) {
|
|
93
|
+
return hiddenMeasurementTextNode;
|
|
94
|
+
}
|
|
95
|
+
const svgNS = 'http://www.w3.org/2000/svg';
|
|
96
|
+
const svg = document.createElementNS(svgNS, 'svg');
|
|
97
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
98
|
+
svg.style.position = 'absolute';
|
|
99
|
+
svg.style.width = '0';
|
|
100
|
+
svg.style.height = '0';
|
|
101
|
+
svg.style.overflow = 'hidden';
|
|
102
|
+
svg.style.pointerEvents = 'none';
|
|
103
|
+
const text = document.createElementNS(svgNS, 'text');
|
|
104
|
+
svg.appendChild(text);
|
|
105
|
+
(document.body || document.documentElement).appendChild(svg);
|
|
106
|
+
hiddenMeasurementTextNode = text;
|
|
107
|
+
return text;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* CHART-11: measures a tick label's real rendered width via a transient, invisible, shared SVG
|
|
112
|
+
* `<text>` node -- the same element type MUI's own axis ticks render as, so glyph metrics match
|
|
113
|
+
* whatever font actually cascades to it (this package has no access to the consumer's theme font
|
|
114
|
+
* at this call site; the transient node inherits the page's own cascade, same as a real tick
|
|
115
|
+
* would). Returns `null` (never throws) when no DOM is available (SSR) or when
|
|
116
|
+
* `getComputedTextLength` is unsupported/unreliable there (jsdom does not implement real SVG text
|
|
117
|
+
* layout) -- callers fall back to `estimateTextWidthPx` in that case, and tests inject their own
|
|
118
|
+
* stub via `spaceForRotatedTicks`'s optional `measureTextWidth` parameter for determinism.
|
|
119
|
+
*/
|
|
120
|
+
function measureTickTextWidthPx(text, fontSizePx) {
|
|
121
|
+
try {
|
|
122
|
+
const node = getHiddenMeasurementTextNode();
|
|
123
|
+
if (!node) return null;
|
|
124
|
+
node.setAttribute('font-size', String(fontSizePx));
|
|
125
|
+
node.textContent = text;
|
|
126
|
+
const length = node.getComputedTextLength();
|
|
127
|
+
return Number.isFinite(length) && length > 0 ? length : null;
|
|
128
|
+
} catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
55
133
|
// THEME-11: this package never formed an opinion on chart margins, so every chart fell back to
|
|
56
134
|
// MUI's own DEFAULT_MARGINS (20px on all four sides) -- measured live (hram's KPI cards) as 167
|
|
57
135
|
// of a 270px-tall chart being furniture. Per-side, not a uniform trim, because each side is for
|
|
@@ -61,79 +139,19 @@ const AVERAGE_GLYPH_WIDTH_EM = 0.6;
|
|
|
61
139
|
// less trimmed than `left` because a LINEAR x-axis's last tick label sits AT the edge and
|
|
62
140
|
// overhangs by about half its width (~13px at caption size) -- a band axis wouldn't need it, but
|
|
63
141
|
// making this conditional on scale type would be inferred behaviour with no named consumer.
|
|
142
|
+
//
|
|
143
|
+
// UCM-CHART-12 Rule 3: no caller-supplied `margin` exists any more (the model owns spacing
|
|
144
|
+
// completely, `resolveChartLayout` builds the final margin object directly) -- this is now only
|
|
145
|
+
// the base every side starts from before `resolveChartLayout` adds the legend band to one side.
|
|
64
146
|
export const PACKAGE_DEFAULT_MARGIN = {
|
|
65
147
|
top: 10, bottom: 8, left: 8, right: 16,
|
|
66
148
|
};
|
|
67
149
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* rotated labels -- `spaceForRotatedTicks` no longer computes a `bottom` of its own.
|
|
74
|
-
*/
|
|
75
|
-
export function withMarginDefaults(margin) {
|
|
76
|
-
const explicit = typeof margin === 'number'
|
|
77
|
-
? { top: margin, bottom: margin, left: margin, right: margin }
|
|
78
|
-
: margin;
|
|
79
|
-
return { ...PACKAGE_DEFAULT_MARGIN, ...explicit };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function formattedTickText(axis, value) {
|
|
83
|
-
if (!axis.valueFormatter) return String(value);
|
|
84
|
-
try {
|
|
85
|
-
return String(axis.valueFormatter(value, { location: 'tick' }));
|
|
86
|
-
} catch {
|
|
87
|
-
// Some formatters require MUI's complete scale context. Falling back to
|
|
88
|
-
// the raw value is conservative: it reserves at least as much space.
|
|
89
|
-
return String(value);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function rotatedTickMetrics(axis, defaultLineHeight) {
|
|
94
|
-
const angle = Number(axis.tickLabelStyle?.angle) || 0;
|
|
95
|
-
if (angle === 0) return null;
|
|
96
|
-
|
|
97
|
-
const fontSize = cssLengthToPixels(axis.tickLabelStyle?.fontSize);
|
|
98
|
-
if (fontSize === 0) return null;
|
|
99
|
-
|
|
100
|
-
const lineHeight = Number(axis.tickLabelStyle?.lineHeight) || defaultLineHeight;
|
|
101
|
-
const longestLabel = (axis.data || []).reduce((longest, value) => {
|
|
102
|
-
const text = formattedTickText(axis, value);
|
|
103
|
-
return text.length > longest.length ? text : longest;
|
|
104
|
-
}, '');
|
|
105
|
-
const textWidth = Math.max(fontSize, longestLabel.length * fontSize * AVERAGE_GLYPH_WIDTH_EM);
|
|
106
|
-
const radians = (angle * Math.PI) / 180;
|
|
107
|
-
const projectedHeight = Math.abs(Math.cos(radians)) * fontSize * lineHeight
|
|
108
|
-
+ Math.abs(Math.sin(radians)) * textWidth;
|
|
109
|
-
const extraHeight = Math.max(1, Math.ceil(projectedHeight - fontSize));
|
|
110
|
-
|
|
111
|
-
return { extraHeight };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* CHART-10: rotated labels project both their line box and text width vertically, and X-Charts
|
|
116
|
-
* uses `xAxis.height` -- not `margin.bottom` -- to size the band that actually CONTAINS them.
|
|
117
|
-
* `margin.bottom` only separates that axis band from the SVG edge; it plays no part in whether a
|
|
118
|
-
* label fits. Measured live (hram, 2026-08-22, real data): once `axis.height` carries the
|
|
119
|
-
* allowance, the rendered tick labels sit fully INSIDE the axis band with nothing below them --
|
|
120
|
-
* `margin.bottom` growing by the same allowance on top was reserving space nothing ever used, up
|
|
121
|
-
* to 130px (38% of a 340px card) on long rotated labels. Only `axis.height` grows here now;
|
|
122
|
-
* `margin` is returned untouched (whatever the caller passed, or didn't) so `withMarginDefaults`
|
|
123
|
-
* downstream applies the package's ordinary bottom default in every case, rotated or not -- a
|
|
124
|
-
* caller-set `margin.bottom` was never touched by this function even before this fix, and still
|
|
125
|
-
* isn't.
|
|
126
|
-
*/
|
|
127
|
-
export function spaceForRotatedTicks(xAxis, margin, defaultLineHeight = 1) {
|
|
128
|
-
const spacedXAxis = xAxis.map((axis) => {
|
|
129
|
-
const metrics = rotatedTickMetrics(axis, defaultLineHeight);
|
|
130
|
-
if (!metrics) return axis;
|
|
131
|
-
if (axis.height != null) return axis;
|
|
132
|
-
return { ...axis, height: MUI_LABELLED_X_AXIS_HEIGHT + metrics.extraHeight };
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
return { xAxis: spacedXAxis, margin };
|
|
136
|
-
}
|
|
150
|
+
// CHART-11: small clearance between the rotated text block's own projected extent and the
|
|
151
|
+
// bottom of the axis's reserved band -- the same order of magnitude as the package's own
|
|
152
|
+
// PACKAGE_DEFAULT_MARGIN breathing-room, applied here for the same reason (a hard 0px gap reads
|
|
153
|
+
// as clipped even when nothing is actually cut off).
|
|
154
|
+
const ROTATED_TICK_CLEARANCE_PX = 8;
|
|
137
155
|
|
|
138
156
|
// THEME-9: MUI X-Charts' own default y-axis width is a flat 45px (ticks) +
|
|
139
157
|
// 20px (label, if present) = 65px regardless of what the ticks actually say
|
|
@@ -265,56 +283,247 @@ export function defaultNumericTickFormatter(locale) {
|
|
|
265
283
|
: new Intl.NumberFormat(locale).format(value));
|
|
266
284
|
}
|
|
267
285
|
|
|
286
|
+
// UCM-CHART-12: named size tokens, resolved through the theme's 8px spacing unit
|
|
287
|
+
// (`src/theme/tokens.js`'s `spacing: 8`). `standard` is pinned to the pre-existing deployed
|
|
288
|
+
// default (`TimeSeriesChart`'s old `CHART_HEIGHT = 320`) so this migration does not also
|
|
289
|
+
// silently redraw every already-shipped default-sized chart; `compact`/`tall` are proportional.
|
|
290
|
+
// Documented plainly in `docs/CHART-LAYOUT.md` -- these are visible product decisions, not
|
|
291
|
+
// internal plumbing.
|
|
292
|
+
export const CHART_SIZE_SPACING_UNITS = { compact: 30, standard: 40, tall: 50 };
|
|
293
|
+
|
|
294
|
+
// UCM-CHART-12 Rule 2: every band is its own measured content, or zero -- no band is ever a
|
|
295
|
+
// constant taken on faith. These two ARE constants, but only for the "content is present" case;
|
|
296
|
+
// each one collapses to 0 the moment its content is absent (`resolveXAxisGeometry`,
|
|
297
|
+
// `resolveChartLayout` below).
|
|
298
|
+
//
|
|
299
|
+
// ui_reviewer finding U1: before this WO, a plain (unrotated, untitled) axis got NO explicit
|
|
300
|
+
// `axis.height` at all -- MUI computed its own default internally. This resolver now always sets
|
|
301
|
+
// `axis.height` explicitly (Rule 1: nothing outside the resolver does chart arithmetic), which
|
|
302
|
+
// looked like a silent behaviour change until checked against MUI's own source
|
|
303
|
+
// (`@mui/x-charts/constants`): `DEFAULT_AXIS_SIZE_HEIGHT = 25` and `AXIS_LABEL_DEFAULT_HEIGHT =
|
|
304
|
+
// 20` -- i.e. `TICK_BAND_BASE_PX`/`AXIS_TITLE_BAND_PX` below are not an approximation, they are
|
|
305
|
+
// MUI's own constants, confirmed pixel-for-pixel. Making them explicit changes nothing about what
|
|
306
|
+
// renders for that common case; `TICK_BAND_BASE_PX + AXIS_TITLE_BAND_PX` = 45 is also exactly the
|
|
307
|
+
// pre-CHART-12 `MUI_LABELLED_X_AXIS_HEIGHT`, decomposed into the two bands Rule 2 requires instead
|
|
308
|
+
// of one opaque "labelled axis height" figure.
|
|
309
|
+
const TICK_BAND_BASE_PX = 25;
|
|
310
|
+
const AXIS_TITLE_BAND_PX = MUI_LABELLED_X_AXIS_HEIGHT - TICK_BAND_BASE_PX;
|
|
311
|
+
// One row of legend chips + gap -- MUI does not reserve this space itself (confirmed: nothing in
|
|
312
|
+
// this package's pre-CHART-12 margin/axis math ever accounted for the legend), so a visible
|
|
313
|
+
// legend growing the SVG uncounted was the sixth defect this WO's Rule 2 also closes.
|
|
314
|
+
const LEGEND_BAND_PX = 32;
|
|
315
|
+
|
|
316
|
+
// UCM-CHART-12 `xLabels: "auto"`: with no real container-width measurement available before
|
|
317
|
+
// render (the presets stay responsive -- MUI measures actual width via ResizeObserver, which this
|
|
318
|
+
// pure function cannot see), "auto" is a documented HEURISTIC, not a true fit computation: rotate
|
|
319
|
+
// once there are enough categories AND the longest one is long enough that a horizontal row would
|
|
320
|
+
// plausibly collide. The mandatory browser-level check (WO Part C) is what actually verifies fit
|
|
321
|
+
// against real rendered geometry; this only picks a reasonable default ahead of that.
|
|
322
|
+
const AUTO_ROTATE_TICK_COUNT = 6;
|
|
323
|
+
const AUTO_ROTATE_LABEL_CHARS = 4;
|
|
324
|
+
|
|
325
|
+
function xAxisTickContentText(axis) {
|
|
326
|
+
const data = axis?.data;
|
|
327
|
+
if (!Array.isArray(data)) return null; // no data array -- can't tell (e.g. a numeric scale); assume content
|
|
328
|
+
return longestFormattedTick(data, axis.valueFormatter);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// The Access-scatter case (WO Part A): an axis whose tick labels are all blank/absent must
|
|
332
|
+
// collapse its reserved band to zero, not keep the package's floor. `null` (no data array at
|
|
333
|
+
// all -- a linear numeric axis, whose ticks come from the scale, not a caller-supplied list)
|
|
334
|
+
// is treated as "assume content", since there is nothing here to measure as empty.
|
|
335
|
+
function xAxisHasVisibleTicks(axis) {
|
|
336
|
+
const text = xAxisTickContentText(axis);
|
|
337
|
+
return text === null || text.length > 0;
|
|
338
|
+
}
|
|
339
|
+
|
|
268
340
|
/**
|
|
269
|
-
* CHART-
|
|
270
|
-
*
|
|
271
|
-
* `
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
* - no `height`, `aspect` set -> unchanged from before this WO: the wrapper's own `aspectRatio`
|
|
276
|
-
* derives its height from its width, and `minHeight` stays exactly what its name says, a floor
|
|
277
|
-
* stopping it collapsing on a narrow viewport. Do not give the chart a fixed height here --
|
|
278
|
-
* that would remove the responsive behaviour four live call sites rely on.
|
|
279
|
-
* - no `height`, no `aspect` -> `minHeight` sizes the chart itself. There is no other sizing
|
|
280
|
-
* information to go on, and a caller in this shape meant the chart to have that height, not
|
|
281
|
-
* merely a wrapper floor around an unsized chart.
|
|
341
|
+
* UCM-CHART-12: decides the x-axis tick band's height and rotation angle. Replaces CHART-10/11's
|
|
342
|
+
* `spaceForRotatedTicks`/`rotatedTickMetrics` -- same measured-text-width mechanism
|
|
343
|
+
* (`measureTickTextWidthPx`, with `estimateTextWidthPx` as the no-DOM fallback and
|
|
344
|
+
* `measureTextWidth` as the test injection point), but now driven by `xLabels`
|
|
345
|
+
* ("auto"|"horizontal"|"angled") instead of a caller-supplied `tickLabelStyle.angle`, and
|
|
346
|
+
* returning 0 outright when the axis has no visible tick content (Rule 2).
|
|
282
347
|
*/
|
|
283
|
-
|
|
284
|
-
if (
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
348
|
+
function resolveXAxisGeometry(axis, xLabelsMode, defaultLineHeight, measureTextWidth, fallbackTickFontSize) {
|
|
349
|
+
if (!xAxisHasVisibleTicks(axis)) return { tickBand: 0, angle: 0 };
|
|
350
|
+
|
|
351
|
+
const fontSize = cssLengthToPixels(axis?.tickLabelStyle?.fontSize) || cssLengthToPixels(fallbackTickFontSize);
|
|
352
|
+
const longestLabel = xAxisTickContentText(axis) || '';
|
|
353
|
+
const dataCount = Array.isArray(axis?.data) ? axis.data.length : 0;
|
|
354
|
+
|
|
355
|
+
let angle = 0;
|
|
356
|
+
if (xLabelsMode === 'angled') {
|
|
357
|
+
angle = Number(axis?.tickLabelStyle?.angle) || -45;
|
|
358
|
+
} else if (xLabelsMode === 'auto') {
|
|
359
|
+
const needsRotation = dataCount > AUTO_ROTATE_TICK_COUNT && longestLabel.length > AUTO_ROTATE_LABEL_CHARS;
|
|
360
|
+
angle = needsRotation ? -45 : 0;
|
|
361
|
+
} // "horizontal" always stays at angle 0
|
|
362
|
+
|
|
363
|
+
if (angle === 0) return { tickBand: TICK_BAND_BASE_PX, angle: 0 };
|
|
364
|
+
// Rotation is a visual instruction independent of whether a font size is knowable here -- an
|
|
365
|
+
// "angled" axis still rotates even when `fontSize` can't be resolved (no theme fallback given),
|
|
366
|
+
// it just can't project a tighter band than the floor without one.
|
|
367
|
+
if (fontSize === 0) return { tickBand: TICK_BAND_BASE_PX, angle };
|
|
368
|
+
|
|
369
|
+
const lineHeight = Number(axis?.tickLabelStyle?.lineHeight) || defaultLineHeight;
|
|
370
|
+
const measure = measureTextWidth || measureTickTextWidthPx;
|
|
371
|
+
const measuredWidth = measure(longestLabel, fontSize);
|
|
372
|
+
const textWidth = Math.max(
|
|
373
|
+
fontSize,
|
|
374
|
+
Number.isFinite(measuredWidth) && measuredWidth > 0
|
|
375
|
+
? measuredWidth
|
|
376
|
+
: estimateTextWidthPx(longestLabel, fontSize),
|
|
377
|
+
);
|
|
378
|
+
const radians = (angle * Math.PI) / 180;
|
|
379
|
+
const projectedHeight = Math.abs(Math.cos(radians)) * fontSize * lineHeight
|
|
380
|
+
+ Math.abs(Math.sin(radians)) * textWidth;
|
|
381
|
+
const tickBand = Math.max(TICK_BAND_BASE_PX, Math.ceil(projectedHeight) + ROTATED_TICK_CLEARANCE_PX);
|
|
382
|
+
return { tickBand, angle };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function resolveChartHeightPx(size, height, spacing) {
|
|
386
|
+
if (height != null) return height;
|
|
387
|
+
const units = CHART_SIZE_SPACING_UNITS[size];
|
|
388
|
+
if (units == null) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
`[ui-core-micha] Unknown chart size="${size}". Use "compact" | "standard" | "tall", `
|
|
391
|
+
+ 'or pass height (in px) for the documented escape.',
|
|
392
|
+
);
|
|
290
393
|
}
|
|
291
|
-
|
|
394
|
+
// MUI's `theme.spacing(n)` returns a CSS length STRING ("240px"), not a number -- the chart
|
|
395
|
+
// height feeds straight into MUI X-Charts' own numeric `height` prop, so it must be unwrapped
|
|
396
|
+
// back to a px number via the same `cssLengthToPixels` the rest of this file already uses.
|
|
397
|
+
return typeof spacing === 'function' ? cssLengthToPixels(spacing(units)) : units * 8;
|
|
292
398
|
}
|
|
293
399
|
|
|
294
400
|
/**
|
|
295
|
-
* CHART-
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
401
|
+
* UCM-CHART-12: the one function that owns a chart's geometry (Rule 1-3), replacing
|
|
402
|
+
* `resolveChartHeight` + `spaceForRotatedTicks` + `warnOnHeightMismatch`. Returns the COMPLETE
|
|
403
|
+
* geometry a preset needs -- wrapper `sx`, chart height, margins, and both resolved axis arrays --
|
|
404
|
+
* plus the named `bands` the composition invariant is asserted against:
|
|
299
405
|
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
406
|
+
* chartHeight === bands.plotHeight + bands.xAxisBand + bands.xTitleBand + bands.legendBand
|
|
407
|
+
* containerWidth === bands.yAxisBand + bands.plotWidth + bands.rightPad (when `containerWidth` is given)
|
|
408
|
+
*
|
|
409
|
+
* `plotHeight`/`plotWidth` are the RESIDUAL of `chartHeight`/`containerWidth` minus the OTHER
|
|
410
|
+
* fields this function itself returns (`margin.top/bottom`, `xAxis[0].height`, `yAxisBand`) --
|
|
411
|
+
* by construction, summing them back up can never fail to balance, so that arithmetic identity
|
|
412
|
+
* ALONE proves nothing about a `UCM-CHART-10`-shaped bug (a term double-counted between
|
|
413
|
+
* `xAxis.height` and `margin.bottom`). What actually catches that class of bug is asserting each
|
|
414
|
+
* individual band lands in exactly one place downstream, independently of the others:
|
|
415
|
+
* `xAxis[0].height === xAxisBand + xTitleBand` (nothing else may also carry the tick/title
|
|
416
|
+
* allowance) and `margin.top + margin.bottom - PACKAGE_DEFAULT_MARGIN.top -
|
|
417
|
+
* PACKAGE_DEFAULT_MARGIN.bottom === legendBand` (the ONLY thing margin grows for, over its own
|
|
418
|
+
* package baseline, is the legend). The test suite asserts both forms side by side.
|
|
419
|
+
*
|
|
420
|
+
* `plotHeight` deliberately does NOT subtract the base `PACKAGE_DEFAULT_MARGIN` breathing room --
|
|
421
|
+
* that margin is intentional spacing around the plot (THEME-11), not one of the three itemized
|
|
422
|
+
* "furniture" bands (tick/title/legend) this WO exists to zero out, so it is treated as part of
|
|
423
|
+
* "the rest of the chart", the same as `plotHeight` itself.
|
|
424
|
+
*
|
|
425
|
+
* `containerWidth` is optional and only meaningful for tests / the browser-level check -- the
|
|
426
|
+
* presets themselves stay responsive (no explicit width) and never pass it.
|
|
427
|
+
*/
|
|
428
|
+
export function resolveChartLayout({
|
|
429
|
+
size = 'standard',
|
|
430
|
+
height,
|
|
431
|
+
xAxis,
|
|
432
|
+
yAxis,
|
|
433
|
+
series = [],
|
|
434
|
+
xLabels = 'auto',
|
|
435
|
+
hideLegend = false,
|
|
436
|
+
legendPosition = DEFAULT_LEGEND_POSITION,
|
|
437
|
+
tickFontSize,
|
|
438
|
+
spacing,
|
|
439
|
+
defaultLineHeight = 1,
|
|
440
|
+
measureTextWidth,
|
|
441
|
+
containerWidth,
|
|
442
|
+
} = {}) {
|
|
443
|
+
const chartHeight = resolveChartHeightPx(size, height, spacing);
|
|
444
|
+
|
|
445
|
+
const xAxisList = xAxis?.length ? xAxis : [{}];
|
|
446
|
+
const primaryXAxis = xAxisList[0];
|
|
447
|
+
const { tickBand: xAxisBand, angle } = resolveXAxisGeometry(
|
|
448
|
+
primaryXAxis, xLabels, defaultLineHeight, measureTextWidth, tickFontSize,
|
|
449
|
+
);
|
|
450
|
+
const xTitleBand = primaryXAxis?.label ? AXIS_TITLE_BAND_PX : 0;
|
|
451
|
+
|
|
452
|
+
// A `vertical: 'middle'` legend renders on the SIDE (consuming width, not height) -- reviewer
|
|
453
|
+
// finding: only branching on 'top' vs. not-'top' always reserved the legend band vertically,
|
|
454
|
+
// over-reserving height and under-reserving width for a side-placed legend. This WO's width
|
|
455
|
+
// invariant does not itemize a legend-width band, so a side legend simply gets none of the
|
|
456
|
+
// height reservation here rather than a wrong one -- consistent with the WO's own text ("0 when
|
|
457
|
+
// legend is off or horizontal").
|
|
458
|
+
const legendVertical = legendPosition?.vertical ?? DEFAULT_LEGEND_POSITION.vertical;
|
|
459
|
+
const legendIsSidePlaced = legendVertical === 'middle';
|
|
460
|
+
const legendHeightBand = hideLegend || legendIsSidePlaced ? 0 : LEGEND_BAND_PX;
|
|
461
|
+
const legendOnTop = legendHeightBand > 0 && legendVertical === 'top';
|
|
462
|
+
|
|
463
|
+
const sizedYAxis = sizeYAxisForContent(yAxis?.length ? yAxis : [{}], series, tickFontSize);
|
|
464
|
+
const yAxisBand = sizedYAxis[0]?.width ?? 0;
|
|
465
|
+
const rightPad = PACKAGE_DEFAULT_MARGIN.right;
|
|
466
|
+
|
|
467
|
+
const resolvedXAxis = xAxisList.map((axisEntry, index) => {
|
|
468
|
+
if (index !== 0 || axisEntry.height != null) return axisEntry;
|
|
469
|
+
// `xLabels` is the SOLE source of truth for rotation (Rule 3) -- `angle` is always written
|
|
470
|
+
// explicitly here, even when 0, rather than only overriding a truthy value. Leaving a stale
|
|
471
|
+
// caller-supplied `tickLabelStyle.angle` in place when `xLabels` resolved to no rotation would
|
|
472
|
+
// let MUI still render a rotated label whose band this resolver reserved as if it were flat --
|
|
473
|
+
// exactly the space/render mismatch this WO exists to close, just inverted (under-reservation
|
|
474
|
+
// instead of CHART-10's over-reservation).
|
|
475
|
+
const { angle: _callerAngle, ...restTickLabelStyle } = axisEntry.tickLabelStyle || {};
|
|
476
|
+
return {
|
|
477
|
+
...axisEntry,
|
|
478
|
+
height: xAxisBand + xTitleBand,
|
|
479
|
+
tickLabelStyle: { ...restTickLabelStyle, angle },
|
|
480
|
+
};
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
const margin = {
|
|
484
|
+
top: PACKAGE_DEFAULT_MARGIN.top + (legendOnTop ? legendHeightBand : 0),
|
|
485
|
+
bottom: PACKAGE_DEFAULT_MARGIN.bottom + (legendOnTop ? 0 : legendHeightBand),
|
|
486
|
+
left: PACKAGE_DEFAULT_MARGIN.left,
|
|
487
|
+
right: rightPad,
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
// `plotHeight`/`plotWidth`: the residual of `chartHeight`/`containerWidth` minus the three named
|
|
491
|
+
// furniture bands (see this function's own docblock for why this alone does not prove the
|
|
492
|
+
// absence of a double-counted term, and what does).
|
|
493
|
+
const plotHeight = chartHeight - xAxisBand - xTitleBand - legendHeightBand;
|
|
494
|
+
const plotWidth = containerWidth != null ? containerWidth - yAxisBand - rightPad : undefined;
|
|
495
|
+
|
|
496
|
+
return {
|
|
497
|
+
chartHeight,
|
|
498
|
+
sx: { width: '100%', height: chartHeight },
|
|
499
|
+
margin,
|
|
500
|
+
xAxis: resolvedXAxis,
|
|
501
|
+
yAxis: sizedYAxis,
|
|
502
|
+
bands: {
|
|
503
|
+
plotHeight,
|
|
504
|
+
xAxisBand,
|
|
505
|
+
xTitleBand,
|
|
506
|
+
legendBand: legendHeightBand,
|
|
507
|
+
yAxisBand,
|
|
508
|
+
rightPad,
|
|
509
|
+
...(containerWidth != null ? { plotWidth, containerWidth } : {}),
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* UCM-CHART-12 Rule 1/3: `minHeight`, `aspect`, and `margin` are gone from the four chart presets
|
|
516
|
+
* -- the model owns sizing and spacing completely. Passing one is a dev-mode error naming its
|
|
517
|
+
* replacement (never a silent ignore, so the 38 existing call sites this breaks get a clear
|
|
518
|
+
* migration path instead of a layout that quietly stops matching what the prop says) -- gated on
|
|
519
|
+
* `NODE_ENV !== 'production'` like the deleted `warnOnHeightMismatch` was, so a shared package
|
|
520
|
+
* cannot crash a consumer's production page over a layout prop; loud in dev, inert in prod.
|
|
305
521
|
*/
|
|
306
|
-
export function
|
|
307
|
-
if (process.env.NODE_ENV === 'production') return;
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
+ 'chart. Pass only height, or drop minHeight if it should just track height.'
|
|
312
|
-
: 'they disagree; height is ignored here and minHeight keeps sizing the wrapper as a floor. '
|
|
313
|
-
+ 'Pass only minHeight, or drop height if it was meant to size the whole card.';
|
|
314
|
-
// eslint-disable-next-line no-console
|
|
315
|
-
console.warn(
|
|
316
|
-
`[ui-core-micha] <${componentName}> received both minHeight={${minHeight}} and height={${height}}; `
|
|
317
|
-
+ resolution,
|
|
522
|
+
export function assertRemovedChartProp(componentName, propName, value, replacement) {
|
|
523
|
+
if (value === undefined || process.env.NODE_ENV === 'production') return;
|
|
524
|
+
throw new Error(
|
|
525
|
+
`[ui-core-micha] <${componentName}> no longer accepts "${propName}" (removed in UCM-CHART-12, `
|
|
526
|
+
+ `v3.0.0 -- see docs/CHART-LAYOUT.md). ${replacement}`,
|
|
318
527
|
);
|
|
319
528
|
}
|
|
320
529
|
|
package/tests/BarChart.test.jsx
CHANGED
|
@@ -20,7 +20,7 @@ function renderChart(props) {
|
|
|
20
20
|
describe('BarChart preset', () => {
|
|
21
21
|
afterEach(cleanup);
|
|
22
22
|
|
|
23
|
-
it('passes both axis labels, tooltip,
|
|
23
|
+
it('passes both axis labels, tooltip, the default "standard" height, and hides a single-series legend', () => {
|
|
24
24
|
renderChart({ series: [{ data: [1, 2] }] });
|
|
25
25
|
const props = chartSpy.mock.calls[0][0];
|
|
26
26
|
|
|
@@ -31,7 +31,10 @@ describe('BarChart preset', () => {
|
|
|
31
31
|
legend: { position: { vertical: 'bottom', horizontal: 'start' } },
|
|
32
32
|
});
|
|
33
33
|
expect(props.hideLegend).toBe(true);
|
|
34
|
-
|
|
34
|
+
// UCM-CHART-12: size defaults to "standard" (320px, the historical default) -- the wrapper
|
|
35
|
+
// always carries an explicit height now, width stays responsive.
|
|
36
|
+
expect(props.height).toBe(320);
|
|
37
|
+
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).height).toBe('320px');
|
|
35
38
|
});
|
|
36
39
|
|
|
37
40
|
it.each([
|
|
@@ -72,51 +75,36 @@ describe('BarChart preset', () => {
|
|
|
72
75
|
expect(props.yAxis[0].label).toBeUndefined();
|
|
73
76
|
});
|
|
74
77
|
|
|
75
|
-
// CHART-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
// UCM-CHART-12: `size`/`height` resolution, wired through the actual component. Replaces the
|
|
79
|
+
// deleted `minHeight`/`aspect` CHART-8 trio -- see the removed-prop assertions below for the
|
|
80
|
+
// migration side of this.
|
|
81
|
+
describe('size/height resolution (UCM-CHART-12)', () => {
|
|
82
|
+
it.each([
|
|
83
|
+
['compact', 240],
|
|
84
|
+
['standard', 320],
|
|
85
|
+
['tall', 400],
|
|
86
|
+
])('resolves size="%s" to %ipx, on both the chart height and the wrapper', (size, px) => {
|
|
87
|
+
renderChart({ series: [{ data: [1] }], size });
|
|
88
|
+
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(px);
|
|
89
|
+
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).height).toBe(`${px}px`);
|
|
81
90
|
});
|
|
82
91
|
|
|
83
|
-
it('
|
|
84
|
-
renderChart({ series: [{ data: [1] }],
|
|
85
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).
|
|
86
|
-
|
|
87
|
-
expect(style.minHeight).toBe('320px');
|
|
88
|
-
expect(style.aspectRatio).toBe('1.8 / 1');
|
|
92
|
+
it('lets height override size entirely, as the documented escape', () => {
|
|
93
|
+
renderChart({ series: [{ data: [1] }], size: 'tall', height: 280 });
|
|
94
|
+
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(280);
|
|
95
|
+
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).height).toBe('280px');
|
|
89
96
|
});
|
|
90
97
|
|
|
91
|
-
it('
|
|
92
|
-
renderChart({ series: [{ data: [1] }], minHeight:
|
|
93
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(380);
|
|
94
|
-
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).minHeight).toBe('380px');
|
|
98
|
+
it('throws in dev when a caller still passes minHeight, naming the replacement', () => {
|
|
99
|
+
expect(() => renderChart({ series: [{ data: [1] }], minHeight: 300 })).toThrow(/size=.*standard.*tall/);
|
|
95
100
|
});
|
|
96
101
|
|
|
97
|
-
it('
|
|
98
|
-
renderChart({ series: [{ data: [1] }],
|
|
99
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(320);
|
|
100
|
-
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).minHeight).toBe('320px');
|
|
102
|
+
it('throws in dev when a caller still passes aspect, naming the removal', () => {
|
|
103
|
+
expect(() => renderChart({ series: [{ data: [1] }], aspect: 1.8 })).toThrow(/aspect/);
|
|
101
104
|
});
|
|
102
105
|
|
|
103
|
-
it('
|
|
104
|
-
renderChart({ series: [{ data: [1] }],
|
|
105
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(280);
|
|
106
|
-
expect(window.getComputedStyle(screen.getByTestId('bar-chart-container')).minHeight).toBe('auto');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('warns only on the disagreeing pair, naming both values, and not on the aspect combination', () => {
|
|
110
|
-
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
111
|
-
renderChart({ series: [{ data: [1] }], minHeight: 420, height: 380 });
|
|
112
|
-
expect(warn).toHaveBeenCalledOnce();
|
|
113
|
-
expect(warn.mock.calls[0][0]).toContain('420');
|
|
114
|
-
expect(warn.mock.calls[0][0]).toContain('380');
|
|
115
|
-
warn.mockClear();
|
|
116
|
-
|
|
117
|
-
renderChart({ series: [{ data: [1] }], minHeight: 320, aspect: 1.8 });
|
|
118
|
-
expect(warn).not.toHaveBeenCalled();
|
|
119
|
-
warn.mockRestore();
|
|
106
|
+
it('throws in dev when a caller still passes margin, naming the removal', () => {
|
|
107
|
+
expect(() => renderChart({ series: [{ data: [1] }], margin: { top: 1 } })).toThrow(/margin/);
|
|
120
108
|
});
|
|
121
109
|
});
|
|
122
110
|
});
|
package/tests/LineChart.test.jsx
CHANGED
|
@@ -34,8 +34,9 @@ describe('LineChart preset', () => {
|
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
// CHART-
|
|
38
|
-
|
|
37
|
+
// UCM-CHART-12: `size`/`height` resolution, wired through the actual component. Replaces the
|
|
38
|
+
// deleted `minHeight`/`aspect` CHART-8 trio.
|
|
39
|
+
describe('size/height resolution (UCM-CHART-12)', () => {
|
|
39
40
|
function renderLine(props) {
|
|
40
41
|
return render(
|
|
41
42
|
<ThemeProvider theme={createTheme()}>
|
|
@@ -46,36 +47,32 @@ describe('LineChart preset', () => {
|
|
|
46
47
|
|
|
47
48
|
afterEach(cleanup);
|
|
48
49
|
|
|
49
|
-
it(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
it.each([
|
|
51
|
+
['compact', 240],
|
|
52
|
+
['standard', 320],
|
|
53
|
+
['tall', 400],
|
|
54
|
+
])('resolves size="%s" to %ipx, on both the chart height and the wrapper', (size, px) => {
|
|
55
|
+
renderLine({ size });
|
|
56
|
+
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(px);
|
|
57
|
+
expect(window.getComputedStyle(screen.getByTestId('line-chart-container')).height).toBe(`${px}px`);
|
|
53
58
|
});
|
|
54
59
|
|
|
55
|
-
it('
|
|
56
|
-
renderLine({
|
|
57
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).
|
|
58
|
-
|
|
59
|
-
expect(style.minHeight).toBe('320px');
|
|
60
|
-
expect(style.aspectRatio).toBe('1.8 / 1');
|
|
60
|
+
it('lets height override size entirely, as the documented escape', () => {
|
|
61
|
+
renderLine({ size: 'compact', height: 280 });
|
|
62
|
+
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(280);
|
|
63
|
+
expect(window.getComputedStyle(screen.getByTestId('line-chart-container')).height).toBe('280px');
|
|
61
64
|
});
|
|
62
65
|
|
|
63
|
-
it('
|
|
64
|
-
renderLine({ minHeight:
|
|
65
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(380);
|
|
66
|
-
expect(window.getComputedStyle(screen.getByTestId('line-chart-container')).minHeight).toBe('380px');
|
|
66
|
+
it('throws in dev when a caller still passes minHeight, naming the replacement', () => {
|
|
67
|
+
expect(() => renderLine({ minHeight: 300 })).toThrow(/size=.*standard.*tall/);
|
|
67
68
|
});
|
|
68
69
|
|
|
69
|
-
it('
|
|
70
|
-
renderLine({
|
|
71
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(320);
|
|
72
|
-
expect(window.getComputedStyle(screen.getByTestId('line-chart-container')).minHeight).toBe('320px');
|
|
70
|
+
it('throws in dev when a caller still passes aspect, naming the removal', () => {
|
|
71
|
+
expect(() => renderLine({ aspect: 1.8 })).toThrow(/aspect/);
|
|
73
72
|
});
|
|
74
73
|
|
|
75
|
-
it('
|
|
76
|
-
renderLine({
|
|
77
|
-
expect(chartSpy.mock.calls.at(-1)[0].height).toBe(280);
|
|
78
|
-
expect(window.getComputedStyle(screen.getByTestId('line-chart-container')).minHeight).toBe('auto');
|
|
74
|
+
it('throws in dev when a caller still passes margin, naming the removal', () => {
|
|
75
|
+
expect(() => renderLine({ margin: { top: 1 } })).toThrow(/margin/);
|
|
79
76
|
});
|
|
80
77
|
});
|
|
81
78
|
});
|