Skip to content

The Open Standard for Generative UI

perf(react-ui): search only the labels that widen a scrolling chart's categories (#1273)

Ankit Dasd51a09cd4ed9
diff --git a/.changeset/chart-label-widening-perf.md b/.changeset/chart-label-widening-perf.md
new file mode 100644
index 000000000..4893471a6
--- /dev/null
+++ b/.changeset/chart-label-widening-perf.md
@@ -0,0 +1,5 @@
+---
+"@openuidev/react-ui": patch
+---
+
+Scrolling `BarChart`, `AreaChart` and `LineChart` lay out long category lists faster: only a label that widens the categories is measured in full, so the layout stays cheap as a long chart streams in. Widened categories are sized to the exact label width.
diff --git a/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.test.ts b/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.test.ts
index 83e99c798..e28393cb2 100644
--- a/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.test.ts
+++ b/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.test.ts
@@ -136,7 +136,7 @@ describe("layoutXAxisLabels — scrolling, full labels", () => {
it("widens the category until a long label wraps in full", () => {
const out = scroll([LONG, "Jan"]);
// The narrowest 3-line wrap is 180px ("Category Name That"), + the 8px gap.
- expect(out.slotWidth).toBe(189);
+ expect(out.slotWidth).toBe(188);
expect(out.maxLines).toBe(3);
expect(drawn(LONG, out)).toEqual(["Extremely Long", "Category Name That", "Needs Truncation"]);
});
@@ -198,6 +198,38 @@ describe("layoutXAxisLabels — scrolling, full labels", () => {
it("leaves the condensed layout's categories alone", () => {
expect(layout([LONG], { slotWidth: 60, visibleWidth: 1000 }).slotWidth).toBe(60);
});
+
+ it("sizes for the widest label that shows in full, wherever it sits", () => {
+ const labels = ["Jan", CSRT, LONG, "Customer Support", "SinglePointDataSetForTestingEdgeCases"];
+ const out = scroll(labels);
+ // LONG sets the width on its own (see above); the 370px word can't show in full.
+ expect(out.slotWidth).toBe(scroll([LONG]).slotWidth);
+ expect(scroll([...labels].reverse())).toEqual(out);
+ expect(scroll([...labels.slice(2), ...labels.slice(0, 2)])).toEqual(out);
+ });
+
+ it("measures a long category list a bounded number of times per label", () => {
+ let measures = 0;
+ const counting = {
+ measureText: (s: string) => {
+ measures++;
+ return { width: s.length * 10 };
+ },
+ } as unknown as CanvasRenderingContext2D;
+ const labels = Array.from({ length: 1000 }, (_, i) => `Region ${i} Support Tickets`);
+ const out = layoutXAxisLabels(counting, labels, {
+ variant: "multiLine",
+ slotWidth: 72,
+ maxHeight: 120,
+ lineHeight: LINE,
+ condensed: false,
+ visibleWidth: 1000,
+ });
+ expect(out.slotWidth).toBeGreaterThan(72);
+ // Only the labels that raise the width are searched; the rest are checked
+ // once or twice (the full search on every label took ~130 per label).
+ expect(measures / labels.length).toBeLessThan(40);
+ });
});
describe("fullLabelWidth", () => {
@@ -206,8 +238,8 @@ describe("fullLabelWidth", () => {
});
it("is the narrowest width that wraps the label into the allowed lines", () => {
- expect(fullLabelWidth(ctx, LONG, 3)).toBeCloseTo(180, 0);
- expect(fullLabelWidth(ctx, LONG, 2)).toBeCloseTo(260, 0);
+ expect(fullLabelWidth(ctx, LONG, 3)).toBe(180);
+ expect(fullLabelWidth(ctx, LONG, 2)).toBe(260);
});
it("never breaks a word", () => {
diff --git a/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.ts b/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.ts
index ca660dcd4..dd8f1b884 100644
--- a/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.ts
+++ b/packages/react-ui/src/components/Charts/utils/xAxisLabelLayout.ts
@@ -91,7 +91,8 @@ function fitsInFull(
width: number,
maxLines: number,
): boolean {
- if (maxLines <= 1) return ctx.measureText(label).width <= width;
+ if (ctx.measureText(label).width <= width) return true;
+ if (maxLines <= 1) return false;
return (
label.split(/\s+/).every((word) => ctx.measureText(word).width <= width) &&
wrapLabelLines(ctx, label, width, Number.POSITIVE_INFINITY).length <= maxLines
@@ -99,8 +100,7 @@ function fitsInFull(
}
/**
- * The narrowest width at which `label` shows in full (see `fitsInFull`).
- * Greedy wrapping never needs more lines at a larger width, so this bisects
+ * The narrowest width at which `label` shows in full (see `fitsInFull`):
* between the widest word and the whole label on one line.
*/
export function fullLabelWidth(
@@ -108,16 +108,34 @@ export function fullLabelWidth(
label: string,
maxLines: number,
): number {
- let hi = ctx.measureText(label).width;
- if (maxLines <= 1) return hi;
- let lo = Math.max(0, ...label.split(/\s+/).map((word) => ctx.measureText(word).width));
+ const oneLine = ctx.measureText(label).width;
+ if (maxLines <= 1) return oneLine;
+ const widestWord = Math.max(0, ...label.split(/\s+/).map((word) => ctx.measureText(word).width));
+ return narrowestFullWidth(ctx, label, maxLines, widestWord, oneLine);
+}
+
+/**
+ * The narrowest width in `[lo, hi]` at which `label` shows in full, given that
+ * it does at `hi`. Greedy wrapping never needs more lines at a larger width,
+ * so this bisects, then snaps to the widest line of the wrap it found: the
+ * wrap is the same at that width, so the result is an exact text width
+ * whatever the search bounds.
+ */
+function narrowestFullWidth(
+ ctx: CanvasRenderingContext2D,
+ label: string,
+ maxLines: number,
+ lo: number,
+ hi: number,
+): number {
if (fitsInFull(ctx, label, lo, maxLines)) return lo;
while (hi - lo > 0.5) {
const mid = (lo + hi) / 2;
if (fitsInFull(ctx, label, mid, maxLines)) hi = mid;
else lo = mid;
}
- return hi;
+ const lines = wrapLabelLines(ctx, label, hi, Number.POSITIVE_INFINITY);
+ return Math.max(...lines.map((line) => ctx.measureText(line).width));
}
/**
@@ -156,13 +174,25 @@ export function layoutXAxisLabels(
Math.min(MAX_SLOT_GROWTH * minSlotWidth, visibleWidth / 2),
);
const lines = variant === "multiLine" ? lineCap : 1;
- let needed = 0;
- for (const label of labels) {
- if (fitsInFull(ctx, label, minSlotWidth * labelShare, lines)) continue;
- const width = fullLabelWidth(ctx, label, lines);
- if (width <= maxSlotWidth * labelShare) needed = Math.max(needed, width);
+ const floor = minSlotWidth * labelShare;
+ const ceiling = maxSlotWidth * labelShare;
+ // The widest label that can show in full under the ceiling sets the width.
+ // Only a label that raises it is searched for its exact width; any other
+ // label costs a check or two (it fits the width found so far, or can't fit
+ // even at the ceiling), so long category lists stay cheap to lay out on
+ // every streamed update.
+ let needed = floor;
+ for (const label of new Set(labels)) {
+ if (fitsInFull(ctx, label, needed, lines) || !fitsInFull(ctx, label, ceiling, lines)) {
+ continue;
+ }
+ const oneLine = ctx.measureText(label).width;
+ needed =
+ lines <= 1
+ ? oneLine
+ : narrowestFullWidth(ctx, label, lines, needed, Math.min(ceiling, oneLine));
}
- if (needed > 0) {
+ if (needed > floor) {
const tight = Math.ceil(needed / labelShare);
// Leave a gap between neighbouring labels (line and area labels span the
// whole category), but never one that makes a chart that fits scroll.