Created
October 31, 2012 15:21
-
-
Save bsr203/3987629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<title>Zoom + Pan</title> | |
<style> | |
body { | |
position: relative; | |
width: 960px; | |
} | |
svg { | |
font: 10px sans-serif; | |
shape-rendering: crispEdges; | |
} | |
rect { | |
fill: #ddd; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #fff; | |
} | |
button { | |
position: absolute; | |
right: 30px; | |
top: 30px; | |
} | |
</style> | |
<button>Reset</button> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
//Width and height | |
var w = 1000; | |
var h = 1000; | |
var padding = 20; | |
var scale_padding = 20; // space to show axes | |
var dataset = [ | |
[80, 20, 10, 60] ]; // x, y, w, h | |
var x = d3.scale.linear() | |
.domain([0, d3.max(dataset, function (d) { | |
return (100); | |
})]) | |
.range([padding + scale_padding, w - padding]); | |
var y = d3.scale.linear() | |
.domain([0, d3.max(dataset, function (d) { | |
return (100); | |
})]) | |
.range([h - padding - scale_padding, padding]); | |
//Define X axis | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom") | |
.ticks(5); | |
//Define Y axis | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.ticks(5); | |
var zoom = d3.behavior.zoom() | |
.x(x) | |
.y(y) | |
.scaleExtent([1, 10]) | |
.on("zoom", zoomed); | |
var svg = d3.select("body").append("svg") | |
.attr("width", w ) | |
.attr("height", h) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") | |
.call(zoom); | |
svg.selectAll("rect") | |
.data(dataset) | |
.enter() | |
.append("rect") | |
.attr("x", function (d) { | |
var xx = Math.min(x(d[0]), x(d[0] + d[2])); | |
return xx; | |
}) | |
.attr("y", function (d) { | |
var yy = Math.min(y(d[1]), y(d[1] + d[3])); | |
return yy; | |
}) | |
.attr("width", function (d) { | |
var x2 = Math.max(x(d[0]), x(d[0] + d[2])); | |
var x1 = Math.min(x(d[0]), x(d[0] + d[2])); | |
return x2 - x1; | |
}) | |
.attr("height", function (d) { | |
var y2 = Math.max(y(d[1]), y(d[1] + d[3])); | |
var y1 = Math.min(y(d[1]), y(d[1] + d[3])); | |
return y2 - y1; | |
}); | |
//Create X axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (h - padding - scale_padding) + ")") | |
.call(xAxis); | |
//Create Y axis | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + (padding + scale_padding) + ",0)") | |
.call(yAxis); | |
d3.select("button").on("click", reset); | |
function zoomed() { | |
svg.select(".x.axis").call(xAxis); | |
svg.select(".y.axis").call(yAxis); | |
svg.selectAll("rect").call(xAxis); | |
svg.selectAll("rect").call(yAxis); | |
} | |
function reset() { | |
d3.transition().duration(750).tween("zoom", function() { | |
var ix = d3.interpolate(x.domain(), [-w / 2, w / 2]), | |
iy = d3.interpolate(y.domain(), [-h / 2, h / 2]); | |
return function(t) { | |
zoom.x(x.domain(ix(t))).y(y.domain(iy(t))); | |
zoomed(); | |
}; | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment