Skip to content

Commit ca159df

Browse files
authored
Merge pull request #1862 from carbon-design-system/fix-barchart-null
fix(barchart): handle null values
2 parents 88e966b + 53d143d commit ca159df

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/components/BarChartCard/barChartUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const formatChartData = (
105105
type
106106
) => {
107107
const data = [];
108-
if (!isNil(values) || !isEmpty(series)) {
108+
if (!isNil(values) && !isEmpty(series)) {
109109
// grouped or stacked
110110
if (type === BAR_CHART_TYPES.GROUPED || type === BAR_CHART_TYPES.STACKED) {
111111
let uniqueDatasetNames;
@@ -151,7 +151,7 @@ export const formatChartData = (
151151
});
152152
});
153153
} // single bars and not time-based
154-
else if (categoryDataSourceId) {
154+
else if (categoryDataSourceId && Array.isArray(values)) {
155155
const uniqueDatasetNames = [
156156
...new Set(values.map((val) => val[categoryDataSourceId])),
157157
];
@@ -171,7 +171,7 @@ export const formatChartData = (
171171
});
172172
});
173173
} // single bars and time-based
174-
else {
174+
else if (Array.isArray(values)) {
175175
const uniqueDatasetNames = [
176176
...new Set(values.map((val) => val[timeDataSourceId])),
177177
];

src/components/BarChartCard/barChartUtils.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ describe('barChartUtils', () => {
147147
});
148148
});
149149

150+
it('formatChartData handles null values', () => {
151+
const series = [
152+
{
153+
dataSourceId: 'particles',
154+
label: 'Particles',
155+
},
156+
];
157+
// check horizontal layout
158+
expect(
159+
formatChartData(series, null, 'city', null, BAR_CHART_TYPES.GROUPED)
160+
).toEqual([]);
161+
});
162+
150163
it('formatChartData returns formatted data for group-based chart', () => {
151164
const series = [
152165
{

0 commit comments

Comments
 (0)