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;
}
</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","#99000d"])
.domain([0,5,10,15,20,25]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.queue()
.defer(d3.csv, "suicide2015state.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.states).features);
suicideByState = {};
suicide.forEach(function(d) {
d["suicideRate"] = +d["suicideRate"];
suicideByState[+d["State Code"]] = d;
});
var path = d3.geoPath()
.projection(d3.geoAlbersUsa());
var subset = topojson.feature(us, us.objects.states).features.filter(function(d) {
return d.id in suicideByState;
});
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(subset)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(suicideByState[d.id]["suicideRate"])
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a.id !== b.id; }))
.attr("class", "states")
.attr("d", path);
svg.append("text")
.style("font-weight", "bold")
.attr("x", width - 170)
.attr("y", height - 158)
.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 - 150 + 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