Created
August 12, 2012 13:24
-
-
Save compactcode/3331794 to your computer and use it in GitHub Desktop.
Fitting a d3 geographic map to a given canvas size.
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<style type="text/css"> | |
#container { | |
width: 455px; | |
margin: auto; | |
} | |
#small { | |
width: 150px; | |
height: 150px; | |
} | |
#large { | |
width: 300px; | |
height: 300px; | |
} | |
circle { | |
fill: red; | |
} | |
</style> | |
<script src="http://d3js.org/d3.v2.min.js" type="text/javascript"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<div id="container"> | |
<svg id="small"> | |
</svg> | |
<svg id="large"> | |
</svg> | |
</div> | |
<script type="text/javascript"> | |
d3.json("world.json", function(result) { | |
renderMap("#small", $("#small").width(), result); | |
renderMap("#large", $("#large").width(), result); | |
}); | |
function renderMap(id, size, result) { | |
var projection = d3.geo.mercator(); | |
projection.scale(size); | |
projection.translate([size / 2, size / 2]); | |
var map = d3.select(id); | |
map.selectAll("path") | |
.data(result.features) | |
.enter() | |
.append("path") | |
.attr("d", d3.geo.path().projection(projection)); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment