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

feat: expose computeRatioByGroups fn #1495

Merged

Conversation

markov00
Copy link
Member

Summary

This PR exposes the function to compute the participation ratio of a value in the total sum of its membership group.
It can be used to compute non-stacked percentage bar charts.
Right now, we only compute such data only for stacked bar charts, but exposing that utility function can also help creating non stacked bar chart.

image

The story was also updated to nicely match the categories used with all the edge cases considered.

Details

As described in the tsdocs: The ratio is computed using absolute values, for example:
Product A made a profit of $200, and product B has a loss of $300. In total, the company lost $100 ($200 – $300).
Product A contributed: abs(200) / ( abs(200) + abs(-300) ) * 100% = 40%
Product B contributed: abs(-300) / ( abs(200) + abs(-300) ) * 100% = 60%
Product A and product B contributed 40% and 60% respectively on a total loss of $100.

We don't compute the ratio for non-finite values. In this case, the original non-finite value is returned.
If the sum of the group values is 0, each ratio is considered 0.

Issues

It enabled the non-stacked percentage bar charts in Kibana elastic/kibana#118525

Checklist

  • The proper chart type label has been added (e.g. :xy, :partition)
  • The proper feature labels have been added (e.g. :interactions, :axis)
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • New public API exports have been added to packages/charts/src/index.ts
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated

@markov00 markov00 added enhancement New feature or request kibana cross issue Has a Kibana issue counterpart :data Data/series/scales related issue :xy Bar/Line/Area chart related labels Nov 22, 2021
@markov00 markov00 marked this pull request as ready for review November 22, 2021 15:08
Copy link
Contributor

@monfera monfera left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

Some comment changes suggested.

Also, this makes me wonder if there'd be utility to a sign-aware version, such that, with the same setup as in the example,

  • Product A contributes (which I think is an OK word here) -200% of the results
  • Product B contributes 300% of the results

The two together contribute 100% of the results. So what's in the PR is a net version, and the above is a gross version

packages/charts/src/utils/common.ts Show resolved Hide resolved
Comment on lines 17 to 21
* The ratio is computed using absolute values.
* Product A made a profit of $200, and product B has a loss of $300. In total, the company lost $100 ($200 – $300).
* Product A contributed: abs(200) / ( abs(200) + abs(-300) ) * 100% = 40%
* Product B contributed: abs(-300) / ( abs(200) + abs(-300) ) * 100% = 60%
* Product A and product B contributed 40% and 60% respectively on a total loss of $100.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice docs! Though as we discussed, I think few people would say that "Product A contributed 40% of the losses" when product A is profitable, while it's fairer to say, "Product A has a weight of 40% in the formation of the overall results"

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right, I've updated it here 9e63025

@markov00
Copy link
Member Author

Also, this makes me wonder if there'd be utility to a sign-aware version, such that, with the same setup as in the example,
Product A contributes (which I think is an OK word here) -200% of the results
Product B contributes 300% of the results
The two together contribute 100% of the results. So what's in the PR is a net version, and the above is a gross version

@monfera you are right, we can probably add that version in a subsequent PR, in the meantime I've created this issue to track it #1500

@markov00 markov00 enabled auto-merge (squash) November 23, 2021 08:47
@markov00 markov00 merged commit 65f4886 into elastic:master Nov 23, 2021
markov00 added a commit to markov00/elastic-charts that referenced this pull request Nov 23, 2021
This commit exposes the function to compute the participation ratio of a value in the total sum of its membership group. It can be used to compute non-stacked percentage bar charts.
markov00 added a commit that referenced this pull request Nov 23, 2021
This commit exposes the function to compute the participation ratio of a value in the total sum of its membership group. It can be used to compute non-stacked percentage bar charts.
nickofthyme pushed a commit that referenced this pull request Nov 23, 2021
# [39.1.0](v39.0.2...v39.1.0) (2021-11-23)

### Features

* expose computeRatioByGroups fn ([#1495](#1495)) ([#1501](#1501)) ([4ec9557](4ec9557))
markov00 added a commit that referenced this pull request Nov 24, 2021
Backports the following commits to 38.0.x:
 - feat: expose computeRatioByGroups fn (#1495)
nickofthyme pushed a commit that referenced this pull request Nov 24, 2021
## [38.0.5](v38.0.4...v38.0.5) (2021-11-24)

### Bug Fixes

* **xy:** non-stacked as percentage ([#1504](#1504)) ([8de10f2](8de10f2)), closes [#1495](#1495)
nickofthyme pushed a commit that referenced this pull request Dec 1, 2021
# [40.1.0](v40.0.0...v40.1.0) (2021-12-01)

### Bug Fixes

* **deps:** update dependency @elastic/eui to ^41.3.0 ([#1506](#1506)) ([d364cc0](d364cc0))
* remove pointer for onElementOver ([#1493](#1493)) ([bf95dbc](bf95dbc))
* **deps:** update dependency @elastic/eui to ^41.2.1 ([#1494](#1494)) ([1c32f82](1c32f82))

### Features

* expose computeRatioByGroups fn ([#1495](#1495)) ([65f4886](65f4886))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:data Data/series/scales related issue enhancement New feature or request kibana cross issue Has a Kibana issue counterpart :xy Bar/Line/Area chart related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants