Charts: Fix chart height and size calculations for pie and donut charts#47074
Charts: Fix chart height and size calculations for pie and donut charts#47074adamwoodnz wants to merge 9 commits intotrunkfrom
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
1 file is newly checked for coverage.
|
de190fa to
049e63e
Compare
Pie and semi-circle charts need both width and height to compute correct chart dimensions; the previous hook only exposed height. Renaming to useElementSize and returning [ref, width, height] lets callers use either or both dimensions (bar/line/semi-circle skip width via destructuring; pie chart uses both for size calculations). Co-authored-by: Cursor <cursoragent@cursor.com>
The `size` prop was being used for both container dimensions and pie diameter, causing incorrect sizing in responsive contexts. The pie would overflow or not fill its container because `withResponsive` set width/height from `size`, conflating two distinct concerns. Now `size` only constrains the maximum pie diameter while container dimensions come from width/height props or responsive measurement. The pie shrinks to fit when the container is smaller than `size`, and fills available space when `size` is omitted. Replace the outer div with a WordPress Stack component for proper gap control via design tokens, and position the legend through conditional rendering instead of CSS flex-direction reversal. Co-authored-by: Cursor <cursoragent@cursor.com>
Loading global CSS like design-tokens.css is the responsibility of the consuming application, not individual chart components. Co-authored-by: Cursor <cursoragent@cursor.com>
The size prop no longer controls container dimensions — it constrains the maximum pie diameter. Width and height now control the container. Updated JSDoc, API reference, stories argTypes, and the docs MDX to reflect the new behavior and remove the broken Responsiveness story reference. Co-authored-by: Cursor <cursoragent@cursor.com>
The tooltip stories still referenced the old `size` prop and set `resize: 'none'`, both of which are no longer needed after decoupling pie size from container dimensions. Switches to `height` and lets container sizing handle the rest. Co-authored-by: Cursor <cursoragent@cursor.com>
Blanking labels with `label: ''` broke pie chart color indexing — `findIndex` by label always returned 0, so all segments got the same color. Use original data with `showLabels: false` instead. Also removes redundant Doughnut/WithTooltipsDoughnut stories, simplifies composition and interactive legend examples, and switches ErrorStates from `size` to `height`. Co-authored-by: Cursor <cursoragent@cursor.com>
f7358d9 to
43245a3
Compare
The hook was renamed and now returns [ref, width, height] instead of [ref, height]. Update mocks in bar-chart and line-chart tests to use the new module path and 3-tuple return value. Also update with-responsive tests: size no longer controls wrapper dimensions (only width/height props do), and size is passed through directly rather than derived from container width. Co-authored-by: Cursor <cursoragent@cursor.com>
Demonstrates the explicit `size` prop on a donut chart with a thin ring, hidden labels, and centered text — useful for verifying the fixed-size rendering path decoupled from container dimensions. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors chart sizing and responsive behavior to fix issues with pie and donut chart height calculations. It generalizes the useElementHeight hook to useElementSize for measuring both width and height, and decouples the size prop from container dimensions so it now specifically controls the maximum pie diameter.
Changes:
- Renamed and enhanced
useElementHeighttouseElementSizeto measure both width and height of DOM elements - Refactored pie chart layout to use WordPress UI Stack component and measure available space dynamically
- Updated
sizeprop semantics: now controls maximum chart element dimensions (pie diameter) rather than container dimensions, with container controlled bywidth/heightprops
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
projects/js-packages/charts/src/hooks/use-element-size.ts |
Renamed from use-element-height.ts and enhanced to measure both width and height, returning a 3-value tuple |
projects/js-packages/charts/src/hooks/test/use-element-size.test.tsx |
Updated all tests to handle the new hook signature with width and height |
projects/js-packages/charts/src/hooks/index.ts |
Updated export to use new useElementSize name |
projects/js-packages/charts/src/types.ts |
Updated prop documentation to clarify that size controls chart element dimensions (pie diameter), not container |
projects/js-packages/charts/src/charts/private/with-responsive/with-responsive.tsx |
Removed size from container dimension calculations - now only used as chart-level prop |
projects/js-packages/charts/src/charts/private/with-responsive/test/with-responsive.test.tsx |
Updated tests to reflect new size prop behavior |
projects/js-packages/charts/src/charts/pie-chart/pie-chart.tsx |
Refactored to use Stack component, measure available space with svg-wrapper, and calculate pie size from measured dimensions |
projects/js-packages/charts/src/charts/pie-chart/pie-chart.module.scss |
Added svg-wrapper styles with flex properties and removed manual gap/legend-top classes |
projects/js-packages/charts/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.tsx |
Updated to use useElementSize with 3-value destructuring |
projects/js-packages/charts/src/charts/line-chart/line-chart.tsx |
Updated to use useElementSize with 3-value destructuring |
projects/js-packages/charts/src/charts/line-chart/test/line-chart.test.tsx |
Updated mock to return 3 values for useElementSize |
projects/js-packages/charts/src/charts/bar-chart/bar-chart.tsx |
Updated to use useElementSize with 3-value destructuring |
projects/js-packages/charts/src/charts/bar-chart/test/bar-chart.test.tsx |
Updated mock to return 3 values for useElementSize |
projects/js-packages/charts/src/charts/pie-chart/stories/index.stories.tsx |
Updated stories to demonstrate new sizing behavior, added WithSize and FixedDimensions stories, removed Responsiveness story |
projects/js-packages/charts/src/charts/pie-chart/stories/index.docs.mdx |
Updated documentation to explain new size prop semantics and responsive behavior |
projects/js-packages/charts/src/charts/pie-chart/stories/index.api.mdx |
Updated API documentation to clarify size, width, height props and added gap prop |
projects/js-packages/charts/src/charts/pie-chart/stories/tooltip.stories.tsx |
Updated tooltip stories to use height instead of size for container dimensions |
projects/js-packages/charts/src/charts/pie-chart/stories/donut.stories.tsx |
Updated donut stories, removed redundant stories, simplified composition examples |
projects/js-packages/charts/changelog/charts-164-fix-chart-height-and-size-calculations-for-pie-chart-and |
Added changelog entry describing the fix |
projects/js-packages/charts/src/charts/pie-chart/stories/index.stories.tsx
Outdated
Show resolved
Hide resolved
After decoupling `size` from container dimensions, the ErrorStates
examples were still passing `size={300}` alongside `height={300}`,
which is redundant — `size` constrains the pie diameter while
`height` constrains the container, and both set to 300 is
effectively a no-op for `size`.
Drop `size` from all four error-state examples so they size
responsively, keeping only `height={300}` on Single Data Point
where a fixed container dimension is useful.
Refs: #47074 (comment)
Co-authored-by: Cursor <cursoragent@cursor.com>
Fixes CHARTS-164
Overview
The
sizeprop on pie and donut charts was pulling double duty — controlling both the container dimensions (viawithResponsive) and the pie diameter. This caused incorrect sizing in responsive contexts: charts would either overflow their container or fail to fill available space, because the responsive wrapper and the pie itself were fighting over the same value.This PR separates those concerns.
sizenow only constrains the maximum pie diameter, whilewidth/heightcontrol the container. A new SVG wrapper element measures available space so the pie can shrink to fit or fill its container automatically. The underlyinguseElementHeighthook is generalized touseElementSizeto support these two-dimensional measurements across all chart types.Proposed changes:
useElementHeighthook touseElementSize, adding width measurement alongside height. This enables components to measure both dimensions of their container.sizeprop now controls only the pie diameter (not the responsive wrapper), and the pie auto-sizes to fit available space when nosizeis specified.Stackcomponent from@wordpress/uifor proper gap control and column layout.gapprop toPieChartfor controlling spacing between chart elements (SVG, legend, children) using WordPress design system tokens.withResponsive) to stop usingsizefor container dimensions —sizeis now passed through as a chart-level prop only.design-tokensCSS import from the pie chart — loading global CSS is the responsibility of the consuming application, not individual components.label: ''causedfindIndexto always return 0, giving all segments the same color. UsesshowLabels: falseinstead.useElementSizereturn signature[ref, width, height].Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
cd projects/js-packages/charts && pnpm run storybooksizeprop)useElementSize)Linear Issue: CHARTS-164
Made with Cursor