|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.background { |
|
fill: none; |
|
pointer-events: all; |
|
} |
|
|
|
.feature { |
|
fill: #ccc; |
|
cursor: pointer; |
|
} |
|
|
|
.feature.active { |
|
fill: orange; |
|
} |
|
|
|
.mesh { |
|
fill: none; |
|
stroke: #fff; |
|
stroke-linecap: round; |
|
stroke-linejoin: round; |
|
} |
|
|
|
</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, |
|
active = d3.select(null); |
|
|
|
var projection = d3.geo.albersUsa() |
|
.scale(1000) |
|
.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", reset); |
|
|
|
var g = svg.append("g") |
|
.style("stroke-width", "1.5px"); |
|
|
|
d3.json("/mbostock/raw/4090846/us.json", function(error, us) { |
|
if (error) throw error; |
|
|
|
g.selectAll("path") |
|
.data(topojson.feature(us, us.objects.states).features) |
|
.enter().append("path") |
|
.attr("d", path) |
|
.attr("class", "feature") |
|
.on("click", clicked); |
|
|
|
g.append("path") |
|
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) |
|
.attr("class", "mesh") |
|
.attr("d", path); |
|
}); |
|
|
|
function clicked(d) { |
|
if (active.node() === this) return reset(); |
|
active.classed("active", false); |
|
active = d3.select(this).classed("active", true); |
|
|
|
var bounds = path.bounds(d), |
|
dx = bounds[1][0] - bounds[0][0], |
|
dy = bounds[1][1] - bounds[0][1], |
|
x = (bounds[0][0] + bounds[1][0]) / 2, |
|
y = (bounds[0][1] + bounds[1][1]) / 2, |
|
scale = .9 / Math.max(dx / width, dy / height), |
|
translate = [width / 2 - scale * x, height / 2 - scale * y]; |
|
|
|
g.transition() |
|
.duration(750) |
|
.style("stroke-width", 1.5 / scale + "px") |
|
.attr("transform", "translate(" + translate + ")scale(" + scale + ")"); |
|
} |
|
|
|
function reset() { |
|
active.classed("active", false); |
|
active = d3.select(null); |
|
|
|
g.transition() |
|
.duration(750) |
|
.style("stroke-width", "1.5px") |
|
.attr("transform", ""); |
|
} |
|
|
|
</script> |
(first i tried IRC, nobody listening. then i tried stackOverflow complained about me not meeting their quality standards?! sigh, a comment via github)
I've replicated this example, except mine does NOT zoom when i click a region? I've made a pastie of my version: http://pastie.org/8436223
the regions render fine, i can click on each and get its name echoed to console, and the second echo of the 'b' var also looks correct?
anyone see a bug? i can also send along the opd_beats.json file if it'd help?