This example uses topojson.mesh to highlight California’s borders with its three neighboring states.
Created
January 26, 2017 21:24
-
-
Save mbostock/8ba407a7a53d80be62a8d54774663c2f to your computer and use it in GitHub Desktop.
Border Mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
height: 600 | |
border: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<svg width="960" height="600" fill="none" stroke-linejoin="round" stroke-linecap="round"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/topojson.v2.min.js"></script> | |
<script> | |
var svg = d3.select("svg"); | |
var path = d3.geoPath(); | |
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) { | |
if (error) throw error; | |
svg.append("g") | |
.attr("fill", "#000") | |
.selectAll("path") | |
.data(topojson.feature(us, us.objects.states).features) | |
.enter().append("path") | |
.attr("d", path) | |
.append("title") | |
.text(function(d) { return d.id; }); | |
svg.append("path") | |
.attr("stroke", "#fff") | |
.attr("stroke-width", 0.5) | |
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))); | |
// California-Nevada border | |
svg.append("path") | |
.attr("stroke", "cyan") | |
.attr("stroke-width", 2.5) | |
.attr("d", path(topojson.mesh(us, us.objects.states, border("06", "32")))); | |
// California-Arizona border | |
svg.append("path") | |
.attr("stroke", "magenta") | |
.attr("stroke-width", 2.5) | |
.attr("d", path(topojson.mesh(us, us.objects.states, border("06", "04")))); | |
// California-Oregon border | |
svg.append("path") | |
.attr("stroke", "yellow") | |
.attr("stroke-width", 2.5) | |
.attr("d", path(topojson.mesh(us, us.objects.states, border("06", "41")))); | |
}); | |
function border(id0, id1) { | |
return function(a, b) { | |
return a.id === id0 && b.id === id1 | |
|| a.id === id1 && b.id === id0; | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment