Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbostock/5416440 to your computer and use it in GitHub Desktop.
Save mbostock/5416440 to your computer and use it in GitHub Desktop.
Merging States II
license: gpl-3.0

A variation of merging polygons, this time using careful rendering rather than topological manipulation.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.state {
fill: #ccc;
}
.state-boundary {
fill: none;
stroke: #fff;
}
.state.selected {
fill: orange;
}
.state.selected-boundary {
fill: none;
stroke: #000;
stroke-width: 2px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.albersUsa()
.scale(1000)
.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);
var selected = {
"9": 1, "10": 1, "12": 1, "13": 1, "19": 1, "17": 1, "18": 1, "21": 1,
"25": 1, "24": 1, "23": 1, "26": 1, "29": 1, "37": 1, "31": 1, "33": 1,
"34": 1, "36": 1, "39": 1, "42": 1, "44": 1, "45": 1, "47": 1, "51": 1,
"50": 1, "55": 1, "54": 1
};
d3.json("/mbostock/raw/4090846/us.json", function(error, us) {
if (error) throw error;
var states = topojson.feature(us, us.objects.states),
selection = {type: "FeatureCollection", features: states.features.filter(function(d) { return d.id in selected; })};
svg.append("path")
.datum(states)
.attr("class", "state")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);
svg.append("path")
.datum(selection)
.attr("class", "state selected-boundary")
.attr("d", path);
svg.append("path")
.datum(selection)
.attr("class", "state selected")
.attr("d", path);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment