Charts: Ensure chart and legend combined height equals the specified height prop #43844
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes CHARTS-30
Proposed changes:
When users set the height property for line/bar charts, they expect the chart and legend to be aligned with the container height, not just the chart height.
This PR dynamically measures the legend’s height and subtracts it from the chart area when rendering
<XYChart />
, so the total rendered height (chart + legend) matches the specified height prop.The enhancement fixes layout issues that occur when charts are placed in fixed-height containers. For example, when a chart is placed in a container with a fixed height, the chart and legend may exceed the container height, causing the legend to be invisible.
Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
Testing instructions:
pnpm --filter="@automattic/charts" storybook
Before:

After:

Vertical legend:
I initially had concerns about potential layout shifts during chart rendering, but testing shows minimal impact. However, if layout shifts become problematic, we could consider adding an optional prop allowing consumers to specify an initial legend height to minimize any initial rendering adjustments.
Screen.Recording.2025-06-09.at.16.13.31.mov