Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading