Last active
September 12, 2016 04:20
-
-
Save gregdevs/a73f8a16f129757c037e72ecdebdd8f2 to your computer and use it in GitHub Desktop.
NY State with Counties Clipped
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> | |
.outline { | |
fill: none; | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
.feature { | |
fill: #ccc; | |
} | |
.mesh { | |
fill: none; | |
stroke: #fff; | |
stroke-width: .5px; | |
stroke-linejoin: round; | |
} | |
path { | |
fill: #ccc; | |
stroke: #fff; | |
stroke-width: .5px; | |
} | |
.county:hover{ | |
fill:orange; | |
} | |
</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.albers(); | |
/* | |
var projection = d3.geo.transverseMercator() | |
.rotate([120 + 30 / 60, -38 - 50 / 60]) | |
*/ | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.json("us.json", function(error, us) { | |
if (error) throw error; | |
var states = topojson.feature(us, us.objects.states), | |
state = states.features.filter(function(d) { return d.id === 06; })[0]; | |
projection.scale(1) | |
.translate([0, 0]); | |
var b = path.bounds(state), | |
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; | |
projection.scale(s) | |
.translate(t); | |
svg.append("path") | |
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) | |
.attr("class", "mesh") | |
.attr("d", path); | |
svg.append("path") | |
.datum(state) | |
.attr("class", "outline") | |
.attr("d", path) | |
.attr('id', 'land'); | |
svg.append("clipPath") | |
.attr("id", "clip-land") | |
.append("use") | |
.attr("xlink:href", "#land"); | |
svg.selectAll("path") | |
.data(topojson.feature(us, us.objects.counties).features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr('county-id', function(d){ | |
return d.id | |
}).attr("clip-path", "url(#clip-land)") | |
.attr('class', 'county'); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment