xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
background: black;
font-family: futura;
font-size: 12px;
fill: white;
}
h1, h2, h3, h4, h5, h6 { font-family: futura;
color: white; }
.counties {
stroke: #B6D7DE;
stroke-width: .25;
fill: #FF5126;
}
.states {
stroke: #DAEBEE;
fill: none;
}
#popoverText {
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;
}
.map-container path {
fill: #d0d0d0;
stroke: #FCEDDA;
stroke-width:.3px;
}
.divisions {
fill: none;
stroke: none;
}
</style>
<body>
<div class="g-chart-container">
<h1>Chicago Gun Traces</h1>
</div>
<div id='popoverText'> </div>
</body>
<svg width="960" height="720"></svg>
<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");
var colorScale = d3.scaleThreshold()
.domain([1,910,1107,1880,2716,15782])
.range(["#fff","#ffeda0","#fed976","#feb24c","#fd8d3c","#fc4e2a"]);
queue()
.defer(d3.csv, "guns-history.csv")
.defer(d3.json, "us.json")
.await(ready);
function ready(error, guns, us) {
if (error) throw error;
console.log(topojson.feature(us, us.objects.counties).features);
gunsLookup = {};
guns.forEach(function(d) {
d.count3 = +d.count3;
gunsLookup[d.FIPS] = d.count3;
})
locationLookup = {};
guns.forEach(function(d) {
locationLookup[d.FIPS] = d.county;
})
var counties = topojson.feature(us, us.objects.counties),
states = topojson.feature(us, us.objects.states);
var projection = d3.geoAlbersUsa()
.scale(1280)
.translate([width / 2, height / 2]);
var path = d3.geoPath()
.projection(projection);
// var path = d3.geoPath()
// .projection(d3.geoAlbersUsa()
// .fitSize([width, height], counties));
svg.selectAll(".counties")
.data(counties.features)
.enter().append("path")
.attr("class", "counties")
.attr("d", path)
.style("fill", function(d) { return gunsLookup[d.id] > 0 ? "#efefef" : "#fff" })
.on("mouseenter", function(d) {
d3.select(this)
.style("stroke-width", 1.5)
d3.select("#popoverText")
.transition()
.ease(d3.easeElastic)
.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()
.ease(d3.easeElastic)
.style("opacity", 0);
});
svg.selectAll(".states")
.data(states.features)
.enter().append("path")
.attr("class", "states")
.attr("d", path)
svg.selectAll(".bubbles")
.data(counties.features)
.enter().append("g")
.attr("class","bubbles")
.attr("transform",function(d) { return "translate(" + path.centroid(d) + ")"})
.append("circle")
.attr("r", function(d) { return Math.sqrt(gunsLookup[d.id])/Math.PI; })
.style("fill", function(d) { return colorScale(gunsLookup[d.id]); })
.style("fill-opacity", .25)
.style("stroke", "#FF5126")
.style("pointer-events","none")
}
</script>
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