Skip to content

Commit e458581

Browse files
committed
round option
1 parent d0c213e commit e458581

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Continuous color legends are rendered as a ramp, and can be configured with the
248248
* *options*.**ticks** - the desired number of ticks, or an array of tick values
249249
* *options*.**tickFormat** - a format function for the legend’s ticks
250250
* *options*.**tickSize** - the tick size
251+
* *options*.**round** - if true (default), round tick positions to pixels
251252
* *options*.**width** - the legend’s width
252253
* *options*.**height** - the legend’s height
253254
* *options*.**marginTop** - the legend’s top margin

src/legends/ramp.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ export function legendRamp(color, {
1111
marginBottom = 16 + tickSize,
1212
marginLeft = 0,
1313
style,
14-
ticks = width / 64,
14+
ticks = (width - marginLeft - marginRight) / 64,
1515
tickFormat,
16+
round = true,
1617
className
1718
}) {
1819
className = maybeClassName(className);
@@ -41,8 +42,15 @@ export function legendRamp(color, {
4142
.call(applyInlineStyles, style);
4243

4344
let tickAdjust = g => g.selectAll(".tick line").attr("y1", marginTop + marginBottom - height);
45+
4446
let x;
4547

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+
4654
const {type, domain, range, interpolate, scale, pivot} = color;
4755

4856
// Continuous
@@ -60,7 +68,8 @@ export function legendRamp(color, {
6068
// domain.length is two, and so the range is simply the extent.) For a
6169
// diverging scale, we need an extra point in the range for the pivot such
6270
// that the pivot is always drawn in the middle.
63-
x = scale.copy().rangeRound(
71+
x = applyRange(
72+
scale.copy(),
6473
quantize(
6574
interpolateNumber(marginLeft, width - marginRight),
6675
Math.min(
@@ -90,9 +99,7 @@ export function legendRamp(color, {
9099

91100
// Construct a linear scale with evenly-spaced ticks for each of the
92101
// 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]);
96103

97104
svg.append("g")
98105
.selectAll("rect")
@@ -110,9 +117,7 @@ export function legendRamp(color, {
110117

111118
// Ordinal (hopefully!)
112119
else {
113-
x = scaleBand()
114-
.domain(domain)
115-
.rangeRound([marginLeft, width - marginRight]);
120+
x = applyRange(scaleBand().domain(domain), [marginLeft, width - marginRight]);
116121

117122
svg.append("g")
118123
.selectAll("rect")

test/output/colorLegendMargins.svg

Lines changed: 2 additions & 11 deletions
Loading

0 commit comments

Comments
 (0)