Skip to content

Commit

Permalink
Easier programmatic zooming.
Browse files Browse the repository at this point in the history
Setting the translate and scale now propagates the changes to the scale domains
in the same manner as user interaction. Likewise, setting new scales resets the
translate and scale back to their default values ([0, 0] and 1 respectively).
  • Loading branch information
mbostock committed Oct 15, 2012
1 parent 06dae9c commit f1c87d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 10 additions & 1 deletion d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5245,13 +5245,16 @@
translate[0] += p[0] - l[0];
translate[1] += p[1] - l[1];
}
function dispatch(event) {
function rescale() {
if (x1) x1.domain(x0.range().map(function(x) {
return (x - translate[0]) / scale;
}).map(x0.invert));
if (y1) y1.domain(y0.range().map(function(y) {
return (y - translate[1]) / scale;
}).map(y0.invert));
}
function dispatch(event) {
rescale();
d3.event.preventDefault();
event({
type: "zoom",
Expand Down Expand Up @@ -5327,11 +5330,13 @@
zoom.translate = function(x) {
if (!arguments.length) return translate;
translate = x.map(Number);
rescale();
return zoom;
};
zoom.scale = function(x) {
if (!arguments.length) return scale;
scale = +x;
rescale();
return zoom;
};
zoom.scaleExtent = function(x) {
Expand All @@ -5343,12 +5348,16 @@
if (!arguments.length) return x1;
x1 = z;
x0 = z.copy();
translate = [ 0, 0 ];
scale = 1;
return zoom;
};
zoom.y = function(z) {
if (!arguments.length) return y1;
y1 = z;
y0 = z.copy();
translate = [ 0, 0 ];
scale = 1;
return zoom;
};
return d3.rebind(zoom, event, "on");
Expand Down
Loading

0 comments on commit f1c87d7

Please sign in to comment.