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: rounding values on stacked w percentage charts #1039

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../tsconfig",
"compilerOptions": {
"downlevelIteration": true,
"target": "es5"
"target": "es5",
"resolveJsonModule": true
},
"include": ["../src/**/*", "./**/*"],
"exclude": ["../**/*.test.*"]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions src/chart_types/xy_chart/utils/stacked_series_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {

import { SeriesKey } from '../../../common/series_id';
import { ScaleType } from '../../../scales/constants';
import { clamp } from '../../../utils/common';
import { DataSeries, DataSeriesDatum } from './series';
import { StackMode } from './specs';

Expand Down Expand Up @@ -137,8 +138,14 @@ export function formatStackedDataSeriesValues(
const { initialY0, initialY1, mark, datum, filled } = originalData;
return {
x,
y1,
y0,
/**
* Due to floating point errors, values computed on a stack
* could falls out of the current defined domain boundaries.
* This in particular cause issues with percent stack, where the domain
* is hardcoded to [0,1] and some value can fall outside that domain.
*/
y1: clampIfStackedAsPercentage(y1, stackMode),
y0: clampIfStackedAsPercentage(y0, stackMode),
initialY0,
initialY1,
mark,
Expand All @@ -154,6 +161,10 @@ export function formatStackedDataSeriesValues(
});
}

function clampIfStackedAsPercentage(value: number, stackMode?: StackMode) {
return stackMode === StackMode.Percentage ? clamp(value, 0, 1) : value;
}

function getOffsetBasedOnStackMode(stackMode?: StackMode) {
switch (stackMode) {
case StackMode.Percentage:
Expand Down
4 changes: 4 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export function compareByValueAsc(a: number | string, b: number | string): numbe
return a > b ? 1 : -1;
}

export function clamp(value: number, lowerBound: number, upperBound: number) {
return minValueWithLowerLimit(value, upperBound, lowerBound);
}

/**
* Return the minimum value between val1 and val2. The value is bounded from below by lowerLimit
* @param val1 a numeric value
Expand Down
Loading