1212
Label viewbox in polar charts by PavelVanecek · Pull Request #6180 · recharts/recharts · GitHub
Skip to content

Label viewbox in polar charts#6180

Merged
ckifer merged 3 commits intomainfrom
label-content-viewbox
Aug 5, 2025
Merged

Label viewbox in polar charts#6180
ckifer merged 3 commits intomainfrom
label-content-viewbox

Conversation

@PavelVanecek
Copy link
Copy Markdown
Collaborator

Description

I am not too happy about this hack but it appears to be working fine for now.

Related Issue

#6030

@PavelVanecek PavelVanecek requested a review from Copilot August 5, 2025 14:39
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 addresses an issue with Label viewbox handling in polar charts by implementing a conditional logic fix to determine the appropriate viewbox based on label position. The main change adds logic to use cartesian viewbox for center-positioned labels and polar viewbox for other positions.

  • Introduces conditional viewbox resolution in Label component based on position prop
  • Adds comprehensive test coverage for Label component behavior in PieChart and LineChart contexts
  • Updates PieChart storybook stories to use new data format and improved styling

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/component/Label.tsx Implements viewbox resolution fix with conditional logic for polar vs cartesian charts
test/component/Label.spec.tsx Adds extensive test coverage for Label component with different chart types and positions
storybook/stories/API/chart/PieChart.stories.tsx Updates PieChart stories with new data format and enhanced donut chart styling

Comment thread src/component/Label.tsx
Comment on lines +414 to +415
const resolvedViewBox = position === 'center' ? cartesianViewBox : (polarViewBox ?? cartesianViewBox);

Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The viewbox resolution logic is complex and could benefit from extraction into a helper function. Consider creating a resolveViewBox(position, polarViewBox, cartesianViewBox) function to improve readability and testability.

Suggested change
const resolvedViewBox = position === 'center' ? cartesianViewBox : (polarViewBox ?? cartesianViewBox);
const resolvedViewBox = resolveViewBox(position, polarViewBox, cartesianViewBox);

Copilot uses AI. Check for mistakes.
Comment thread src/component/Label.tsx
Comment on lines +410 to +412
* I am not proud about this solution but it's a quick fix for https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460.
* What we should really do is split Label into two components: CartesianLabel and PolarLabel and then handle their respective viewBoxes separately.
* Also other components should set its own viewBox in a context so that we can fix https://github.com/recharts/recharts/issues/6156
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment acknowledges this is a temporary solution but lacks specificity about why this approach was chosen and what makes it problematic. Consider documenting the specific issues with the current approach and timeline for the proper solution.

Suggested change
* I am not proud about this solution but it's a quick fix for https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460.
* What we should really do is split Label into two components: CartesianLabel and PolarLabel and then handle their respective viewBoxes separately.
* Also other components should set its own viewBox in a context so that we can fix https://github.com/recharts/recharts/issues/6156
* Temporary workaround: This logic mixes cartesian and polar viewBox handling in a single Label component.
* This was chosen to quickly address https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460,
* but it introduces complexity and increases the risk of bugs, as the Label component now has to handle two fundamentally different coordinate systems.
* This makes the code harder to maintain and reason about, and can lead to incorrect label rendering in edge cases.
* The proper solution is to split Label into CartesianLabel and PolarLabel components, each handling their own viewBox logic.
* Additionally, components should set their own viewBox in context to resolve https://github.com/recharts/recharts/issues/6156.
* Plan: Refactoring to separate components is targeted for the next major release (v3.0), pending design discussion and community feedback.

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.74%. Comparing base (e56913d) to head (deab113).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6180   +/-   ##
=======================================
  Coverage   96.74%   96.74%           
=======================================
  Files         222      222           
  Lines       19985    19991    +6     
  Branches     4137     4138    +1     
=======================================
+ Hits        19334    19340    +6     
  Misses        645      645           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 5, 2025

Bundle Report

Changes will increase total bundle size by 1.44kB (0.06%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.03MB 689 bytes (0.07%) ⬆️
recharts/bundle-es6 886.21kB 689 bytes (0.08%) ⬆️
recharts/bundle-umd 489.1kB 57 bytes (0.01%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
component/Label.js 689 bytes 16.14kB 4.46%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 57 bytes 489.1kB 0.01%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
component/Label.js 689 bytes 17.52kB 4.09%

Comment thread src/component/Label.tsx
const viewBox = viewBoxFromProps || polarViewBox || viewBoxFromContext;
/*
* I am not proud about this solution but it's a quick fix for https://github.com/recharts/recharts/issues/6030#issuecomment-3155352460.
* What we should really do is split Label into two components: CartesianLabel and PolarLabel and then handle their respective viewBoxes separately.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo splitting them would make the API less straightforward.

But can't do it now anyways since breaking changes

@ckifer ckifer merged commit c6c26b5 into main Aug 5, 2025
25 of 27 checks passed
@PavelVanecek PavelVanecek deleted the label-content-viewbox branch August 5, 2025 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants