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

[Suggestion] allowing sum of sections to be different than 1. #2

Open
avrahamcool opened this issue Jul 9, 2020 · 1 comment
Open

Comments

@avrahamcool
Copy link
Contributor

Sum of section percentages must be equal to 1

you should be able to draw a pie chart even if the sum of total sections is not equal to 1,
by normalizing the given percentages to 1.

example 1 (when the total sum is greater than 1):
[{"color": red, "percentage": 1}, {"color": blue, "percentage": 1}] => should resolve into 0.5 for each of the sections.

example 2 (when the sum is smaller than 1):
[{"color": red, "percentage": 0.5}, {"color": blue, "percentage": 0.25}] => should resolve into 0.66 for red, and 0.33 for blue.

the general way to do that calculation:
you can calculate the total sum of all percentages.
const allPercentages = sections.map(s => s.percentage).reduce((sum, i) => sum + i, 0);

and then recalculate the normalized percentage for each section to be: const normalizedPercentage = percentage / allPercentages.

as a side-effect, you also gain the ability to pass the percentage as "whole numbers" like 50, 75 etc. (representing 50%, 75%).
because now they don't have a restriction that force then to sum to 1.

@avrahamcool avrahamcool changed the title [Suggestion] allowing sum of sections to be different then 1. [Suggestion] allowing sum of sections to be different than 1. Jul 9, 2020
@stal269
Copy link
Owner

stal269 commented Jul 11, 2020

Looks like a great idea, thanks!

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

No branches or pull requests

2 participants