Skip to content

Instantly share code, notes, and snippets.

@pgiraud
Forked from mbostock/.block
Last active August 29, 2015 13:56
Show Gist options
  • Save pgiraud/9001340 to your computer and use it in GitHub Desktop.
Save pgiraud/9001340 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.tract {
fill: #eee;
}
.tract:hover {
fill: orange;
}
.tract-border {
fill: none;
stroke: #777;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 1200;
var projection = d3.geo.transverseMercator();
// .rotate([74 + 30 / 60, -38 - 50 / 60]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("nj-tracts.json", function(error, nj) {
var tracts = topojson.feature(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]) / width, (b[1][1] - b[0][1]) / height),
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.selectAll("path")
.data(tracts.features.filter(function(d) { return (d.id / 10000 | 0) % 100 !== 99; }))
.enter().append("path")
.attr("class", "tract")
.attr("d", path)
.append("title")
.text(function(d) { return d.id; });
svg.append("path")
.datum(topojson.mesh(nj, nj.objects.tracts, function(a, b) { return a !== b; }))
.attr("class", "tract-border")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
</script>
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