@@ -11,8 +11,9 @@ export function legendRamp(color, {
11
11
marginBottom = 16 + tickSize ,
12
12
marginLeft = 0 ,
13
13
style,
14
- ticks = width / 64 ,
14
+ ticks = ( width - marginLeft - marginRight ) / 64 ,
15
15
tickFormat,
16
+ round = true ,
16
17
className
17
18
} ) {
18
19
className = maybeClassName ( className ) ;
@@ -41,8 +42,15 @@ export function legendRamp(color, {
41
42
. call ( applyInlineStyles , style ) ;
42
43
43
44
let tickAdjust = g => g . selectAll ( ".tick line" ) . attr ( "y1" , marginTop + marginBottom - height ) ;
45
+
44
46
let x ;
45
47
48
+ // Some D3 scales use scale.interpolate, some scale.interpolator, and some
49
+ // scale.round; this normalizes the API so it works with all scale types.
50
+ const applyRange = round
51
+ ? ( x , range ) => x . rangeRound ( range )
52
+ : ( x , range ) => x . range ( range ) ;
53
+
46
54
const { type, domain, range, interpolate, scale, pivot} = color ;
47
55
48
56
// Continuous
@@ -60,7 +68,8 @@ export function legendRamp(color, {
60
68
// domain.length is two, and so the range is simply the extent.) For a
61
69
// diverging scale, we need an extra point in the range for the pivot such
62
70
// that the pivot is always drawn in the middle.
63
- x = scale . copy ( ) . rangeRound (
71
+ x = applyRange (
72
+ scale . copy ( ) ,
64
73
quantize (
65
74
interpolateNumber ( marginLeft , width - marginRight ) ,
66
75
Math . min (
@@ -90,9 +99,7 @@ export function legendRamp(color, {
90
99
91
100
// Construct a linear scale with evenly-spaced ticks for each of the
92
101
// thresholds; the domain extends one beyond the threshold extent.
93
- x = scaleLinear ( )
94
- . domain ( [ - 1 , range . length - 1 ] )
95
- . rangeRound ( [ marginLeft , width - marginRight ] ) ;
102
+ x = applyRange ( scaleLinear ( ) . domain ( [ - 1 , range . length - 1 ] ) , [ marginLeft , width - marginRight ] ) ;
96
103
97
104
svg . append ( "g" )
98
105
. selectAll ( "rect" )
@@ -110,9 +117,7 @@ export function legendRamp(color, {
110
117
111
118
// Ordinal (hopefully!)
112
119
else {
113
- x = scaleBand ( )
114
- . domain ( domain )
115
- . rangeRound ( [ marginLeft , width - marginRight ] ) ;
120
+ x = applyRange ( scaleBand ( ) . domain ( domain ) , [ marginLeft , width - marginRight ] ) ;
116
121
117
122
svg . append ( "g" )
118
123
. selectAll ( "rect" )
0 commit comments