@@ -56,9 +56,12 @@ interface ISubBinOptions extends IBinOptions {
5656const 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