Built with blockbuilder.org
Last active
August 23, 2017 12:54
-
-
Save ozanyusufoglu/2a0e95dd799ae5ec3243b35b915e79e9 to your computer and use it in GitHub Desktop.
fresh block
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: mit |
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> | |
<meta charset="utf-8"> | |
<title>D3.js US Map with Zoom to State and Click on County</title> | |
<style> | |
.background { | |
fill: none; | |
pointer-events: all; | |
} | |
#states { | |
fill: #aaa; | |
} | |
#states .active { | |
display:none; | |
} | |
#state-borders { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 1.5px; | |
stroke-linejoin: round; | |
stroke-linecap: round; | |
pointer-events: none; | |
} | |
.county-boundary { | |
fill: #aaa; | |
stroke: #fff; | |
stroke-width: .5px; | |
} | |
.county-boundary:hover, .state:hover { | |
fill: orange; | |
} | |
</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 = 500, | |
centered; | |
var projection = d3.geo.albersUsa() | |
.scale(1070) | |
.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); | |
svg.append("rect") | |
.attr("class", "background") | |
.attr("width", width) | |
.attr("height", height) | |
.on("click", clicked); | |
var g = svg.append("g"); | |
d3.json("us.json", function(error, us) { | |
g.append("g") | |
.attr("id", "counties") | |
.selectAll("path") | |
.data(topojson.feature(us, us.objects.counties).features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr("class", "county-boundary") | |
.on("click", countyclicked); | |
g.append("g") | |
.attr("id", "states") | |
.selectAll("path") | |
.data(topojson.feature(us, us.objects.states).features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr("class", "state") | |
.on("click", clicked); | |
g.append("path") | |
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) | |
.attr("id", "state-borders") | |
.attr("d", path); | |
}); | |
function clicked(d) { | |
var x, y, k; | |
if (d && centered !== d) { | |
var centroid = path.centroid(d); | |
x = centroid[0]; | |
y = centroid[1]; | |
k = 4; | |
centered = d; | |
} else { | |
x = width / 2; | |
y = height / 2; | |
k = 1; | |
centered = null; | |
} | |
g.selectAll("path") | |
.classed("active", centered && function(d) { return d === centered; }); | |
g.transition() | |
.duration(750) | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")") | |
.style("stroke-width", 1.5 / k + "px"); | |
} | |
function countyclicked(d) { | |
alert(d.id); | |
} | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment