Answer to a question on stackoverflow. I used the answers to another question to centre and scale the map
Last active
December 15, 2015 22:49
-
-
Save djvanderlaan/5336035 to your computer and use it in GitHub Desktop.
Identity projection of map in d3.js
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> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<div id="vis"> | |
</div> | |
<script> | |
var height = 400; | |
var width = 400; | |
var vis = d3.select("#vis").append("svg") | |
.attr("width", width).attr("height", height) | |
d3.json("po_2012_simplified.json", function(json) { | |
var projection = d3.geo.projection(function(x, y) { return [x, y];}) | |
.precision(0).scale(1).translate([0, 0]); | |
var path = d3.geo.path().projection(projection); | |
var bounds = path.bounds(json), | |
scale = .95 / Math.max((bounds[1][0] - bounds[0][0]) / width, | |
(bounds[1][1] - bounds[0][1]) / height), | |
transl = [(width - scale * (bounds[1][0] + bounds[0][0])) / 2, | |
(height - scale * (bounds[1][1] + bounds[0][1])) / 2]; | |
projection.scale(scale).translate(transl); | |
vis.selectAll("path").data(json.features).enter().append("path") | |
.attr("d", path) | |
.style("fill", "#D0D0D0") | |
.style("stroke-width", "0.5px") | |
.style("stroke", "black") | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment