From Mike's bl.ock on topojson.merge: now merge multiple polygons into a single polygon with a hole.
-
-
Save espinielli/8dbe026854e9faa0265b to your computer and use it in GitHub Desktop.
Polygons merge with a hole.
This file contains hidden or 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> | |
<meta charset="utf-8"> | |
<style> | |
.state { | |
fill: #ccc; | |
} | |
.state-boundary { | |
fill: none; | |
stroke: #fff; | |
} | |
.state.selected { | |
fill: orange; | |
stroke: #000; | |
} | |
</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 = 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); | |
var selected = d3.set([ | |
9, 10, 12, 13, 17, 18, 19, 21, 23, 24, | |
25, 26, 29, 31, 33, 34, 36, 37, 39, 42, | |
44, 45, 47, 50, 51, 55 | |
]); | |
d3.json("/mbostock/raw/4090846/us.json", function(error, us) { | |
svg.append("path") | |
.datum(topojson.feature(us, us.objects.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(topojson.merge(us, us.objects.states.geometries.filter(function(d) { return selected.has(d.id); }))) | |
.attr("class", "state selected") | |
.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