Demonstrates using d3.behavior.zoom
with a geographic projection. Based on an
example by Iain Dillingham.
-
-
Save kouphax/3852082 to your computer and use it in GitHub Desktop.
Zoom/Pan Map Example
This file contains hidden or 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> | |
<style> | |
path { | |
fill: #E5F5F9; | |
stroke: #2CA25F; | |
stroke-width: 1; | |
} | |
#axes { | |
stroke: #BDBDBD; | |
stroke-width: 0.5; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script> | |
<script> | |
var width = 960, | |
height = 500, | |
projection = d3.geo.mercator(), | |
path = d3.geo.path().projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.call(d3.behavior.zoom() | |
.translate(projection.translate()) | |
.scale(projection.scale()) | |
.on("zoom", redraw)); | |
var axes = svg.append("g").attr("id", "axes"), | |
xAxis = axes.append("line").attr("y2", height), | |
yAxis = axes.append("line").attr("x2", width); | |
d3.json("uk.json", function (json) { | |
svg.selectAll("path") | |
.data(json.features) | |
.enter().append("path"); | |
redraw(); | |
}); | |
function redraw() { | |
if (d3.event) { | |
projection | |
.translate(d3.event.translate) | |
.scale(d3.event.scale); | |
} | |
svg.selectAll("path").attr("d", path); | |
var t = projection.translate(); | |
xAxis.attr("x1", t[0]).attr("x2", t[0]); | |
yAxis.attr("y1", t[1]).attr("y2", t[1]); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment