Skip to content

Instantly share code, notes, and snippets.

@compactcode
Created August 12, 2012 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compactcode/3331794 to your computer and use it in GitHub Desktop.
Save compactcode/3331794 to your computer and use it in GitHub Desktop.
Fitting a d3 geographic map to a given canvas size.
<!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>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment