1- import { InternSet , rollup , sort } from "d3" ;
1+ import { InternSet , rollups } from "d3" ;
22import { ascendingDefined , descendingDefined } from "./defined.js" ;
33import { first , isColor , isEvery , isIterable , isOpacity , labelof , map , maybeValue , range , valueof } from "./options.js" ;
44import { registry } from "./scales/index.js" ;
@@ -78,11 +78,16 @@ export function inferChannelScale(name, channel) {
7878// computed; i.e., if the scale’s domain is set explicitly, that takes priority
7979// over the sort option, and we don’t need to do additional work.
8080export function channelDomain ( data , facets , channels , facetChannels , options ) {
81- const { reverse : defaultReverse , reduce : defaultReduce = true , limit : defaultLimit } = options ;
8281 for ( const x in options ) {
8382 if ( ! registry . has ( x ) ) continue ; // ignore unknown scale keys (including generic options)
84- let { value : y , reverse = defaultReverse , reduce = defaultReduce , limit = defaultLimit } = maybeValue ( options [ x ] ) ;
85- if ( reverse === undefined ) reverse = y === "width" || y === "height" ; // default to descending for lengths
83+ let {
84+ value : y ,
85+ order = options . order ,
86+ reverse = options . reverse ,
87+ reduce = options . reduce === undefined ? true : options . reduce ,
88+ limit = options . limit
89+ } = maybeValue ( options [ x ] ) ;
90+ order = order === undefined ? y === "width" || y === "height" ? descendingGroup : ascendingGroup : maybeOrder ( order ) ; // prettier-ignore
8691 if ( reduce == null || reduce === false ) continue ; // disabled reducer
8792 const X = x === "fx" || x === "fy" ? reindexFacetChannel ( facets , facetChannels [ x ] ) : findScaleChannel ( channels , x ) ;
8893 if ( ! X ) throw new Error ( `missing channel for scale: ${ x } ` ) ;
@@ -106,12 +111,13 @@ export function channelDomain(data, facets, channels, facetChannels, options) {
106111 : values ( channels , y , y === "y" ? "y2" : y === "x" ? "x2" : undefined ) ;
107112 const reducer = maybeReduce ( reduce === true ? "max" : reduce , YV ) ;
108113 X . domain = ( ) => {
109- let domain = rollup (
114+ let domain = rollups (
110115 range ( XV ) ,
111116 ( I ) => reducer . reduceIndex ( I , YV ) ,
112117 ( i ) => XV [ i ]
113118 ) ;
114- domain = sort ( domain , reverse ? descendingGroup : ascendingGroup ) ;
119+ if ( order ) domain . sort ( order ) ;
120+ if ( reverse ) domain . reverse ( ) ;
115121 if ( lo !== 0 || hi !== Infinity ) domain = domain . slice ( lo , hi ) ;
116122 return domain . map ( first ) ;
117123 } ;
@@ -154,6 +160,17 @@ function values(channels, name, alias) {
154160 throw new Error ( `missing channel: ${ name } ` ) ;
155161}
156162
163+ function maybeOrder ( order ) {
164+ if ( typeof order === "function" ) return order ;
165+ switch ( `${ order } ` . toLowerCase ( ) ) {
166+ case "ascending" :
167+ return ascendingGroup ;
168+ case "descending" :
169+ return descendingGroup ;
170+ }
171+ throw new Error ( `invalid order: ${ order } ` ) ;
172+ }
173+
157174function ascendingGroup ( [ ak , av ] , [ bk , bv ] ) {
158175 return ascendingDefined ( av , bv ) || ascendingDefined ( ak , bk ) ;
159176}
0 commit comments