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

Fix the validation on the final item to ensure the percentage always adds up to precisely 100%. #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ShadenAlmuhalhel
Copy link

In the validation function on the final item to ensure the percentage always adds up to precisely 100%, it always takes the last segment regardless of its value. For example, when the total percentage is equal to 101% and the last segment is 0%, it will subtract the 1% from 0% and the result become -1%. Another example when the total percentage is equal to 99% and the two last segments both 0%, it will add 1% to the last segment (0%), now the percentages of the last two segments are different (0% and 1%) even the actual value is the same for both (0). The optimal way is to find the optimal segment (not the last segment in every time), and make Math.ceil or Math.floor instead of Math.round depending on the case (>100 or <100) for the optimal segment chosen. Which it is the same method followed in Excel pie graphs.

To understand how to choose the optimal segment, in this example, we have four rows of data:

Values [48,17,7,0], Total: 72

Percentage | Math.round(Percentage)

66.666 | 67%
23.611 | 24%
9.722 | 10%
0.000 | 0%

The total percentage is 101%. So, we need to determine the optimal segment to re-calculate its percentage using Math.floor to reduce the total percentage to 100%.
So, we need to make a set of percentages that its fraction >= .5, which Math.round(Percentage) rounds the percentage up to the next largest integer. In this case the set become [66.666, 23.661, 9.722]. The next step is to find the most appropriate percentage for this case, which it's the percentage with minimum fraction in the set [23.611].
Re-calculate the percentage: Math.floor(23.611) = 23%.
Now these are the final percentages with total equals to 100%: [67%, 23%, 10%, 0%].

Here is another example of another case, in this example, we have four rows of data:

Values [79,77,30,0], Total: 186

Percentage | Math.round(Percentage)

42.473 | 42%
41.397 | 41%
16.129 | 16%
0.000 | 0%

The total percentage is 99%. So, we need to determine the optimal segment to re-calculate its percentage using Math.ceil to increase the total percentage to 100%.
So, we need to make a set of percentages that its fraction < .5, which Math.round(Percentage) return a value equal to the given percentage. In this case the set become [42.473, 41.397, 16.129, 0.000]. The next step is to find the most appropriate percentage for this case, which it is the percentage with maximum fraction in the set [42.473].
Re-calculate the percentage: Math.ceil(42.473) = 43%.
Now these are the final percentages with total equals to 100%: [43%, 41%, 16%, 0%].

Thank you.
Shaden

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.

None yet

1 participant