Skip to content

Commit a829aa2

Browse files
committed
Add brush.clamp method to specify to clamp extent to range or not
1 parent 1810583 commit a829aa2

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

d3.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7570,7 +7570,7 @@ d3 = function() {
75707570
return subticks;
75717571
}
75727572
d3.svg.brush = function() {
7573-
var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, resizes = d3_svg_brushResizes[0], extent = [ [ 0, 0 ], [ 0, 0 ] ], extentDomain;
7573+
var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, resizes = d3_svg_brushResizes[0], extent = [ [ 0, 0 ], [ 0, 0 ] ], clamp = true, extentDomain;
75747574
function brush(g) {
75757575
g.each(function() {
75767576
var g = d3.select(this), bg = g.selectAll(".background").data([ 0 ]), fg = g.selectAll(".extent").data([ 0 ]), tz = g.selectAll(".resize").data(resizes, String), e;
@@ -7691,7 +7691,16 @@ d3 = function() {
76917691
r0 -= position;
76927692
r1 -= size + position;
76937693
}
7694-
min = Math.max(r0, Math.min(r1, point[i]));
7694+
if (typeof clamp === "boolean") {
7695+
apply_clamp = clamp;
7696+
} else {
7697+
apply_clamp = clamp[i];
7698+
}
7699+
if (apply_clamp || !dragging) {
7700+
min = Math.max(r0, Math.min(r1, point[i]));
7701+
} else {
7702+
min = point[i];
7703+
}
76957704
if (dragging) {
76967705
max = (min += position) + size;
76977706
} else {
@@ -7733,6 +7742,11 @@ d3 = function() {
77337742
resizes = d3_svg_brushResizes[!x << 1 | !y];
77347743
return brush;
77357744
};
7745+
brush.clamp = function(z) {
7746+
if (!arguments.length) return clamp;
7747+
clamp = z;
7748+
return brush;
7749+
};
77367750
brush.extent = function(z) {
77377751
var x0, x1, y0, y1, t;
77387752
if (!arguments.length) {

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/svg/brush.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ d3.svg.brush = function() {
1414
y = null, // y-scale, optional
1515
resizes = d3_svg_brushResizes[0],
1616
extent = [[0, 0], [0, 0]], // [x0, y0], [x1, y1], in pixels (integers)
17+
clamp = true, // whether or not to clamp the extent to the range
1718
extentDomain; // the extent in data space, lazily created
1819

1920
function brush(g) {
@@ -223,8 +224,18 @@ d3.svg.brush = function() {
223224
r1 -= size + position;
224225
}
225226

226-
// Clamp the point so that the extent fits within the range extent.
227-
min = Math.max(r0, Math.min(r1, point[i]));
227+
if (typeof clamp === 'boolean') {
228+
apply_clamp = clamp
229+
} else {
230+
apply_clamp = clamp[i]
231+
}
232+
if (apply_clamp || !dragging) {
233+
// Clamp the point so that the extent fits within the range extent.
234+
min = Math.max(r0, Math.min(r1, point[i]));
235+
} else {
236+
// No clamping
237+
min = point[i];
238+
}
228239

229240
// Compute the new extent bounds.
230241
if (dragging) {
@@ -285,6 +296,12 @@ d3.svg.brush = function() {
285296
return brush;
286297
};
287298

299+
brush.clamp = function(z) {
300+
if (!arguments.length) return clamp;
301+
clamp = z;
302+
return brush;
303+
};
304+
288305
brush.extent = function(z) {
289306
var x0, x1, y0, y1, t;
290307

test/svg/brush-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ suite.addBatch({
2121
}
2222
},
2323

24+
"clamp": {
25+
"defaults to true": function(brush) {
26+
assert.isTrue(brush().clamp());
27+
},
28+
"returns one-dimensional array when clamp is defined for x and y": function(brush) {
29+
var b = brush().clamp([true, false])
30+
assert.deepEqual(b.clamp(), [true, false]);
31+
}
32+
},
33+
2434
"extent": {
2535
"returns null when no scales are attached": function(brush) {
2636
assert.isNull(brush().extent());

0 commit comments

Comments
 (0)