Skip to content

Instantly share code, notes, and snippets.

@gdmf
Created December 1, 2012 04:54
Show Gist options
  • Save gdmf/4180596 to your computer and use it in GitHub Desktop.
Save gdmf/4180596 to your computer and use it in GitHub Desktop.
d3: Mercator
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: #a4bac7;
}
.foreground {
fill: none;
stroke: #333;
stroke-width: 1.5px;
}
.graticule {
fill: none;
stroke: #fff;
stroke-width: .5px;
}
.graticule:nth-child(2n) {
stroke-dasharray: 2,2;
}
.land {
fill: #d7c7ad;
stroke: #766951;
}
.boundary {
fill: none;
stroke: #a5967e;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://raw.github.com/d3/d3-plugins/master/geo/projection/projection.js"></script>
<script>
var width = 400,
height = 600;
var projection = d3.geo.mercator()
.scale(5000)
.translate([-1300, 400]);
var path = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("path")
.datum(graticule.outline)
.attr("class", "background")
.attr("d", path);
svg.selectAll(".graticule")
.data(graticule.lines)
.enter().append("path")
.attr("class", "graticule")
.attr("d", path);
svg.append("path")
.datum(graticule.outline)
.attr("class", "foreground")
.attr("d", path);
d3.json("../4180065/world-boundaries.json", function(error, collection) {
svg.insert("path", ".graticule")
.datum(collection)
.attr("class", "boundary")
.attr("d", path);
});
d3.json("../4179989/world.json", function(error, collection) {
svg.insert("path", ".graticule,.boundary")
.datum(collection)
.attr("class", "land")
.attr("d", path);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment