D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sarubenfeld
Full window
Github gist
us_map_unemployment
<!DOCTYPE html> <meta charset="utf-8"> <style> .counties { fill: none; stroke: #fff; } .states { fill: none; stroke: #fff; stroke-linejoin: round; } #neighborhoodPopover { position: absolute; text-align: center; padding: 2px; font-family: futura; font-size: 12px; background: #fff; border: 0px; border-radius: 8px; pointer-events: none; opacity: 0; } .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> <svg width="960" height="600"></svg> <div id='neighborhoodPopover'> </div> <script src="//d3js.org/d3.v4.min.js"></script> <script src="//d3js.org/topojson.v1.min.js"></script> <script> var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var rateById = d3.map(); var quantize = d3.scaleQuantize() .domain([0, 0.15]) .range(d3.range(9).map(function(i) { return "q" + i + "-9"; })); var projection = d3.geoAlbersUsa() .scale(1280) .translate([width / 2, height / 2]); var path = d3.geoPath() .projection(projection); d3.queue() .defer(d3.json, "us.json") .defer(d3.json, "california.json") .defer(d3.json, "sf.json") .defer(d3.tsv, "unemployment.tsv", function(d) { rateById.set(d.id, +d.rate); }) .await(ready); function ready(error, us) { if (error) throw error; svg.append("g") .attr("class", "counties") .selectAll("path") .data(topojson.feature(us, us.objects.counties).features) .enter().append("path") .attr("class", function(d) { return quantize(rateById.get(d.id)); }) .attr("d", path) .on("mouseenter", function(d) { console.log(d); d3.select(this) .style("stroke-width", 1.5) .style("stroke-dasharray", 0) d3.select("#neighborhoodPopover") .transition() .style("opacity", 1) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY) + "px") .text(rateById.get(d.id)) }) .on("mouseleave", function(d) { d3.select(this) .style("stroke-width", .25) .style("stroke-dasharray", 1) d3.select("#neighborhoodPopovercountyText") .transition() .style("opacity", 0); }); svg.append("path") .datum(topojson.mesh(us, us.objects.counties.id, function(a, b) { return a !== b; })) .attr("class", "counties") .attr("d", path); } console,log(us); </script>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v1.min.js