@@ -19,7 +19,8 @@ export function channelSort(channels, facetChannels, data, options) {
1919 const { reverse : defaultReverse , reduce : defaultReduce = true , limit : defaultLimit } = options ;
2020 for ( const x in options ) {
2121 if ( ! registry . has ( x ) ) continue ; // ignore unknown scale keys
22- const { value : y , reverse = defaultReverse , reduce = defaultReduce , limit = defaultLimit } = maybeValue ( options [ x ] ) ;
22+ let { value : y , reverse = defaultReverse , reduce = defaultReduce , limit = defaultLimit } = maybeValue ( options [ x ] ) ;
23+ if ( reverse === undefined ) reverse = y === "dx" || y === "dy" ; // default to descending for lengths
2324 if ( reduce == null || reduce === false ) continue ; // disabled reducer
2425 const X = channels . find ( ( [ , { scale} ] ) => scale === x ) || facetChannels && facetChannels . find ( ( [ , { scale} ] ) => scale === x ) ;
2526 if ( ! X ) throw new Error ( `missing channel for scale: ${ x } ` ) ;
@@ -33,14 +34,12 @@ export function channelSort(channels, facetChannels, data, options) {
3334 return domain ;
3435 } ;
3536 } else {
36- let YV ;
37- if ( y === "data" ) {
38- YV = data ;
39- } else {
40- const Y = channels . find ( ( [ name ] ) => name === y ) ;
41- if ( ! Y ) throw new Error ( `missing channel: ${ y } ` ) ;
42- YV = Y [ 1 ] . value ;
43- }
37+ const YV = y === "data" ? data
38+ : y === "dy" ? difference ( channels , "y1" , "y2" )
39+ : y === "dx" ? difference ( channels , "x1" , "x2" )
40+ : y === "y" ? values ( channels , y , "y2" )
41+ : y === "x" ? values ( channels , y , "x2" )
42+ : values ( channels , y ) ;
4443 const reducer = maybeReduce ( reduce === true ? "max" : reduce , YV ) ;
4544 X [ 1 ] . domain = ( ) => {
4645 let domain = rollup ( range ( XV ) , I => reducer . reduce ( I , YV ) , i => XV [ i ] ) ;
@@ -52,6 +51,19 @@ export function channelSort(channels, facetChannels, data, options) {
5251 }
5352}
5453
54+ function difference ( channels , k1 , k2 ) {
55+ const X1 = values ( channels , k1 ) ;
56+ const X2 = values ( channels , k2 ) ;
57+ return Float64Array . from ( X2 , ( x2 , i ) => Math . abs ( x2 - X1 [ i ] ) ) ;
58+ }
59+
60+ function values ( channels , name , alias ) {
61+ let channel = channels . find ( ( [ n ] ) => n === name ) ;
62+ if ( ! channel && alias !== undefined ) channel = channels . find ( ( [ n ] ) => n === alias ) ;
63+ if ( channel ) return channel [ 1 ] . value ;
64+ throw new Error ( `missing channel: ${ name } ` ) ;
65+ }
66+
5567function ascendingGroup ( [ ak , av ] , [ bk , bv ] ) {
5668 return ascending ( av , bv ) || ascending ( ak , bk ) ;
5769}
0 commit comments