Hover over map on left to see zoomed in area on right.
forked from syntagmatic's block: Map Loupe
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
margin: 0;
width: 960px;
}
.counties {
fill: none;
}
.states {
fill: none;
stroke: #fff;
stroke-linejoin: round;
}
.q0-9 { fill:rgb(247,251,255); }
.q1-9 { fill:rgb(222,235,247); }
.q2-9 { fill:rgb(198,219,239); }
.q3-9 { fill:rgb(158,202,225); }
.q4-9 { fill:rgb(107,174,214); }
.q5-9 { fill:rgb(66,146,198); }
.q6-9 { fill:rgb(33,113,181); }
.q7-9 { fill:rgb(8,81,156); }
.q8-9 { fill:rgb(8,48,107); }
</style>
<body>
<div id="main" style="float: left;"></div>
<div id="zoom" style="float: left;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script>
var width = 480,
height = 500;
var rateById = d3.map();
var quantile = d3.scale.quantile()
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var projection = d3.geo.albersUsa()
.scale(680)
.translate([width / 2, height / 2]);
var projectionzoom = d3.geo.albersUsa()
.scale(4000)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var pathzoom = d3.geo.path()
.projection(projectionzoom);
var svg = d3.select("#main").append("svg")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() {
var point = projectionzoom(projection.invert(d3.mouse(this)));
svgzoom.attr("transform", "translate(" + (width/2-point[0]) + "," + (height/2-point[1]) + ")")
});
var svgzoom = d3.select("#zoom").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
queue()
.defer(d3.json, "https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json")
.defer(d3.tsv, "unemployment.tsv", function(d) { rateById.set(d.id, +d.rate); })
.await(ready);
function ready(error, us) {
if (error) throw error;
quantile.domain(rateById.values());
svg.append("rect")
.style("fill", "#fff")
.attr("width", width)
.attr("height", height)
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("class", function(d) { return quantile(rateById.get(d.id)); })
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", path);
svgzoom.append("rect")
.style("fill", "#fff")
.attr("width", width)
.attr("height", height)
svgzoom.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("class", function(d) { return quantile(rateById.get(d.id)); })
.attr("d", pathzoom);
svgzoom.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", pathzoom);
}
</script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js
https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js