Skip to content

feat: Support string value in Pie outerRadius callbacks#6191

Merged
ckifer merged 3 commits intorecharts:mainfrom
rephaelberkooz:reph/outer-radius-string-callback
Aug 11, 2025
Merged

feat: Support string value in Pie outerRadius callbacks#6191
ckifer merged 3 commits intorecharts:mainfrom
rephaelberkooz:reph/outer-radius-string-callback

Conversation

@rephaelberkooz
Copy link
Contributor

Description

Adds support for string values as returned by outerRadius callback in Pie.

Related Issue

5998

Motivation and Context

Currently, Pie supports number and string values, which are handled by the getPercentValue() util. Additionally, Pie supports callbacks that return number values, which are not handled and instead returned directly

This PR adds support for callbacks that return string values, by using getPercentValue() to handle the returned string/number from the callback.

How Has This Been Tested?

Added unit test to Pie's test suite, testing the execution of outerRadius callbacks that return numbers and strings.

I ran the tests locally, using npm run test.

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have added a storybook story or extended an existing story to show my changes

@rephaelberkooz
Copy link
Contributor Author

Updates to recharts.org documentation coming soon

@rephaelberkooz
Copy link
Contributor Author

One question: do we have a pattern for allowing users to create custom functions in storybook? I was wondering how to show the user-defined functions as a parameter in the storybook example

@codecov
Copy link

codecov bot commented Aug 7, 2025

Bundle Report

Changes will increase total bundle size by 118 bytes (0.0%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.03MB 67 bytes (0.01%) ⬆️
recharts/bundle-es6 888.23kB 51 bytes (0.01%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
polar/Pie.js 67 bytes 22.69kB 0.3%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
polar/Pie.js 51 bytes 21.03kB 0.24%

@codecov
Copy link

codecov bot commented Aug 7, 2025

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6191   +/-   ##
=======================================
  Coverage   96.74%   96.74%           
=======================================
  Files         222      222           
  Lines       20019    20019           
  Branches     4140     4140           
=======================================
  Hits        19368    19368           
  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.

@PavelVanecek
Copy link
Collaborator

pattern for allowing users to create custom functions in storybook

https://storybook.js.org/docs/writing-stories/args#mapping-to-complex-arg-values

storybook/stories/Examples/Synchronised.stories.tsx

@PavelVanecek PavelVanecek requested a review from Copilot August 7, 2025 23:14
@PavelVanecek
Copy link
Collaborator

Thanks for the PR!

Copy link
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 adds support for string values as return types from outerRadius callbacks in Pie charts, extending the existing functionality that only supported number returns from callbacks.

  • Updated type definitions to allow callbacks to return both numbers and strings
  • Modified the getOuterRadius function to handle string returns from callbacks using getPercentValue()
  • Added comprehensive unit tests for both string and number callback returns

Reviewed Changes

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

File Description
src/polar/Pie.tsx Updated getOuterRadius function to use getPercentValue() for callback return values and modified type definitions
src/state/types/PieSettings.ts Updated type definition to allow callbacks to return both numbers and strings
test/polar/Pie.spec.tsx Added unit tests for string and number callback returns in outerRadius
storybook/stories/API/polar/Pie.stories.tsx Updated documentation to clarify callback can return strings or numbers

{ name: 'C', value: 20 },
];

const outerRadiusCallback = (dataPoint: any) => {
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

Consider using a more specific type instead of any for the dataPoint parameter. Based on the test data structure, it should be { name: string; value: number }.

Suggested change
const outerRadiusCallback = (dataPoint: any) => {
const outerRadiusCallback = (dataPoint: { name: string; value: number }) => {

Copilot uses AI. Check for mistakes.
{ name: 'C', value: 20 },
];

const outerRadiusCallback = (dataPoint: any) => {
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

Consider using a more specific type instead of any for the dataPoint parameter. Based on the test data structure, it should be { name: string; value: number }.

Suggested change
const outerRadiusCallback = (dataPoint: any) => {
const outerRadiusCallback = (dataPoint: { name: string; value: number }) => {

Copilot uses AI. Check for mistakes.
const sectorPaths = Array.from(sectors).map(sector => sector.getAttribute('d'));
expect(sectorPaths[0]).not.toBe(sectorPaths[1]);
expect(sectorPaths[1]).not.toBe(sectorPaths[2]);
expect(sectorPaths[0]).not.toBe(sectorPaths[2]);
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

[nitpick] The test logic for verifying different sector sizes could be more robust. Consider checking actual computed radius values or path dimensions instead of just comparing path strings, as path differences don't guarantee the intended radius behavior.

Suggested change
expect(sectorPaths[0]).not.toBe(sectorPaths[2]);
// Helper to extract the outer arc endpoint from SVG path and compute radius
function getOuterRadiusFromPath(d: string | null, cx: number, cy: number): number | null {
if (!d) return null;
// Find the first arc command (A) and its endpoint
// Example: M x0 y0 A rx ry xAxisRotation largeArcFlag sweepFlag x1 y1 ...
const arcMatch = d.match(/A\s*[\d.]+\s*[\d.]+\s*[\d.]+\s*[01]\s*[01]\s*([\d.]+)\s*([\d.]+)/);
if (!arcMatch) return null;
const x = parseFloat(arcMatch[1]);
const y = parseFloat(arcMatch[2]);
// Compute distance from center
return Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y - cy, 2));
}
const cx = 250, cy = 250;
const expectedRadii = [0.6 * 250, 0.8 * 250, 1.0 * 250]; // 60%, 80%, 100% of 250
const sectorPaths = Array.from(sectors).map(sector => sector.getAttribute('d'));
const computedRadii = sectorPaths.map(d => getOuterRadiusFromPath(d, cx, cy));
// Check that computed radii are close to expected
computedRadii.forEach((radius, i) => {
expect(radius).not.toBeNull();
expect(radius!).toBeCloseTo(expectedRadii[i], 1);
});

Copilot uses AI. Check for mistakes.
const sectorPaths = Array.from(sectors).map(sector => sector.getAttribute('d'));
expect(sectorPaths[0]).not.toBe(sectorPaths[1]);
expect(sectorPaths[1]).not.toBe(sectorPaths[2]);
expect(sectorPaths[0]).not.toBe(sectorPaths[2]);
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

[nitpick] The test logic for verifying different sector sizes could be more robust. Consider checking actual computed radius values or path dimensions instead of just comparing path strings, as path differences don't guarantee the intended radius behavior.

Suggested change
expect(sectorPaths[0]).not.toBe(sectorPaths[2]);
function extractRadiusFromPath(d: string | null, cx: number, cy: number): number | null {
if (!d) return null;
const match = d.match(/M\s*([-\d.]+)\s*,?\s*([-\d.]+)/);
if (!match) return null;
const x = parseFloat(match[1]);
const y = parseFloat(match[2]);
return Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y - cy, 2));
}
const expectedRadii = [100, 150, 200];
const actualRadii = Array.from(sectors).map((sector, i) =>
extractRadiusFromPath(sector.getAttribute('d'), 250, 250)
);
actualRadii.forEach((radius, i) => {
expect(radius).not.toBeNull();
expect(radius!).toBeCloseTo(expectedRadii[i], 1);
});

Copilot uses AI. Check for mistakes.
@ckifer ckifer merged commit b9a51d4 into recharts:main Aug 11, 2025
20 checks passed
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.

4 participants