Built with blockbuilder.org
xxxxxxxxxx
<meta charset="utf-8">
<style type="text/css">
body {
font-family: arial, sans;
font-size: 14px;
width: 960px;
margin: 40px auto;
}
svg {
border: 1px solid #ccc;
}
.states {
stroke:#fff;
fill:none;
}
.counties {
stroke: #fff;
fill: none;
stroke-width: .3px;
}
.county-bubble {
stroke:#fff;
stroke-width: .3;
}
</style>
<body>
</body>
<script src="https://d3js.org/d3.v4.js" charset="utf-8"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script type="text/javascript">
var width = 960,
height = 500,
color = d3.scaleLinear()
.range(["#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15","#67000d"])
.domain([0,5,10,15,20,25,30,35,40]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.queue()
.defer(d3.csv, "suicide2010s.csv")
.defer(d3.json, "us.json")
.await(ready);
function ready(error, suicide, us) {
if (error) return console.warn(error);
console.log(suicide);
console.log(us);
console.log(topojson.feature(us, us.objects.counties).features);
suicideByCounty = {};
suicide.forEach(function(d) {
d["suicideRate"] = +d["suicideRate"];
suicideByCounty[+d["County Code"]] = d;
});
var path = d3.geoPath()
.projection(d3.geoAlbersUsa());
var subset = topojson.feature(us, us.objects.counties).features.filter(function(d) {
return d.id in suicideByCounty;
});
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(subset)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(suicideByCounty[d.id]["suicideRate"])
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a.id !== b.id; }))
.attr("class", "counties")
.attr("d", path);
svg.append("text")
.style("font-weight", "bold")
.attr("x", width - 170)
.attr("y", height - 180)
.text("Suicide per 100,000");
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("transform", function(d,i) {
return "translate(" + (width-160) + "," + (height - 170 + 16 * i) + ")";
})
.on("click", function(min,i) {
console.log(min,i);
});
legend.append("rect")
.attr("width", 12)
.attr("height", 12)
.style("fill", function(d) {
return color(d);
});
legend.append("text")
.attr("x", 16)
.attr("y", 7)
.attr("alignment-baseline", "middle")
.style("font-size", "12px")
.text(function(d) {
return d.toFixed(0);
});
}
</script>
Modified http://d3js.org/d3.v4.js to a secure url
Modified http://d3js.org/topojson.v1.min.js to a secure url
https://d3js.org/d3.v4.js
https://d3js.org/topojson.v1.min.js