@@ -68,11 +68,25 @@ function autoScaleRangeY(scale, dimensions) {
68
68
}
69
69
70
70
function autoScaleRound ( scale ) {
71
- if ( scale . round === undefined && isOrdinalScale ( scale ) && scale . scale . step ( ) >= 5 ) {
71
+ if ( scale . round === undefined && isBandScale ( scale ) && roundError ( scale ) <= 30 ) {
72
72
scale . scale . round ( true ) ;
73
73
}
74
74
}
75
75
76
+ // If we were to turn on rounding for this band or point scale, how much wasted
77
+ // space would it introduce (on both ends of the range)? This must match
78
+ // d3.scaleBand’s rounding behavior:
79
+ // https://github.com/d3/d3-scale/blob/83555bd759c7314420bd4240642beda5e258db9e/src/band.js#L20-L32
80
+ function roundError ( { scale} ) {
81
+ const n = scale . domain ( ) . length ;
82
+ const [ start , stop ] = scale . range ( ) ;
83
+ const paddingInner = scale . paddingInner ? scale . paddingInner ( ) : 1 ;
84
+ const paddingOuter = scale . paddingOuter ? scale . paddingOuter ( ) : scale . padding ( ) ;
85
+ const m = n - paddingInner ;
86
+ const step = Math . abs ( stop - start ) / Math . max ( 1 , m + paddingOuter * 2 ) ;
87
+ return ( step - Math . floor ( step ) ) * m ;
88
+ }
89
+
76
90
function piecewiseRange ( { scale, range} ) {
77
91
const length = scale . domain ( ) . length ;
78
92
if ( ! ( length > 2 ) ) return range ;
@@ -179,6 +193,10 @@ export function isOrdinalScale({type}) {
179
193
return type === "ordinal" || type === "point" || type === "band" ;
180
194
}
181
195
196
+ function isBandScale ( { type} ) {
197
+ return type === "point" || type === "band" ;
198
+ }
199
+
182
200
export function isDivergingScale ( { type} ) {
183
201
return / ^ d i v e r g i n g ( $ | - ) / . test ( type ) ;
184
202
}
0 commit comments