You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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.
The text was updated successfully, but these errors were encountered: