-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(xy): non-stacked as percentage (#1504)
Backports the following commits to 38.0.x: - feat: expose computeRatioByGroups fn (#1495)
- Loading branch information
Showing
12 changed files
with
161 additions
and
26 deletions.
There are no files selected for viewing
Binary file modified
BIN
-3.27 KB
(76%)
...r-all-stories-bar-chart-stacked-as-percentage-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.06 KB
...bar-series-stories-stacked-bars-configs-non-stacked-with-external-fn-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.4 KB
...ies-stories-stacked-bars-configs-percentage-stacked-with-external-fn-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.4 KB
...ies-stories-stacked-bars-configs-percentage-stacked-with-internal-fn-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { groupBy, GroupKeysOrKeyFn } from '../../chart_types/xy_chart/utils/group_data_series'; | ||
import { isFiniteNumber } from '../common'; | ||
|
||
/** | ||
* The function computes the participation ratio of a value in the total sum of its membership group. | ||
* It returns a shallow copy of the input array where each object is augmented with the computed ratio. | ||
* | ||
* @remarks | ||
* 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 has a weight of: abs(200) / ( abs(200) + abs(-300) ) * 100% = 40% | ||
* Product B has a weight of: abs(-300) / ( abs(200) + abs(-300) ) * 100% = 60% | ||
* Product A and product B have respectively a weight of 40% and 60% in the formation of the overall total loss of $100. | ||
* | ||
* We don't compute the ratio for non-finite values. In this case, we return the original non-finite value. | ||
* | ||
* If the sum of the group values is 0, each ratio is considered 0. | ||
* | ||
* @public | ||
* @param data - an array of objects | ||
* @param groupAccessors - an array of accessor keys or a fn to describe an unique id for each group | ||
* @param valueAccessor - a fn that returns the value to use | ||
* @param ratioKeyName - the object key used to store the computed ratio | ||
*/ | ||
export function computeRatioByGroups<T extends Record<string, unknown>>( | ||
data: T[], | ||
groupAccessors: GroupKeysOrKeyFn<T>, | ||
valueAccessor: (k: T) => number | null | undefined, | ||
ratioKeyName: string, | ||
) { | ||
return groupBy(data, groupAccessors, true) | ||
.map((groupedData) => { | ||
const groupSum = groupedData.reduce((sum, datum) => { | ||
const value = valueAccessor(datum); | ||
return sum + (isFiniteNumber(value) ? Math.abs(value) : 0); | ||
}, 0); | ||
return groupedData.map((datum) => { | ||
const value = valueAccessor(datum); | ||
return { | ||
...datum, | ||
// if the value is null/undefined we don't compute the ratio, we just return the original null/undefined value | ||
[ratioKeyName]: isFiniteNumber(value) ? (groupSum === 0 ? 0 : Math.abs(value) / groupSum) : value, | ||
}; | ||
}); | ||
}) | ||
.flat(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters