Skip to content

Commit 74d724a

Browse files
committed
fix(plugin-charts): data labels print in their value axis's units and keep headroom above the tallest mark
Claude-Session: https://claude.ai/code/session_01JQwxbY75sPQWcfUg8cZQhg Co-authored-by: Claude <noreply@anthropic.com>
1 parent c4c515a commit 74d724a

2 files changed

Lines changed: 209 additions & 11 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* objectstack#27372 — a chart's data labels (`showDataLabels`) read in the
9+
* units of their value axis, and the tallest mark keeps its label on the
10+
* chart surface.
11+
*
12+
* Two defects, one label:
13+
* - The value axis falls back to the compact formatter (`400K`) when no
14+
* `yAxis.format` is authored; the data label had no fallback and printed
15+
* the raw number (`174000` beside a `400K` tick).
16+
* - The label sits above its mark. With recharts' default 5px top margin, a
17+
* mark on the plot's top edge (the domain's nice max) had its label drawn
18+
* wholly above the chart surface.
19+
*
20+
* happy-dom paints no text, but given a fixed box recharts computes every mark
21+
* and label position arithmetically (the same property
22+
* `AdvancedChartImpl.pointAxisEdgeLabels-13390.test.tsx` relies on), so these
23+
* pins read the label's text and its `y` from the DOM.
24+
*/
25+
import React from 'react';
26+
import { describe, it, expect, afterEach, vi } from 'vitest';
27+
import { render, cleanup } from '@testing-library/react';
28+
29+
const WIDTH = 520;
30+
const HEIGHT = 300;
31+
32+
vi.mock('recharts', async () => {
33+
const actual = await vi.importActual<typeof import('recharts')>('recharts');
34+
return {
35+
...actual,
36+
ResponsiveContainer: ({ children }: { children: React.ReactElement<{ width: number; height: number }> }) =>
37+
React.cloneElement(children, { width: WIDTH, height: HEIGHT }),
38+
};
39+
});
40+
41+
import AdvancedChartImpl from './AdvancedChartImpl';
42+
43+
afterEach(cleanup);
44+
45+
/** The card's shape: the tallest value lands on a nice top tick (400K). */
46+
const PIPELINE = [
47+
{ stage: 'Prospecting', amount: 174000, won: 12 },
48+
{ stage: 'Qualification', amount: 400000, won: 30 },
49+
{ stage: 'Proposal', amount: 251000, won: 18 },
50+
{ stage: 'Negotiation', amount: 92500, won: 7 },
51+
];
52+
53+
/** The font size the chart prints data labels in. */
54+
const LABEL_FONT_PX = 11;
55+
56+
type Family = NonNullable<React.ComponentProps<typeof AdvancedChartImpl>['chartType']>;
57+
58+
function draw(chartType: Family, extra: Record<string, unknown> = {}) {
59+
const { container } = render(
60+
<AdvancedChartImpl
61+
chartType={chartType}
62+
data={PIPELINE}
63+
xAxisKey="stage"
64+
series={[{ dataKey: 'amount', label: 'Amount' }]}
65+
showLegend={false}
66+
showDataLabels
67+
isAnimationActive={false}
68+
{...extra}
69+
/>,
70+
);
71+
return container;
72+
}
73+
74+
/** Each data label: its text and the `y` its baseline is drawn at. */
75+
function labelsOf(container: HTMLElement) {
76+
const texts = [...container.querySelectorAll('.recharts-label-list text')];
77+
expect(texts.length, 'the chart drew no data labels, so this measures nothing').toBeGreaterThan(0);
78+
return texts.map((t) => ({ text: (t.textContent ?? '').trim(), y: Number(t.getAttribute('y')) }));
79+
}
80+
81+
/** The y-axis tick texts, so a label can be read against its own axis. */
82+
function yTicksOf(container: HTMLElement, axis = '.recharts-yAxis') {
83+
return [...container.querySelectorAll(`${axis} .recharts-cartesian-axis-tick-value`)].map((t) =>
84+
(t.textContent ?? '').trim(),
85+
);
86+
}
87+
88+
describe('objectstack#27372 — a data label reads in its value axis’s units', () => {
89+
for (const chartType of ['bar', 'line', 'area'] as const) {
90+
it(`prints the compact default beside a compact ${chartType} axis when no format is authored`, () => {
91+
const container = draw(chartType);
92+
// The axis is the reference: it must itself be compact, or the label
93+
// matching it would prove nothing about units.
94+
expect(yTicksOf(container)).toContain('400K');
95+
expect(labelsOf(container).map((l) => l.text)).toEqual(['174K', '400K', '251K', '92.5K']);
96+
});
97+
}
98+
99+
it('lets an authored axis format win over the compact default', () => {
100+
const container = draw('bar', { yAxes: [{ field: 'amount', format: '$0,0' }] });
101+
expect(labelsOf(container).map((l) => l.text)).toEqual(['$174,000', '$400,000', '$251,000', '$92,500']);
102+
});
103+
104+
it('prints each series in the units of the axis it is bound to on a dual-axis chart', () => {
105+
const container = draw('bar', {
106+
series: [
107+
{ dataKey: 'amount', label: 'Amount' },
108+
{ dataKey: 'won', label: 'Won', yAxis: 'right' },
109+
],
110+
yAxes: [{ field: 'amount' }, { field: 'won', position: 'right', format: '0.0' }],
111+
});
112+
expect(labelsOf(container).map((l) => l.text)).toEqual([
113+
'174K', '400K', '251K', '92.5K',
114+
'12.0', '30.0', '18.0', '7.0',
115+
]);
116+
});
117+
118+
it('prints a combo series in the units of the axis it is drawn against', () => {
119+
const container = draw('combo', {
120+
series: [
121+
{ dataKey: 'amount', label: 'Amount', chartType: 'bar' },
122+
{ dataKey: 'won', label: 'Won', chartType: 'line', yAxis: 'right' },
123+
],
124+
yAxes: [{ field: 'amount' }, { field: 'won', position: 'right', format: '0.0' }],
125+
});
126+
expect(labelsOf(container).map((l) => l.text)).toEqual([
127+
'174K', '400K', '251K', '92.5K',
128+
'12.0', '30.0', '18.0', '7.0',
129+
]);
130+
});
131+
});
132+
133+
describe('objectstack#27372 — the tallest mark keeps its data label on the chart surface', () => {
134+
for (const chartType of ['bar', 'line', 'area', 'combo'] as const) {
135+
it(`draws the top label of a ${chartType} chart inside the surface`, () => {
136+
const container = draw(chartType);
137+
const labels = labelsOf(container);
138+
const top = labels.reduce((a, b) => (b.y < a.y ? b : a));
139+
expect(top.text).toBe('400K');
140+
// The label's baseline is at `y`; its glyphs rise at most one font size
141+
// above it. Below that, the label is (partly) off the top of the svg.
142+
expect(top.y - LABEL_FONT_PX, `"${top.text}" is drawn above the chart surface`).toBeGreaterThanOrEqual(0);
143+
});
144+
}
145+
146+
it('keeps recharts’ default top margin when data labels are off', () => {
147+
const { container } = render(
148+
<AdvancedChartImpl
149+
chartType="bar"
150+
data={PIPELINE}
151+
xAxisKey="stage"
152+
series={[{ dataKey: 'amount', label: 'Amount' }]}
153+
showLegend={false}
154+
isAnimationActive={false}
155+
/>,
156+
);
157+
const plotTop = Number(container.querySelector('defs clipPath rect')?.getAttribute('y'));
158+
expect(plotTop).toBe(5);
159+
});
160+
});

‎ui/packages/plugin-charts/src/AdvancedChartImpl.tsx‎

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,36 @@ function estimateLabelWidthPx(label: string): number {
482482
/** recharts' default chart margin, left plus right (5px each side). */
483483
const CHART_MARGIN_X_PX = 10;
484484

485+
/** The font size `showDataLabels` prints each point's value in, in px. */
486+
const DATA_LABEL_FONT_PX = 11;
487+
/** recharts' `LabelList` default `offset`: the gap between a mark and its label. */
488+
const DATA_LABEL_OFFSET_PX = 5;
489+
490+
/**
491+
* The chart margin a vertical cartesian chart (bar, line, area, combo) takes
492+
* while `showDataLabels` is on (objectstack#27372).
493+
*
494+
* A data label sits ABOVE its mark: its baseline is `DATA_LABEL_OFFSET_PX`
495+
* over the mark's top. recharts' default margin leaves 5px between the plot's
496+
* top edge and the end of the chart surface, and the value axis's nice domain
497+
* routinely puts the tallest mark exactly on that edge (a 400,000 bar under a
498+
* 400K top tick). Its label was then drawn wholly outside the surface: the
499+
* tallest bar, the one a reader looks at first, was the one with no value.
500+
*
501+
* The top margin is that 5px plus the label's own height (offset and font),
502+
* so a mark ON the top edge still has its label inside the surface. A margin
503+
* rather than a padded domain: an author's `yAxis.max` pins the domain and
504+
* must keep doing so, and the margin holds whatever the domain is. The other
505+
* three sides stay recharts' default 5px, which `CHART_MARGIN_X_PX` counts on.
506+
* Horizontal bars are left alone: their value runs across the plot.
507+
*/
508+
const DATA_LABEL_CHART_MARGIN = {
509+
top: 5 + DATA_LABEL_OFFSET_PX + DATA_LABEL_FONT_PX,
510+
right: 5,
511+
bottom: 5,
512+
left: 5,
513+
} as const;
514+
485515
/**
486516
* The inset, in px, a line or area chart's categorical x axis keeps at BOTH
487517
* ends of the plot (objectui#13390).
@@ -2128,10 +2158,6 @@ function AdvancedChartImplInner({
21282158
yAxisPositionNotes(yPlacement, categoriesRunDown),
21292159
yAxisUndrawnNotes(yPlacement, yAxes, categoriesRunDown),
21302160
);
2131-
/** The entry a series' marks are measured against — for its value labels. */
2132-
const axisOfSeries = (s: { yAxis?: string }): NormalizedAxis | undefined =>
2133-
hasDualAxis ? (s.yAxis === 'right' ? rightY : leftY) : primaryY;
2134-
21352161
const xTickFormatter = React.useMemo(
21362162
() => formatterFor(xAxisSpec?.format, displayLocale) ?? formatTick,
21372163
[xAxisSpec?.format, displayLocale, formatTick],
@@ -2196,6 +2222,12 @@ function AdvancedChartImplInner({
21962222
() => formatterFor(rightY?.format, displayLocale) ?? formatYTick,
21972223
[rightY?.format, displayLocale, formatYTick],
21982224
);
2225+
/**
2226+
* The tick formatter of the value axis a series' marks are measured
2227+
* against — the one its value labels print with (objectstack#27372).
2228+
*/
2229+
const valueTickFormatterOf = (s: { yAxis?: string }): ((v: any) => string) =>
2230+
hasDualAxis ? (s.yAxis === 'right' ? rightYTickFormatter : leftYTickFormatter) : yTickFormatter;
21992231

22002232
/**
22012233
* Every number plotted in one slot of a dual axis (or on the only axis,
@@ -2317,10 +2349,16 @@ function AdvancedChartImplInner({
23172349
? <Brush dataKey={xAxisKey} height={24} travellerWidth={8} stroke="hsl(var(--muted-foreground))" />
23182350
: null;
23192351

2320-
/** `showDataLabels` prints each point's value on the mark itself. */
2321-
const dataLabel = (formatter?: (v: any) => string) =>
2352+
/**
2353+
* `showDataLabels` prints each point's value on the mark itself, in the
2354+
* units of the value axis the mark is measured against (objectstack#27372):
2355+
* the caller passes that axis's own tick formatter, so an authored
2356+
* `yAxis.format` wins and an axis without one gives its label the compact
2357+
* default (`174K` beside a `400K` tick, never a raw `174000`).
2358+
*/
2359+
const dataLabel = (formatter: (v: any) => string) =>
23222360
showDataLabels
2323-
? <LabelList position="top" className="fill-foreground" fontSize={11} {...(formatter ? { formatter } : {})} />
2361+
? <LabelList position="top" className="fill-foreground" fontSize={DATA_LABEL_FONT_PX} offset={DATA_LABEL_OFFSET_PX} formatter={formatter} />
23242362
: null;
23252363

23262364
// Shared X-axis props for time/categorical axes, in two branches.
@@ -3034,7 +3072,7 @@ function AdvancedChartImplInner({
30343072
happens at chart level because that is the only place the CATEGORY
30353073
is known — a line/area item handler is handed the curve's props and
30363074
no datum. */}
3037-
<ComposedChart ref={setChartSurface} data={data} {...comboClickProps}>
3075+
<ComposedChart ref={setChartSurface} data={data} {...(showDataLabels ? { margin: DATA_LABEL_CHART_MARGIN } : {})} {...comboClickProps}>
30383076
<CartesianGrid {...gridProps} {...valueAxisIdFor(valueAxesHaveIds, categoriesRunDown)} />
30393077
<XAxis dataKey={xAxisKey} {...xAxisCommonProps} />
30403078
{/* A combo always draws both value axes (an authored combo binds an
@@ -3072,7 +3110,7 @@ function AdvancedChartImplInner({
30723110
: (seriesType === 'bar' ? 'left' : 'right');
30733111
const pres = seriesStyle(s, seriesType as any);
30743112
const stackProps = s.stack ? { stackId: String(s.stack) } : {};
3075-
const valueFormatter = formatterFor((yAxisId === 'right' ? rightY : leftY)?.format, displayLocale);
3113+
const valueFormatter = yAxisId === 'right' ? rightYTickFormatter : leftYTickFormatter;
30763114

30773115
if (seriesType === 'line') {
30783116
return (
@@ -3122,7 +3160,7 @@ function AdvancedChartImplInner({
31223160
<ChartFrame title={title} subtitle={subtitle}>
31233161
<ChartFootnote note={joinNotes(xPositionNote, yPositionNote)}>
31243162
<ChartContainer config={config} className={className} {...containerProps}>
3125-
<ChartComponent ref={setChartSurface} data={data} layout={isHorizontal ? 'vertical' : 'horizontal'} {...cartesianClickProps}>
3163+
<ChartComponent ref={setChartSurface} data={data} layout={isHorizontal ? 'vertical' : 'horizontal'} {...(showDataLabels && !isHorizontal ? { margin: DATA_LABEL_CHART_MARGIN } : {})} {...cartesianClickProps}>
31263164
<defs>
31273165
{gradColors.map((c) => (
31283166
<React.Fragment key={gslug(c)}>
@@ -3263,7 +3301,7 @@ function AdvancedChartImplInner({
32633301
// Recharts keys stacking off `stackId`, so the author's group name
32643302
// passes through unchanged.
32653303
const stackProps = s.stack ? { stackId: String(s.stack) } : {};
3266-
const valueFormatter = formatterFor(axisOfSeries(s)?.format, displayLocale);
3304+
const valueFormatter = valueTickFormatterOf(s);
32673305

32683306
if (chartType === 'bar' || chartType === 'horizontal-bar') {
32693307
// One series, one colour: a bar's colour names its SERIES, so a

0 commit comments

Comments
 (0)