Part of a series of examples. See:
-
-
Save mojowen/4614623 to your computer and use it in GitHub Desktop.
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"> | |
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script> | |
<style> | |
.background { | |
fill: none; | |
pointer-events: all; | |
} | |
#states { | |
fill: #aaa; | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
#states .active { | |
fill: steelblue; | |
} | |
</style> | |
<body> | |
<script> | |
var width = 960, | |
height = 500, | |
centered; | |
var projection = d3.geo.albersUsa() | |
.scale(width) | |
.translate([0, 0]); | |
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", click); | |
var g = svg.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")") | |
.append("g") | |
.attr("id", "states"); | |
d3.json("readme.json", function(json) { | |
g.selectAll("path") | |
.data(json.features) | |
.enter().append("path") | |
.attr("d", path) | |
.on("click", click); | |
}); | |
function click(d) { | |
var x = 0, | |
y = 0, | |
k = 1; | |
if (d && centered !== d) { | |
var centroid = path.centroid(d); | |
x = -centroid[0]; | |
y = -centroid[1]; | |
k = 4; | |
centered = d; | |
} else { | |
centered = null; | |
} | |
g.selectAll("path") | |
.classed("active", centered && function(d) { return d === centered; }); | |
g.transition() | |
.duration(1000) | |
.attr("transform", "scale(" + k + ")translate(" + x + "," + y + ")") | |
.style("stroke-width", 1.5 / k + "px"); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment