Skip to content

Commit 77c3146

Browse files
authored
fix: stacked percentage with zero values (#622)
This commit review the work done in #618 to fix the following case: if we are stacking areas in percentage mode and the values for all series are 0 we shall use 0% as value for each point in that bucket. In the aforementioned PR we were handling that case as null, completely disconnecting the previous data points from the following ones on the chart.
1 parent 699783e commit 77c3146

5 files changed

+14
-14
lines changed

src/chart_types/xy_chart/rendering/rendering.areas.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,10 @@ describe('Rendering points - areas', () => {
11141114
{
11151115
datum: [1546300800000, 0],
11161116
initialY0: null,
1117-
initialY1: null,
1117+
initialY1: 0,
11181118
x: 1546300800000,
11191119
y0: null,
1120-
y1: null,
1120+
y1: 0,
11211121
},
11221122
{
11231123
datum: [1546387200000, 5],

src/chart_types/xy_chart/utils/stacked_series_utils.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,17 @@ describe('Stacked Series Utils', () => {
449449
const stackedValues: Map<any, StackedValues> = new Map();
450450
stackedValues.set(1, {
451451
values: [0, 0, 0],
452-
percent: [null, null, null],
452+
percent: [0, 0, 0],
453453
total: 0,
454454
});
455455
const formattedDatum = getStackedFormattedSeriesDatum({ x: 1, y1: 0 }, stackedValues, 0, false, true);
456456
expect(formattedDatum).toEqual({
457457
datum: undefined,
458458
initialY0: null,
459-
initialY1: null,
459+
initialY1: 0,
460460
x: 1,
461461
y0: null,
462-
y1: null,
462+
y1: 0,
463463
});
464464
});
465465
});

src/chart_types/xy_chart/utils/stacked_series_utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ScaleType } from '../../../scales';
2222
/** @internal */
2323
export interface StackedValues {
2424
values: number[];
25-
percent: Array<number | null>;
25+
percent: Array<number>;
2626
total: number;
2727
}
2828

@@ -105,7 +105,7 @@ export function computeYStackedMapValues(
105105
);
106106
const percent = stackArray.values.map((value) => {
107107
if (stackArray.total === 0) {
108-
return null;
108+
return 0;
109109
}
110110
return value / stackArray.total;
111111
});
@@ -192,11 +192,11 @@ export function getStackedFormattedSeriesDatum(
192192
let y1: number | null = null;
193193
let y0: number | null | undefined = null;
194194
if (isPercentageMode) {
195-
if (data.y1 != null && stack.total !== 0) {
196-
y1 = data.y1 / stack.total;
195+
if (data.y1 != null) {
196+
y1 = stack.total !== 0 ? data.y1 / stack.total : 0;
197197
}
198-
if (data.y0 != null && stack.total !== 0) {
199-
y0 = data.y0 / stack.total;
198+
if (data.y0 != null) {
199+
y0 = stack.total !== 0 ? data.y0 / stack.total : 0;
200200
}
201201
} else {
202202
y1 = data.y1;

stories/area/8_stacked_percentage_zeros.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const DATA = [
117117
[1585735200000, 83],
118118
[1585749600000, 46],
119119
[1585764000000, 7],
120-
[1585778400000, 2],
120+
[1585778400000, null],
121121
[1585792800000, 12],
122122
[1585807200000, 44],
123123
[1585821600000, 84],
@@ -172,7 +172,7 @@ const DATA = [
172172
[1585735200000, 8],
173173
[1585749600000, 2],
174174
[1585764000000, 0],
175-
[1585778400000, 0],
175+
[1585778400000, null],
176176
[1585792800000, 0],
177177
[1585807200000, 1],
178178
[1585821600000, 6],
@@ -227,7 +227,7 @@ const DATA = [
227227
[1585735200000, 3],
228228
[1585749600000, 1],
229229
[1585764000000, 1],
230-
[1585778400000, 0],
230+
[1585778400000, null],
231231
[1585792800000, 0],
232232
[1585807200000, 2],
233233
[1585821600000, 1],

0 commit comments

Comments
 (0)