Built with blockbuilder.org
xxxxxxxxxx
<meta charset="utf-8">
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;;
font-size: 16px;
width: 960px;
height: 500px;
margin: 20px auto;
}
.states {
stroke: #fff;
fill: none;
pointer-events: none;
}
.counties {
stroke: #fff;
fill: none;
stroke-width: .3px;
}
.tooltip {
position: absolute;
text-align: left;
padding: 5px;
font: 12px sans-serif;
background: white;
opacity: 0.9;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
.tooltip h3 {
margin: 0px 0px 8px;
}
</style>
<body>
</body>
<center>
<b>Heart Disease mortality</b><br>
1999-2016
<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;
var color = d3.scaleThreshold()
.range(["#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15","#67000d"])
.domain([0,100,200,300,400,500]);
var radius = d3.scaleSqrt()
.range([0,20])
.domain([0,10000]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
d3.queue()
.defer(d3.csv, "HeartDisease1999-2016.csv")
.defer(d3.json, "us.json")
.await(ready);
function ready(error, deaths, us) {
if (error) return console.warn(error);
deathsByFips = {};
deaths.forEach(function(d) {
d.Deaths = +d.Deaths;
d.Population = +d.Population;
d["Crude Rate"] = +(d["Crude Rate"].replace(" (Unreliable)", ""));
deathsByFips[+d["County Code"]] = d;
});
var format = d3.format(".2%");
var commas = d3.format(",");
var path = d3.geoPath()
.projection(d3.geoAlbersUsa());
var subset = topojson.feature(us, us.objects.counties).features.filter(function(d) {
return d.id in deathsByFips;
});
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(subset)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(deathsByFips[d.id]["Crude Rate"]);
})
.on("mouseover", function(d) {
var val = deathsByFips[d.id];
var fill_color = color(val["Crude Rate"]);
tooltip.html("");
tooltip.style("visibility", "visible")
.style("border", "3px solid " + fill_color);
tooltip.append("h3").text(val.County);
tooltip.append("div")
.text("Population: " + commas(val["Population"]));
tooltip.append("div")
.text("Deaths per 100,000: " +(val["Crude Rate"]));
d3.selectAll(".counties path")
.style("opacity", 1)
.style("stroke", null);
d3.select(this)
.style("opacity", 1)
.style("stroke", "#222")
.raise();
})
.on("mousemove", function() {
return tooltip.style("top", (d3.event.pageY-52) + "px").style("left", (d3.event.pageX+18) + "px");
})
.on("mouseout", function() {
tooltip.style("visibility", "hidden")
d3.selectAll(".counties path")
.style("opacity", 5);
d3.selectAll(".states")
.style("opacity", 5);
});
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 - 176)
.attr("y", height - 152)
.text("Deaths per 100,000");
var legend = svg.selectAll(".legend")
.data(color.domain().reverse())
.enter().append("g")
.attr("transform", function(d,i) {
return "translate(" + (width-170) + "," + (height - 144 + 16 * 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", 11)
.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