Skip to content
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

new(shape): support dynamic fill directly in Pie #1225

Merged
merged 4 commits into from
Jun 3, 2021

Conversation

iampueroo
Copy link
Contributor

@iampueroo iampueroo commented May 26, 2021

This is a PR for issue #1193

💥 Breaking Changes

  • None. Previously the fill property supported only string value. It still accepts the string value (but can also accept a string accessor function). I added a test case for both.

🚀 Enhancements

  • This PR enables developers to create a Pie chart with custom colors per wedge by just using the <Pie /> component. Prior, they had to add the <path> elements as children of the <Pie> in order to dynamic colors.

📝 Documentation

  • The fill function can now be a string or a function that receives an PieArcDatum<Datum> and returns a string fill value: (arc: PieArcDatum<Datum>) => string.

🐛 Bug Fix

  • Wouldn't call this a bug fix....

🏠 Internal

  • Um... ?

@@ -7,6 +7,8 @@ import { arc as arcPath, pie as piePath } from '../util/D3ShapeFactories';

export type PieArcDatum<Datum> = PieArcDatumType<Datum>;

type StringAccessor<Datum> = (arc: PieArcDatum<Datum>) => string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question: I chose to provide PieArcDatum since it also includes the index, but it could also be (datum: Datum, index: number). I'd like to get your thoughts on this - PieArcDatum may be unnecessary, especially if the pattern in other components is to merely provide the Datum type.

If not, I think the name shouldn't be arc but instead something like arcDatum or just pieArcDatum - I can change

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good question, I think that PieArcDatum makes sense. Since it's strictly more info, including the computed arc size, it seems like a win to me.

@williaster williaster changed the title fix(pie): support dynamic fill directly in Pie new(shape): support dynamic fill directly in Pie May 27, 2021
@williaster williaster linked an issue May 27, 2021 that may be closed by this pull request
Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Thanks for the great addition @iampueroo! This looks good to me overall, I had one potential simplification (see what you think) but otherwise we can get this in!

@@ -78,14 +83,28 @@ export default function Pie<Datum>({
// eslint-disable-next-line react/jsx-no-useless-fragment
if (children) return <>{children({ arcs, path, pie })}</>;

const fillGenerator = getFillGenerator(fill);
Copy link
Collaborator

Choose a reason for hiding this comment

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

another option that would simplify this slightly is to apply the logic inline rather than always creating the accessor. if we wanted to use the accessor multiple places I think your approach would be better but since we just use it once I think this would be okay

<path
  {...}
  fill={fill == null || typeof fill === 'string' ? fill : fill(arc)}
/>

@@ -7,6 +7,8 @@ import { arc as arcPath, pie as piePath } from '../util/D3ShapeFactories';

export type PieArcDatum<Datum> = PieArcDatumType<Datum>;

type StringAccessor<Datum> = (arc: PieArcDatum<Datum>) => string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good question, I think that PieArcDatum makes sense. Since it's strictly more info, including the computed arc size, it seems like a win to me.

@@ -102,6 +105,22 @@ describe('<Pie />', () => {
expect(fn).toHaveBeenCalled();
});

test('it should accept a custom fill function', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for adding tests!

packages/visx-shape/src/shapes/Pie.tsx Outdated Show resolved Hide resolved
@iampueroo
Copy link
Contributor Author

@williaster thanks for the review. I just pushed this commit which removes the fill generator function and changes a variable name to be clearer. Happy to keep tweaking things as needed.

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Thanks for iterating! this looks good to me, I had one minor comment but I can commit it and merge 🚀

packages/visx-shape/src/shapes/Pie.tsx Outdated Show resolved Hide resolved
@williaster williaster merged commit 1e01d78 into airbnb:master Jun 3, 2021
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

🎉 This PR is included in version v1.12.0 of the packages modified 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pie chart slice color?
2 participants