|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.states { |
|
fill: none; |
|
stroke: #fff; |
|
stroke-linejoin: round; |
|
} |
|
|
|
.q0-9 { fill:rgb(251,247,255); } |
|
.q1-9 { fill:rgb(235,222,247); } |
|
.q2-9 { fill:rgb(219,198,239); } |
|
.q3-9 { fill:rgb(202,158,225); } |
|
.q4-9 { fill:rgb(174,107,214); } |
|
.q5-9 { fill:rgb(146,66,198); } |
|
.q6-9 { fill:rgb(113,33,181); } |
|
.q7-9 { fill:rgb(81,8,156); } |
|
.q8-9 { fill:rgb(48,8,107); } |
|
|
|
.p { fill: #eee; } |
|
|
|
</style> |
|
<body> |
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
<script src="http://d3js.org/queue.v1.min.js"></script> |
|
<script src="http://d3js.org/topojson.v0.min.js"></script> |
|
<script> |
|
|
|
var width = 960, |
|
height = 500; |
|
|
|
var quantize = d3.scale.quantize() |
|
.domain([0, .15]) |
|
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; })); |
|
|
|
var path = d3.geo.path(); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width) |
|
.attr("height", height); |
|
|
|
queue() // see the bottle server |
|
.defer(d3.json, "/static/us.json") |
|
.defer(d3.tsv, "/static/unemployment.tsv") |
|
.await(ready); |
|
|
|
function ready(error, us, unemployment) { |
|
// (unemployment)rateById is calculated from unemployment data |
|
var rateById = {}; |
|
unemployment.forEach(function(d) { rateById[d.id] = +d.rate; }); |
|
|
|
// geometries of all the counties in the US |
|
var countyGeom = topojson.object(us, us.objects.counties).geometries; |
|
var stGeom = []; |
|
var c; |
|
|
|
console.log(); |
|
svg.append("g") |
|
.attr("class", "counties") |
|
.selectAll("path") |
|
.data(countyGeom).enter() |
|
.append("path") // <path d="..." class="q#(d.id)-9" /> |
|
.attr("class", function(d) { |
|
if(Math.floor(d.id / 1000) == 30 || Math.floor(d.id / 1000) == 36 || Math.floor(d.id / 1000) == 06 || Math.floor(d.id / 1000) == 12 || Math.floor(d.id / 1000) == 48 || Math.floor(d.id / 1000) == 29){ |
|
c = quantize(rateById[d.id]); |
|
} else { c= "p";} |
|
return c; |
|
}) |
|
.attr("d", path); |
|
|
|
// state outlines |
|
svg.append("path") |
|
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a.id !== b.id; })) |
|
.attr("class", "states") |
|
.attr("d", path); |
|
} |
|
|
|
</script> |