-
Notifications
You must be signed in to change notification settings - Fork 826
Charts: adds legend composition for pie chart family #44796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Charts: adds legend composition for pie chart family #44796
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 2 files.
Full summary · PHP report · JS report Coverage check overridden by
Coverage tests to be added later
|
6be5518
to
06c3060
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a composition API for the pie chart family (PieChart and PieSemiCircleChart) allowing developers to use <PieChart.Legend />
syntax for more flexible chart composition, while maintaining full backward compatibility with existing prop-based approaches.
- Adds compound component support (
PieChart.SVG
,PieChart.HTML
,PieChart.Legend
) for flexible composition - Optimizes child processing performance by replacing dual iterations with single-pass processing
- Reorganizes Storybook examples to demonstrate both traditional and composition API patterns
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
pie-chart.tsx | Implements composition API with compound components and optimized child processing |
pie-semi-circle-chart.tsx | Adds composition API support matching PieChart implementation |
index.stories.tsx (both) | Adds Storybook examples demonstrating composition API usage |
donut.stories.tsx | Updates donut chart stories with composition API examples |
index.docs.mdx | Documents the new composition API with usage examples |
composition-api.test.tsx | Adds comprehensive tests for the new composition functionality |
index.tsx | Exports both responsive and unresponsive chart variants |
changelog file | Documents the feature addition |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
</h3> | ||
</PieChartUnresponsive.HTML> | ||
|
||
<PieChartUnresponsive.SVG> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using PieChartUnresponsive
instead of the standard PieChart
component creates inconsistency in the API examples. Stories should demonstrate the primary API that users will consume.
<PieChartUnresponsive.SVG> | |
<PieChart | |
data={ chartData } | |
size={ 400 } | |
withTooltips={ true } | |
thickness={ 0.7 } | |
> | |
<PieChart.HTML> | |
<h3 style={ { textAlign: 'center', marginBottom: '20px' } }> | |
Device Usage Distribution | |
</h3> | |
</PieChart.HTML> | |
<PieChart.SVG> |
Copilot uses AI. Check for mistakes.
@@ -55,6 +58,20 @@ interface PieChartProps extends BaseChartProps< DataPointPercentage[] > { | |||
children?: ReactNode; | |||
} | |||
|
|||
// Base props type with optional responsive properties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's consolidate the code with semi circle chart in follow ups, particularly L61-L129
@@ -120,6 +167,40 @@ const PieChartInternal = ( { | |||
|
|||
const { isValid, message } = validateData( data ); | |||
|
|||
// Process children to extract compound components | |||
const { svgChildren, htmlChildren, otherChildren } = useMemo( () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could be reused as well in semi circle chart
Children.forEach( child.props.children, htmlChild => { | ||
html.push( htmlChild ); | ||
} ); | ||
} else if ( child.type === Group ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we need this, do we?
Children.forEach( child.props.children, htmlChild => { | ||
html.push( htmlChild ); | ||
} ); | ||
} else if ( child.type === Group ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well and looks good! Left a couple of non-blocking comments. Let's ship and iterate 🚀
06c3060
to
f2f0171
Compare
Fixes CHARTS-90: Add composition API for pie chart
family
Proposed changes:
<PieChart.Legend />
syntaxcomponents render outside
Children.map
iterations with a single passexamples
showLegend
prop approachTechnical details:
React children
useMemo
andChildren.forEach
forbetter performance
<Group>
render inside the SVG context while React componentsrender outside
Other information:
break them?
Jetpack product discussion
Charts package enhancement - adding composition API for pie chart family to match
LineChart and BarChart functionality, enabling more flexible chart composition patterns.
Does this pull request change what data or activity we track or use?
No, this PR only adds a new API pattern for legend composition and optimizes existing
functionality without changing any data handling or tracking.
Testing instructions:
<PieChart.Legend />
and<PieSemiCircleChart.Legend />
showLegend
prop approachSpecific test cases: