Fix the validation on the final item to ensure the percentage always adds up to precisely 100%. #178
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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