Skip to content

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

Description

@avrahamcool

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions