Skip to content

Commit 9ad324c

Browse files
committed
fix: fix error of empty groupFields when use bin
1 parent 40d0afc commit 9ad324c

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

packages/vdataset/__tests__/bin.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,48 @@ describe('bin transform', () => {
452452
});
453453
}
454454
});
455+
456+
test('should not throw error when groupField is empty array', () => {
457+
const data = [
458+
{
459+
v: 1
460+
},
461+
{
462+
v: 1
463+
},
464+
{
465+
v: 1
466+
},
467+
{
468+
v: 2
469+
},
470+
{
471+
v: 5
472+
},
473+
{
474+
v: 7
475+
},
476+
{
477+
v: 8
478+
},
479+
{
480+
v: 9
481+
},
482+
{
483+
v: 10
484+
}
485+
];
486+
const out: any = bin(data, {
487+
field: 'v',
488+
groupField: [],
489+
outputNames: {
490+
x0: '__BinStart__',
491+
x1: '__BinEnd__',
492+
count: '__BinCount__',
493+
percentage: '__BinPercentage__'
494+
}
495+
});
496+
497+
expect(out.length).toBe(10);
498+
});
455499
});

packages/vdataset/src/transform/bin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ interface ISubBinOptions extends IBinOptions {
5656
const subBin: Transform = (data: Array<object>, options: ISubBinOptions) => {
5757
const { numBins, thresholds, countName, percentageName, valuesName, countField, field, n, x0Name, x1Name } = options;
5858

59+
const groupField = options.groupField;
60+
const usingGroup = Array.isArray(groupField) ? groupField.length > 0 : !!groupField;
61+
5962
// we'll build outputs later; if no grouping, pre-create per-bin outputs
6063
const out: any[] = [];
61-
if (!options.groupField) {
64+
if (!usingGroup) {
6265
for (let i = 0; i < numBins; i++) {
6366
const rec: any = { [x0Name]: thresholds[i], [x1Name]: thresholds[i + 1], [countName]: 0 };
6467
if (options.includeValues) {
@@ -67,8 +70,6 @@ const subBin: Transform = (data: Array<object>, options: ISubBinOptions) => {
6770
out.push(rec);
6871
}
6972
}
70-
const groupField = options.groupField;
71-
const usingGroup = Array.isArray(groupField) ? groupField.length > 0 : !!groupField;
7273

7374
// when grouping, keep per-bin maps from groupKey -> aggregated weight, values and representative group object
7475
const binGroupCounts: Array<Map<string, number>> = usingGroup ? new Array(numBins).fill(0).map(() => new Map()) : [];
@@ -277,6 +278,7 @@ export const bin: Transform = (data: Array<object>, options?: IBinOptions) => {
277278
: options?.groupField
278279
? [options.groupField]
279280
: [];
281+
const normalizedGroupField = groupField.length ? groupField : undefined;
280282
const subViewOptions = {
281283
...options,
282284
numBins,
@@ -288,7 +290,8 @@ export const bin: Transform = (data: Array<object>, options?: IBinOptions) => {
288290
field,
289291
n,
290292
x0Name,
291-
x1Name
293+
x1Name,
294+
groupField: normalizedGroupField
292295
};
293296
if (!facetField.length) {
294297
return subBin(data, subViewOptions);
@@ -304,9 +307,10 @@ export const bin: Transform = (data: Array<object>, options?: IBinOptions) => {
304307
});
305308
return Object.values(subViewMap)
306309
.map(subDataset => {
310+
const combinedGroupField = [...groupField, ...facetField];
307311
return subBin(subDataset, {
308312
...subViewOptions,
309-
groupField: [...groupField, ...facetField],
313+
groupField: combinedGroupField.length ? combinedGroupField : undefined,
310314
n: subDataset.length
311315
});
312316
})

0 commit comments

Comments
 (0)