Skip to content

Instantly share code, notes, and snippets.

@mojowen
Forked from mbostock/.block
Last active December 13, 2015 23:18
Show Gist options
  • Save mojowen/4990265 to your computer and use it in GitHub Desktop.
Save mojowen/4990265 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!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("mouseover", click);
});
function click(d) {
var x = 0,
y = 0,
k = 1;
alert(d.properties.unemployment)
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