@@ -94,7 +94,7 @@ function stack(x, y = one, kx, ky, {offset, order, reverse}, options) {
94
94
const facetstacks = [ ] ;
95
95
for ( const facet of facets ) {
96
96
const stacks = X ? Array . from ( group ( facet , ( i ) => X [ i ] ) . values ( ) ) : [ facet ] ;
97
- if ( compare ) applyOrder ( stacks , compare ) ;
97
+ if ( compare ) for ( const stack of stacks ) stack . sort ( compare ) ;
98
98
for ( const stack of stacks ) {
99
99
let yn = 0 ;
100
100
let yp = 0 ;
@@ -228,23 +228,23 @@ function offsetCenterFacets(facetstacks, Y1, Y2) {
228
228
}
229
229
230
230
function maybeOrder ( order , offset , ky ) {
231
- if ( order === undefined && offset === offsetWiggle ) return orderInsideOut ( orderAscending ) ;
231
+ if ( order === undefined && offset === offsetWiggle ) return orderInsideOut ( ascendingDefined ) ;
232
232
if ( order == null ) return ;
233
233
if ( typeof order === "string" ) {
234
234
const negate = order . startsWith ( "-" ) ;
235
- const direction = negate ? orderDescending : orderAscending ;
235
+ const compare = negate ? descendingDefined : ascendingDefined ;
236
236
switch ( ( negate ? order . slice ( 1 ) : order ) . toLowerCase ( ) ) {
237
237
case "value" :
238
238
case ky :
239
- return orderY ( direction ) ;
239
+ return orderY ( compare ) ;
240
240
case "z" :
241
- return orderZ ( direction ) ;
241
+ return orderZ ( compare ) ;
242
242
case "sum" :
243
- return orderSum ( direction ) ;
243
+ return orderSum ( compare ) ;
244
244
case "appearance" :
245
- return orderAppearance ( direction ) ;
245
+ return orderAppearance ( compare ) ;
246
246
case "inside-out" :
247
- return orderInsideOut ( direction ) ;
247
+ return orderInsideOut ( compare ) ;
248
248
}
249
249
return orderAccessor ( field ( order ) ) ;
250
250
}
@@ -254,18 +254,18 @@ function maybeOrder(order, offset, ky) {
254
254
}
255
255
256
256
// by value
257
- function orderY ( order ) {
258
- return ( data , X , Y ) => order ( Y ) ;
257
+ function orderY ( compare ) {
258
+ return ( data , X , Y ) => ( i , j ) => compare ( Y [ i ] , Y [ j ] ) ;
259
259
}
260
260
261
261
// by location
262
- function orderZ ( order ) {
263
- return ( data , X , Y , Z ) => order ( Z ) ;
262
+ function orderZ ( compare ) {
263
+ return ( data , X , Y , Z ) => ( i , j ) => compare ( Z [ i ] , Z [ j ] ) ;
264
264
}
265
265
266
266
// by sum of value (a.k.a. “ascending”)
267
- function orderSum ( order ) {
268
- return orderZDomain ( order , ( data , X , Y , Z ) =>
267
+ function orderSum ( compare ) {
268
+ return orderZDomain ( compare , ( data , X , Y , Z ) =>
269
269
groupSort (
270
270
range ( data ) ,
271
271
( I ) => sum ( I , ( i ) => Y [ i ] ) ,
@@ -275,8 +275,8 @@ function orderSum(order) {
275
275
}
276
276
277
277
// by x = argmax of value
278
- function orderAppearance ( order ) {
279
- return orderZDomain ( order , ( data , X , Y , Z ) =>
278
+ function orderAppearance ( compare ) {
279
+ return orderZDomain ( compare , ( data , X , Y , Z ) =>
280
280
groupSort (
281
281
range ( data ) ,
282
282
( I ) => X [ greatest ( I , ( i ) => Y [ i ] ) ] ,
@@ -287,8 +287,8 @@ function orderAppearance(order) {
287
287
288
288
// by x = argmax of value, but rearranged inside-out by alternating series
289
289
// according to the sign of a running divergence of sums
290
- function orderInsideOut ( order ) {
291
- return orderZDomain ( order , ( data , X , Y , Z ) => {
290
+ function orderInsideOut ( compare ) {
291
+ return orderZDomain ( compare , ( data , X , Y , Z ) => {
292
292
const I = range ( data ) ;
293
293
const K = groupSort (
294
294
I ,
@@ -316,39 +316,28 @@ function orderInsideOut(order) {
316
316
} ) ;
317
317
}
318
318
319
- function orderAscending ( O ) {
320
- return ( i , j ) => ascendingDefined ( O [ i ] , O [ j ] ) ;
321
- }
322
-
323
- function orderDescending ( O ) {
324
- return ( i , j ) => descendingDefined ( O [ i ] , O [ j ] ) ;
325
- }
326
-
327
319
function orderAccessor ( f ) {
328
- return ( data ) => orderAscending ( valueof ( data , f ) ) ;
320
+ return ( data ) => {
321
+ const O = valueof ( data , f ) ;
322
+ return ( i , j ) => ascendingDefined ( O [ i ] , O [ j ] ) ;
323
+ } ;
329
324
}
330
325
331
326
function orderComparator ( f ) {
332
327
return ( data ) => ( i , j ) => f ( data [ i ] , data [ j ] ) ;
333
328
}
334
329
335
330
function orderGiven ( domain ) {
336
- return orderZDomain ( orderAscending , ( ) => domain ) ;
331
+ return orderZDomain ( ascendingDefined , ( ) => domain ) ;
337
332
}
338
333
339
- // Given an explicit ordering of distinct values in z, returns a parallel column
340
- // O that can be used with applyOrder to sort stacks. Note that this is a series
341
- // order: it will be consistent across stacks.
342
- function orderZDomain ( order , domain ) {
334
+ // Given an ordering (domain) of distinct values in z that can be derived from
335
+ // the data, returns a comparator that can be used to sort stacks. Note that
336
+ // this is a series order: it will be consistent across stacks.
337
+ function orderZDomain ( compare , domain ) {
343
338
return ( data , X , Y , Z ) => {
344
339
if ( ! Z ) throw new Error ( "missing channel: z" ) ;
345
340
const map = new InternMap ( domain ( data , X , Y , Z ) . map ( ( d , i ) => [ d , i ] ) ) ;
346
- return order ( Z . map ( ( z ) => map . get ( z ) ) ) ;
341
+ return ( i , j ) => compare ( map . get ( Z [ i ] ) , map . get ( Z [ j ] ) ) ;
347
342
} ;
348
343
}
349
-
350
- function applyOrder ( stacks , compare ) {
351
- for ( const stack of stacks ) {
352
- stack . sort ( compare ) ;
353
- }
354
- }
0 commit comments