xxxxxxxxxx
<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: #000;
stroke-width: .25;
fill: none;
}
.bubbles {
stroke: #000;
fill-opacity: .5;
}
button {
font-family: Futura;
}
#popoverText {
position: absolute;
text-align: center;
padding: 2px;
font-family: Futura;
background: #fff;
border: 0px;
border-radius: 8px;
pointer-events: none;
opacity: 0;
}
</style>
<body>
<div class="g-chart-container">
<h2 style="color:white"> 2016 Presidential Election: Highest Number of Polling Center Closures by County </h2>
</div>
<button id="bubblesOn">Turn on Bubbles</button>
<button id="bubblesOff">Turn off Bubbles</button>
<svg width="960" height="720"></svg>
<div id='popoverText'> </div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
queue()
.defer(d3.csv, "polls.csv")
.defer(d3.json, "us.json")
.await(ready);
function ready(error, polls, us) {
if (error) throw error;
console.log(topojson.feature(us, us.objects.counties).features);
pollsLookup = {};
polls.forEach(function(d) {
d.count3 = +d.count3;
pollsLookup[d.FIPS] = d.count3;
})
locationLookup = {};
polls.forEach(function(d) {
locationLookup[d.FIPS] = d.county;
})
var counties = topojson.feature(us, us.objects.counties),
states = topojson.feature(us, us.objects.states);
var path = d3.geoPath()
.projection(d3.geoAlbersUsa()
.fitSize([width, height], counties));
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.selectAll(".counties")
.data(counties.features)
.enter().append("path")
.attr("class", "counties")
.attr("d", path)
.style("fill", function(d) { return pollsLookup[d.id] > 0 ? "#efefef" : "#fff" })
.on("mouseenter", function(d) {
// .style("fill", function(d) { return color(+d.properties.PCT_ROM);})
d3.select(this)
.style("stroke-width", 1.5)
d3.select("#popoverText")
.transition()
.style("opacity", 1)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY) + "px")
.text(locationLookup[d.id])
})
.on("mouseleave", function(d) {
d3.select(this)
.style("stroke-width", .25)
d3.select("#popoverText")
.transition()
.style("opacity", 0);
});
var statePaths = svg.selectAll(".states")
.data(states.features)
.enter().append("path")
.attr("class", "states")
.attr("d", function(d) { return path(d); })
d3.selectAll(".states")
.style("filter", "url(#glow)");
var pollsColorScale = d3.scaleThreshold()
.domain([1,910,1107,1880,2716,15782])
.range(["rgb(247,251,255)","rgb(222,235,247)","rgb(198,219,239)","rgb(158,202,225)","rgb(107,174,214)","rgb(66,146,198)", "rgb(8,48,107)"]);
var pollsBubbles = svg.selectAll(".bubbles")
.data(counties.features)
.enter().append("circle")
.attr("class", "bubbles")
.attr("r", function(d) { return Math.sqrt(pollsLookup[d.id])/Math.PI })
.attr("cx", function(d) { return path.centroid(d)[0] })
.attr("cy", function(d) { return path.centroid(d)[1] })
.style("fill", function(d) { return pollsColorScale(pollsLookup[d.id]); })
d3.selectAll("button")
.on("click", function() {
console.log(this.id);
var bubbleType = this.id;
d3.selectAll(".bubbles")
.transition()
.ease(d3.easeElastic)
.duration(3000)
.attr("r", function(d) {
return (bubbleType == "bubblesOff" ? 0 : Math.sqrt(pollsLookup[d.id])/Math.PI)
})
})
}
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v1.min.js
https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js