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

Add percentageDecimals options to pie chart, and use half even rounding #5270

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

thedustin
Copy link
Contributor

@thedustin thedustin commented Feb 5, 2024

📑 Summary

Rounding percentages is hard, as rounded percentages like to not sum up to 100% anymore.

This PR introduced two smaller changes, the use of the half even rounding mode, and a pie chart option to specify the number of digits to show in the chart. Both can be used to reduce or even fix greater or smaller than 100% issues.

Resolves #3987

📏 Design Decisions

Rounding Mode

There are different ways of rounding a number, the default way in JavaScript is the rounding half up mode (see mdn), which has a rounding bias and leads for percentages to sums over 100%. By using the rounding half even mode (also called "bankers' rounding", or "statistician's rounding"), we instead round .5 numbers to the nearest, even number. This rounding mode is to prefer, as it minimizes the overall error, and reduces rounding bias compared to other rounding modes like half up.

As you can see, this still does not make perfect 100%, but will lead to less than 100% instead of greater than 100%. I think this is to prefer, as a sum of greater than 100% always looks more suspicious than the other way around. Also, the question is if we should make this configurable too.

Different behaviour of rounding modes

bug-3987-old-behaviour

bug-3987-new-behaviour

percentageDecimals option

Another way of decreasing percentage sum errors is to less round the numbers, by using/showing more decimal places. At the moment the pie chart does not show decimals at all. I introduced an option percentageDecimals to allow setting the number of decimal places showed in the chart.

As the Intl.NumberFormat component already knows how to use different rounding modes, how to select the number of decimal places, and how to format percentages, I decided to use it here.

The allowed values are taken from the Intl.NumberFormat minimumFractionDigits options.

Pie Chart with decimal places

bug-3987-decimals-1 html
bug-3987-decimals-2

📋 Tasks

Make sure you

Copy link

netlify bot commented Feb 5, 2024

Deploy Preview for mermaid-js failed.

Name Link
🔨 Latest commit 41a3ca2
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/660ef591d72258000829bac1

@github-actions github-actions bot added the Type: Bug / Error Something isn't working or is incorrect label Feb 5, 2024
@thedustin thedustin force-pushed the bug/3987_pie_chart_percentage_sum_gt_100 branch from 8f3e6c4 to 41879ae Compare February 5, 2024 19:30
Copy link

codecov bot commented Feb 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b043d79) 73.73% compared to head (19deb5a) 85.10%.
Report is 2 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #5270       +/-   ##
============================================
+ Coverage    73.73%   85.10%   +11.37%     
============================================
  Files          164      174       +10     
  Lines        13864    11670     -2194     
  Branches       741      845      +104     
============================================
- Hits         10222     9932      -290     
+ Misses        3467     1542     -1925     
- Partials       175      196       +21     
Flag Coverage Δ
e2e 85.10% <100.00%> (+6.95%) ⬆️
unit ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
packages/mermaid/src/diagrams/pie/pieRenderer.ts 98.41% <100.00%> (+0.10%) ⬆️

... and 58 files with indirect coverage changes

@thedustin thedustin force-pushed the bug/3987_pie_chart_percentage_sum_gt_100 branch from 41879ae to 19deb5a Compare February 5, 2024 19:51
Copy link
Member

@sidharthv96 sidharthv96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!
Love the PR, great code, description, tests and docs.

@sidharthv96 sidharthv96 requested a review from a team February 10, 2024 06:14
Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an amazing PR to me! I love the new Intl.NumberFormat, and the great tests and documentation!

The only issue I have is the custom rounding we're doing if pieConfig.percentageDecimals === 0.

I don't think we should do this, since it's a bit confusing to see 4.51% to be rounded to 4%, when you'd expect it to be rounded to 5%, even if you know about half-even rounding.

FYI, I don't think all browsers support the roundingMode: 'halfEven', since it's a pretty new feature, but that's fine, it will just fallback to the current default rounding behaviour 😄.

Comment on lines +128 to +134

// "Half even" rounding mode rounds exactly .5 to the nearest, even number.
// By using 3 decimal places, we can round "close to .5" to the nearest even number too.
if (pieConfig.percentageDecimals === 0) {
num = Number(num.toFixed(3));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure this block is a good idea, since diagrams like below will change.

E.g., if I have 4.51%, I'd expect it to round to 5%, not to 4%.

```mermaid
pie title Pets adopted by volunteers
    "Dogs" : 5.51 %% should round up to 6
    "Cats" : 5.49 %% should round down to 5
    "Rats" : 4.51 %% should round up to 5
    "Mice" : 4.49 %% should round down to 4
    "Other": 80
```
pie title Pets adopted by volunteers
    "Dogs" : 5.51 %% should round up to 6
    "Cats" : 5.49 %% should round down to 5
    "Rats" : 4.51 %% should round up to 5
    "Mice" : 4.49 %% should round down to 4
    "Other": 80

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thedustin how can we fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. I think it's not fixable, as this merge requests explicitly introduced a new (more uncommon, but technically cleaner) way to round values. In this example you are right, you would notice the difference immediately (as you specified percentage values).

My goal was to find a way to resolve the "percentages add up to more than 100%" issue (#3987), but I agree with you that this way of rounding will confuse people.

As rounding always introduces some kind of error, maybe the better solution would be to keep the rounding as it is, and only introducing the decimal digits option. This way you can show the more precise values, when you need to prevent sum over 100%.

packages/mermaid/src/schemas/config.schema.yaml Outdated Show resolved Hide resolved
Co-authored-by: Alois Klink <alois@aloisklink.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug / Error Something isn't working or is incorrect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pie Chart with % sum > 100%
4 participants