Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active April 20, 2019 01:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mbostock/8814734 to your computer and use it in GitHub Desktop.
Save mbostock/8814734 to your computer and use it in GitHub Desktop.
Neighboring Congressional Districts
license: gpl-3.0

This example uses topojson.neighbors to compute neighboring congressional districts (from the 113th congress) from geographic boundaries by detecting shared edges (arcs). The red district is the hovered district, and the orange district is the neighboring districts.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke-linejoin: round;
stroke-linecap: round;
}
.land {
fill: #bbb;
}
.land :hover {
fill: red;
}
.land .neighbor {
fill: orange;
}
.border {
pointer-events: none;
fill: none;
stroke: #fff;
}
.border--district {
stroke-width: .5px;
}
.border--state {
stroke-width: 1.5px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/queue.v1.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 600;
var projection = d3.geo.albersUsa()
.scale(1280)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
queue()
.defer(d3.json, "/mbostock/raw/4090846/us.json")
.defer(d3.json, "/mbostock/raw/4090846/us-congress-113.json")
.await(ready);
function ready(error, us, congress) {
if (error) throw error;
var districts = congress.objects.districts,
neighbors = topojson.neighbors(districts.geometries);
svg.append("defs").append("path")
.attr("id", "land")
.datum(topojson.feature(us, us.objects.land))
.attr("d", path);
svg.append("clipPath")
.attr("id", "clip-land")
.append("use")
.attr("xlink:href", "#land");
var district = svg.append("g")
.attr("class", "land")
.attr("clip-path", "url(#clip-land)")
.selectAll("path")
.data(topojson.feature(congress, districts).features)
.enter().append("path")
.attr("d", path);
district.append("title")
.text(function(d) { return d.id; });
district
.each(function(d, i) { d.neighbors = d3.selectAll(neighbors[i].map(function(j) { return district[0][j]; })); })
.on("mouseover", function(d) { d.neighbors.classed("neighbor", true); })
.on("mouseout", function(d) { d.neighbors.classed("neighbor", false); });
svg.append("path")
.attr("class", "border border--district")
.datum(topojson.mesh(congress, congress.objects.districts, function(a, b) { return a !== b && (a.id / 1000 | 0) === (b.id / 1000 | 0); }))
.attr("d", path);
svg.append("path")
.attr("class", "border border--state")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("d", path);
}
d3.select(self.frameElement).style("height", height + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment