Skip to content

Instantly share code, notes, and snippets.

@wsvekla
Last active October 3, 2016 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wsvekla/4533258 to your computer and use it in GitHub Desktop.
Save wsvekla/4533258 to your computer and use it in GitHub Desktop.
2010 Census Geography with D3 and TopoJSON

Mapping and ID-ing Philadelphia 2010 Census Tracts with D3 and TopoJSON

<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: #aaa;
stroke: #fff;
stroke-width: .75px;
pointer-events: all;
}
text {
font-family: sans-serif;
font-size: 75px;
color: #aaa;
}
</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 = 500;
var projection = d3.geo.mercator()
.center([-75.12, 40])
.scale(80000);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var text = svg.append("text")
.attr("y", height/8)
.attr("text-anchor", "left");
d3.json("tracts.json", function(error, topology) {
svg.selectAll("path")
.data(topojson.object(topology, topology.objects.tracts).geometries)
.enter().append("path")
.attr("class", "tracts")
.attr("id", function(d){return d.id;})
.attr("d", path)
.on("mouseover", function(){d3.select(this).style("fill", "#ff0");
text.text("tract: " + this.id);})
.on("mouseout", function(){d3.select(this).style("fill", "#aaa");
text.text("");});
});
</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