Skip to content

Commit

Permalink
Fixing exception in bar chart with stack mode when no data is present. (
Browse files Browse the repository at this point in the history
#20312)

* Fixing exception in bar chart with stack mode when no data is present.

This change is fixing an issue which results in an exception being thrown when a bar chart visualization in stack mode has no data.

* Simplifying code by replacing `transform`.

* Suppressing linter hint.

* Adding test case.
  • Loading branch information
dennisoelkers authored Aug 30, 2024
1 parent 5377deb commit 07f821f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ describe('Chart Layout Generators', () => {

expect(result).toEqual(layoutsFor4axis);
});

it('does not throw exception when chart data is `undefined` in stack mode', () => {
const result = generateLayouts({ ...params, chartData: [], barmode: 'stack' });

expect(result).toBeDefined();
});
});

describe('getHoverTemplateSettings', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/

import transform from 'lodash/transform';
import zipWith from 'lodash/zipWith';
import sum from 'lodash/sum';
import flattenDeep from 'lodash/flattenDeep';
Expand Down Expand Up @@ -266,7 +265,8 @@ export const generateMappersForYAxis = (
});
};

const joinValues = (values: Array<Array<number>>, barmode: BarMode): Array<number> => {
// eslint-disable-next-line default-param-last
const joinValues = (values: Array<Array<number>> = [], barmode: BarMode): Array<number> => {
if (barmode === 'stack' || barmode === 'relative') {
return zipWith(...values, (...iterateValues) => sum(iterateValues));
}
Expand All @@ -285,7 +285,7 @@ export type GenerateLayoutsParams = {

export const generateLayouts = (
{ unitTypeMapper, chartData, barmode, widgetUnits, config, theme }: GenerateLayoutsParams,
): Record<string, unknown> => {
) => {
const groupYValuesByUnitTypeKey = chartData.reduce<{} | Record<FieldUnitType | DefaultAxisKey, Array<Array<any>>>>((res, value: ChartDefinition) => {
const traceName = value.fullPath;
const fieldName = getFieldNameFromTrace({ series: config.series, fullPath: traceName });
Expand All @@ -301,10 +301,11 @@ export const generateLayouts = (
return res;
}, {});

return transform(unitTypeMapper, (res, { axisKeyName, axisCount }, unitTypeKey: FieldUnitType | DefaultAxisKey) => {
return Object.fromEntries(Object.entries(unitTypeMapper).map(([unitTypeKey, { axisKeyName, axisCount }]) => {
const unitValues = joinValues(groupYValuesByUnitTypeKey[unitTypeKey], barmode);
res[axisKeyName] = getUnitLayoutWithData(unitTypeKey, axisCount, unitValues, theme);
});

return [axisKeyName, getUnitLayoutWithData(unitTypeKey as FieldUnitType, axisCount, unitValues, theme)];
}));
};

const getHoverTexts = ({ convertedValues, unit }: { convertedValues: Array<any>,
Expand Down

0 comments on commit 07f821f

Please sign in to comment.