D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sarubenfeld
Full window
Github gist
2016 Presidential Election: Percentage Republican by State
<!DOCTYPE html> <meta charset="utf-8"> <link href="https://fonts.googleapis.com/css?family=Bungee+Hairline" rel="stylesheet"> <style> h2 { font-family: 'Bungee Hairline', cursive; font-size: 18px; text-anchor: middle } body { background: radial-gradient(720px at 490px, #081f2b 0%, #061616 100%); height: 960px; } .counties { stroke: #bfbfbf; stroke-width: .25; fill: #fff; } .states { stroke: white; stroke-width: .2; fill: none; } #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(255,245,240)} .q1-9{fill:rgb(254,224,210)} .q2-9{fill:rgb(252,187,161)} .q3-9{fill:rgb(252,146,114)} .q4-9{fill:rgb(251,106,74)} .q5-9{fill:rgb(239,59,44)} .q6-9{fill:rgb(203,24,29)} .q7-9{fill:rgb(165,15,21)} .q8-9{fill:rgb(103,0,13)} </style> <body> <div class="g-chart-container"> <h2 style="color:white"> 2016 Presidential Election: Percentage Republican by State </h2> </div> <svg width="960" height="600"></svg> <div id='neighborhoodPopover'> </div> <script src="https://d3js.org/d3.v4.min.js"></script> <script src="https://d3js.org/topojson.v2.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, 1]) .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.csv, "states_edit.csv", function(d) { rateById.set(d.FIPS, +d.per_gop); }) .await(ready) function ready(error, us) { if (error) throw error; var defs = svg.append("defs"); //Filter for the outside glow var filter = defs.append("filter") .attr("id","glow"); filter.append("feGaussianBlur") .attr("stdDeviation","3.5") .attr("result","coloredBlur"); var feMerge = filter.append("feMerge"); feMerge.append("feMergeNode") .attr("in","coloredBlur"); feMerge.append("feMergeNode") .attr("in","SourceGraphic"); svg.append("g") .attr("class", "states") .selectAll("path") .data(topojson.feature(us, us.objects.states).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", .5) 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() .ease(d3.easeElastic) .style("opacity", 0); }); svg.append("path") .datum(topojson.mesh(us, us.objects.states.id, function(a, b) { return a !== b; })) .attr("class", "states") .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); svg.append("g") .attr("id", "states") .selectAll("path") .data(topojson.feature(us, us.objects.states).features) .enter().append("path") .attr("d", path) .on("click", clicked); // svg.append("g") // .attr("id", "counties") // .selectAll("path") // .data(topojson.feature(us, us.objects.counties).features) // .enter().append("path") // .attr("d", path) // .on("click", clicked); svg.append("path") .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) .attr("id", "state-borders") .attr("d", path); } function clicked(d) { var x, y, k; if (d && centered !== d) { var centroid = path.centroid(d); x = centroid[0]; y = centroid[1]; k = 4; centered = d; } else { x = width / 2; y = height / 2; k = 1; centered = null; } g.selectAll("path") .classed("active", centered && function(d) { return d === centered; }); g.transition() .duration(200) .ease(d3.easePoly) .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")") .style("stroke-width", 1.5 / k + "px"); d3.selectAll(".states") .style("filter", "url(#glow)"); console.log(us); } </script>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js