Skip to content

Commit 811ee90

Browse files
authored
fix: render stacked bar with stringified values (#488)
The stacked bars are computed adding up the previous value with the current one. This works fine if the passed values are numbers. If the number is codified as a string, the resulting stacked value is a wrongly concatenated string of values. This commit cast every y value to a number, if NaN or null it will use null fix #487
1 parent 3e049af commit 811ee90

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ describe('Rendering points - areas', () => {
10011001
expect(renderedArea.areaGeometry.seriesIdentifier.specId).toEqual(SPEC_ID);
10021002
expect(renderedArea.areaGeometry.transform).toEqual({ x: 0, y: 0 });
10031003
});
1004-
test('Can render points points', () => {
1004+
test('Can render points', () => {
10051005
const {
10061006
areaGeometry: { points },
10071007
indexedGeometries,

src/chart_types/xy_chart/utils/__snapshots__/series.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Array [
1111
},
1212
"x": 0,
1313
"y0": null,
14-
"y1": undefined,
14+
"y1": null,
1515
},
1616
],
1717
"key": "spec{spec1}yAccessor{y1}splitAccessors{y-1}",
@@ -34,7 +34,7 @@ Array [
3434
},
3535
"x": 1,
3636
"y0": null,
37-
"y1": undefined,
37+
"y1": null,
3838
},
3939
],
4040
"key": "spec{spec1}yAccessor{y1}splitAccessors{y-2}",
@@ -57,7 +57,7 @@ Array [
5757
},
5858
"x": 2,
5959
"y0": null,
60-
"y1": undefined,
60+
"y1": null,
6161
},
6262
],
6363
"key": "spec{spec1}yAccessor{y1}splitAccessors{y-10}",
@@ -80,7 +80,7 @@ Array [
8080
},
8181
"x": 3,
8282
"y0": null,
83-
"y1": undefined,
83+
"y1": null,
8484
},
8585
],
8686
"key": "spec{spec1}yAccessor{y1}splitAccessors{y-6}",

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RawDataSeries,
1010
splitSeries,
1111
SeriesIdentifier,
12+
cleanDatum,
1213
} from './series';
1314
import { BasicSeriesSpec, LineSeriesSpec, SpecTypes, SeriesTypes } from './specs';
1415
import { formatStackedDataSeriesValues } from './stacked_series_utils';
@@ -560,4 +561,24 @@ describe('Series', () => {
560561

561562
expect(getSortedDataSeriesColorsValuesMap(seriesCollection)).toEqual(undefinedSortedColorValues);
562563
});
564+
test('clean datum shall parse string as number for y values', () => {
565+
let datum = cleanDatum([0, 1, 2], 0, 1, 2);
566+
expect(datum.y1).toBe(1);
567+
expect(datum.y0).toBe(2);
568+
datum = cleanDatum([0, '1', 2], 0, 1, 2);
569+
expect(datum.y1).toBe(1);
570+
expect(datum.y0).toBe(2);
571+
572+
datum = cleanDatum([0, '1', '2'], 0, 1, 2);
573+
expect(datum.y1).toBe(1);
574+
expect(datum.y0).toBe(2);
575+
576+
datum = cleanDatum([0, 1, '2'], 0, 1, 2);
577+
expect(datum.y1).toBe(1);
578+
expect(datum.y0).toBe(2);
579+
580+
datum = cleanDatum([0, 'invalid', 'invalid'], 0, 1, 2);
581+
expect(datum.y1).toBe(null);
582+
expect(datum.y0).toBe(null);
583+
});
563584
});

src/chart_types/xy_chart/utils/series.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,29 @@ function getSplitAccessors(datum: Datum, accessors: Accessor[] = []): Map<string
206206
/**
207207
* Reformat the datum having only the required x and y property.
208208
*/
209-
function cleanDatum(datum: Datum, xAccessor: Accessor, yAccessor: Accessor, y0Accessor?: Accessor): RawDataSeriesDatum {
209+
export function cleanDatum(
210+
datum: Datum,
211+
xAccessor: Accessor,
212+
yAccessor: Accessor,
213+
y0Accessor?: Accessor,
214+
): RawDataSeriesDatum {
210215
const x = datum[xAccessor];
211-
const y1 = datum[yAccessor];
216+
const y1 = castToNumber(datum[yAccessor]);
212217
const cleanedDatum: RawDataSeriesDatum = { x, y1, datum, y0: null };
213218
if (y0Accessor) {
214-
cleanedDatum.y0 = datum[y0Accessor];
219+
cleanedDatum.y0 = castToNumber(datum[y0Accessor]);
215220
}
216221
return cleanedDatum;
217222
}
218223

224+
function castToNumber(value: any): number | null {
225+
if (value === null || value === undefined) {
226+
return null;
227+
}
228+
const num = Number(value);
229+
return isNaN(num) ? null : num;
230+
}
231+
219232
export function getFormattedDataseries(
220233
specs: YBasicSeriesSpec[],
221234
dataSeries: Map<SpecId, RawDataSeries[]>,

0 commit comments

Comments
 (0)