diff --git a/src/components/BarChartCard/BarChartCard.jsx b/src/components/BarChartCard/BarChartCard.jsx index 67b4f8520e..67e77c240f 100644 --- a/src/components/BarChartCard/BarChartCard.jsx +++ b/src/components/BarChartCard/BarChartCard.jsx @@ -121,9 +121,9 @@ const BarChartCard = ({ ); // Set the colors for each dataset - const uniqueDatasets = [ - ...new Set(chartData.map((dataset) => dataset.group)), - ]; + const uniqueDatasets = !isAllValuesEmpty + ? [...new Set(chartData.map((dataset) => dataset.group))] + : []; const colors = !isAllValuesEmpty ? formatColors(series, uniqueDatasets, isEditable) : null; diff --git a/src/components/BarChartCard/barChartUtils.js b/src/components/BarChartCard/barChartUtils.js index 558cc80471..e17caca36e 100644 --- a/src/components/BarChartCard/barChartUtils.js +++ b/src/components/BarChartCard/barChartUtils.js @@ -104,8 +104,9 @@ export const formatChartData = ( timeDataSourceId, type ) => { - const data = []; - if (!isNil(values) || !isEmpty(series)) { + let data = values; + if (!isNil(values) && !isEmpty(series)) { + data = []; // grouped or stacked if (type === BAR_CHART_TYPES.GROUPED || type === BAR_CHART_TYPES.STACKED) { let uniqueDatasetNames; diff --git a/src/components/BarChartCard/barChartUtils.test.js b/src/components/BarChartCard/barChartUtils.test.js index db9bd0d0f0..519e348014 100644 --- a/src/components/BarChartCard/barChartUtils.test.js +++ b/src/components/BarChartCard/barChartUtils.test.js @@ -185,6 +185,9 @@ describe('barChartUtils', () => { value: 270, }, ]); + expect( + formatChartData(series, null, 'city', null, BAR_CHART_TYPES.GROUPED) + ).toBeNull(); }); it('formatChartData returns formatted data for time-based and group-based chart', () => { @@ -257,6 +260,11 @@ describe('barChartUtils', () => { value: 200, }, ]); + + // Handle nulls + expect( + formatChartData(series, null, null, 'timestamp', BAR_CHART_TYPES.STACKED) + ).toBeNull(); }); it('formatChartData returns formatted data for simple, non-time and non-group chart', () => { @@ -292,6 +300,9 @@ describe('barChartUtils', () => { value: 388, }, ]); + expect( + formatChartData(series, null, 'city', null, BAR_CHART_TYPES.SIMPLE) + ).toBeNull(); }); it('formatChartData returns formatted data for time-based, non-group chart', () => { @@ -332,6 +343,9 @@ describe('barChartUtils', () => { value: 565, }, ]); + expect( + formatChartData(series, null, null, 'timestamp', BAR_CHART_TYPES.SIMPLE) + ).toBeNull(); }); it('formatChartData doesnt return null values', () => {