Skip to content

Instantly share code, notes, and snippets.

@TaraZhu
Last active October 25, 2017 13:33
Show Gist options
  • Save TaraZhu/0c59f53cab62906496bb26c2961f133b to your computer and use it in GitHub Desktop.
Save TaraZhu/0c59f53cab62906496bb26c2961f133b to your computer and use it in GitHub Desktop.
NY_Death_Interactive
license: mit

This dataset is about the leading causes of death by sex and ethnicity in New York City in since 2007, the raw json file can be found here.

More about this theme click this link.

This line chart presents the top 5 causes of death each year.

Chart code forked from d3noob’s block.

Resize inspired by connieGao0819’s Block and Qingquan "Q" Zhao’s Block. Text resize inspired by Curran Kelleher’s Block.

This interactive vis use highlighting as technique, hover on text or line, the selected disease will increase opacity and stroke width.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>New York City Leading Causes of Death</title>
<style>
body {
font: 12px Arial;
}
path {
stroke: steelblue;
/* stroke-width: 2;*/
fill: none;
}
svg {
padding-top: 10px;
padding-left: 50px;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.legend {
/* font-size: 10px;*/
font-weight: bold;
text-anchor: middle;
}
</style>
</head>
<body>
<div id="chart"></div>
<script>
var chartDiv = document.getElementById("chart"),
svg = d3.select(chartDiv).append("svg"),
xAxis = svg.append("g"),
yAxis = svg.append("g"),
g = svg.append("g");
function mouseOver(className) {
d3.selectAll("." + className)
.style("opacity", 1.0)
.style("stroke-width", 4)
.style("font-size", "18px");
}
function mouseOut(className) {
d3.selectAll("." + className)
.style("opacity", 0.8)
.style("font-size", "10px")
.style("stroke-width", 2);
}
function redraw() {
var chartDivWidth = isNaN(window.innerWidth) ? window.clientWidth : window.innerWidth,
chartDivHeight = isNaN(window.innerHeight) ? window.clientHeight : window.innerHeight;
var margin = {top: 30, right: 20, bottom: 70, left: 50},
width = chartDivWidth - margin.left - margin.right,
height = chartDivHeight - margin.top - margin.bottom;
var parseDate = d3.timeParse("%Y");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var numline = d3.line()
.x(function (d) {
return x(d.Year);
})
.y(function (d) {
return y(d.Deaths);
});
svg
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
d3.csv("NY_Death.csv", function (error, data) {
data.forEach(function (d) {
d.Year = parseDate(d.Year);
d.Deaths = +d.Deaths;
});
x.domain(d3.extent(data, function (d) {
return d.Year;
}));
y.domain([0, d3.max(data, function (d) {
return d.Deaths;
})]);
var dataNest = d3.nest()
.key(function (d) {
return d.Cause;
})
.entries(data);
var color = d3.scaleOrdinal(d3.schemeCategory10);
legendSpace = width / dataNest.length;
dataNest.forEach(function (d, i) {
var path = g.selectAll("#tag" + d.key.replace(/\s+/g, "")).data([data]),
text = svg.selectAll("#text" + d.key.replace(/\s+/g, "")).data([data]);
path.exit().remove();
path.enter()
.append("path")
.attr("class", "line")
.attr("stroke-width", 2)
.style("stroke", function () {
return d.color = color(d.key);
})
.style("opacity", 0.8)
.attr("class", d.key.replace(/\s+/g, ""))
.attr("id", "tag" + d.key.replace(/\s+/g, ""))
.merge(path)
.attr("d", numline(d.values))
.on("mouseover", function(d){
mouseOver(this.className.baseVal)
})
.on("mouseout", function(d){
mouseOut(this.className.baseVal);
});
text.enter().append("text")
.style("opacity", 0.8)
.style("font-size", "10px")
.attr("class", "legend")
.attr("class", d.key.replace(/\s+/g, ""))
.attr("id", "text" + d.key.replace(/\s+/g, ""))
.text(d.key)
.merge(text)
.attr("x", (legendSpace / 2) + i * legendSpace)
.attr("y", height + (margin.bottom / 2) + 5)
.style("fill", function () {
return d.color = color(d.key);
})
.on("mouseover", function(d){
mouseOver(this.className.baseVal)
})
.on("mouseout", function(d){
mouseOut(this.className.baseVal);
});
});
xAxis
.merge(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
yAxis
.merge(yAxis)
.attr("class", "axis")
.call(d3.axisLeft(y));
});
}
redraw();
window.addEventListener("resize", redraw);
</script>
</body>
</html>
Cause Year Deaths
All Other Causes 2007 8142
All Other Causes 2008 8556
All Other Causes 2009 8445
All Other Causes 2010 9552
All Other Causes 2011 10387
All Other Causes 2012 10708
All Other Causes 2013 11075
All Other Causes 2014 11134
Diabetes Mellitus 2007 1555
Diabetes Mellitus 2008 1631
Diabetes Mellitus 2009 1678
Diabetes Mellitus 2010 1711
Diabetes Mellitus 2011 1770
Diabetes Mellitus 2012 1808
Diabetes Mellitus 2013 1843
Diabetes Mellitus 2014 1798
Diseases of Heart 2007 21441
Diseases of Heart 2008 21192
Diseases of Heart 2009 20084
Diseases of Heart 2010 17927
Diseases of Heart 2011 16900
Diseases of Heart 2012 16731
Diseases of Heart 2013 16759
Diseases of Heart 2014 16517
Influenza and Pneumonia 2007 2236
Influenza and Pneumonia 2008 2297
Influenza and Pneumonia 2009 2274
Influenza and Pneumonia 2010 2448
Influenza and Pneumonia 2011 2491
Influenza and Pneumonia 2012 2240
Influenza and Pneumonia 2013 2472
Influenza and Pneumonia 2014 2220
Malignant Neoplasms 2007 13249
Malignant Neoplasms 2008 13040
Malignant Neoplasms 2009 13174
Malignant Neoplasms 2010 13330
Malignant Neoplasms 2011 13438
Malignant Neoplasms 2012 13401
Malignant Neoplasms 2013 13358
Malignant Neoplasms 2014 13377
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment