Skip to content

Instantly share code, notes, and snippets.

@pierrelorioux
Last active December 14, 2015 23:59
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 pierrelorioux/5170072 to your computer and use it in GitHub Desktop.
Save pierrelorioux/5170072 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.tract {
fill: #eee;
}
.tract-border {
fill: none;
stroke: #333;
}
.state-border {
fill: none;
stroke: #333;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 1200;
var projection = d3.geo.mercator()
.rotate([74 + 30 / 60, -38 - 50 / 60, 90]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "rotate(90 " + width / 2 + "," + height / 2 + ")");
d3.json("nj-tracts.json", function(error, nj) {
var tracts = topojson.object(nj, nj.objects.tracts);
projection
.scale(1)
.translate([0, 0]);
var b = path.bounds(tracts),
s = .95 / Math.max((b[1][0] - b[0][0]) / height, (b[1][1] - b[0][1]) / width),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
projection
.scale(s)
.translate(t);
svg.append("path")
.datum({type: "GeometryCollection", geometries: tracts.geometries.filter(function(d) { return (d.id / 10000 | 0) % 100 !== 99; })})
.attr("class", "tract")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(nj, nj.objects.tracts, function(a, b) { return a !== b; }))
.attr("class", "tract-border")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(nj, nj.objects.tracts, function(a, b) { return a === b; }))
.attr("class", "state-border")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment