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

fix(partition): zero value sectors cause max stack call #2260

Merged
merged 3 commits into from
Nov 29, 2023
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 @@ -29,11 +29,11 @@ function layVector(
areaAccessor: (e: ArrayEntry) => number,
): LayoutElement {
const area = nodes.reduce((p, n) => p + areaAccessor(n), 0);
const dependentSize = area / independentSize; // here we lose a bit of accuracy
const dependentSize = independentSize === 0 ? 0 : area / independentSize; // here we lose a bit of accuracy
let currentOffset = 0;
const sectionOffsets = [currentOffset];
const sectionSizes = nodes.map((e, i) => {
const sectionSize = areaAccessor(e) / dependentSize; // here we gain back a bit of accuracy
const sectionSize = dependentSize === 0 ? 0 : areaAccessor(e) / dependentSize; // here we gain back a bit of accuracy
if (i < nodes.length - 1) sectionOffsets.push((currentOffset += sectionSize));
return sectionSize;
});
Expand Down
28 changes: 28 additions & 0 deletions packages/charts/src/chart_types/partition_chart/partition.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import { Store } from 'redux';

import { computeLegendSelector } from './state/selectors/compute_legend';
import { partitionMultiGeometries } from './state/selectors/geometries';
import { getLegendItemsLabels } from './state/selectors/get_legend_items_labels';
import { MockGlobalSpec, MockSeriesSpec } from '../../mocks/specs';
import { MockStore } from '../../mocks/store';
import { GlobalChartState } from '../../state/chart_state';
import { LegendItemLabel } from '../../state/selectors/get_legend_items_labels';
import { LIGHT_THEME } from '../../utils/themes/light_theme';

// sorting is useful to ensure tests pass even if order changes (where order doesn't matter)
const ascByLabel = (a: LegendItemLabel, b: LegendItemLabel) => (a.label < b.label ? -1 : a.label > b.label ? 1 : 0);
Expand Down Expand Up @@ -160,5 +162,31 @@ describe('Retain hierarchy even with arbitrary names', () => {
);
expect(getLegendItemsLabels(store.getState()).map((l) => l.label)).toEqual([]);
});
it('avoid max stack call with zero value at specific dimensions', () => {
MockStore.updateDimensions(store, { width: 557, height: 360, top: 0, left: 0 });
MockStore.addSpecs(
[
MockGlobalSpec.settings({ showLegend: false, theme: LIGHT_THEME }),
MockSeriesSpec.treemap({
data: [
{ cat: 'a', val: 1 },
{ cat: 'b', val: 1 },
{ cat: 'c', val: 0 },
{ cat: 'd', val: 1 },
],
valueAccessor: (d: { cat: string; val: number }) => d.val,
layers: [
{
groupByRollup: (d: { cat: string; val: number }) => d.cat,
},
],
}),
],
store,
);
expect(() => {
partitionMultiGeometries(store.getState());
}).not.toThrow();
});
});
});