Skip to content

Instantly share code, notes, and snippets.

@JulienAssouline
Last active November 19, 2017 22:26
Show Gist options
  • Save JulienAssouline/de09b7edc0956d19ee49467706bd5c38 to your computer and use it in GitHub Desktop.
Save JulienAssouline/de09b7edc0956d19ee49467706bd5c38 to your computer and use it in GitHub Desktop.
small multiple slope chart
delete Primary_Site Percentage_Change_1950_2014 Percentage_Change_1950_2014_1 Survival_rate_1950_1954 Survival_rate_2007_2013 difference_year X_1 X_2
1 23 Hodgkin (lymphoma) -83.6 -3.3 30 88.9 58.9 1 2
2 17 Prostate -38.5 -0.6 43 99.6 56.6 1 2
3 26 Leukemia -4.4 -0.3 10 64.8 54.8 1 2
4 25 Myeloma 209.7 1 6 50.6 44.6 1 2
5 12 Skin 163.1 1.2 49 93.6 44.6 1 2
6 24 Non-Hodgkin (lymphoma) 70.6 0.7 33 74.2 41.2 1 2
7 20 Kidney & Pelvis 27.8 0.4 34 74.8 40.8 1 2
8 18 Testis -70.2 -2.7 57 96.9 39.9 1 2
9 13 Breast (females) -38.5 -0.7 60 92.2 32.2 1 2
10 3 Colon & Rectum -57.7 -1.3 37 67.1 30.1 1 2
11 5 Rectum -71.2 -2.2 40 68.5 28.5 1 2
12 19 Bladder -29.8 -0.6 53 78.6 25.6 1 2
13 4 Colon -51.7 -1.1 41 66.5 25.5 1 2
14 0 Oral & Throat -50.3 -1.3 46 69.2 23.2 1 2
15 22 Thyroid -41.3 -0.9 80 98.4 18.4 1 2
16 1 Esophagus 24.7 0.7 4 22.3 18.3 1 2
17 2 Stomach -88.5 -3.3 12 29.9 17.9 1 2
18 6 Liver 61.6 0.9 1 17.7 16.7 1 2
19 16 Ovary -17 -0.4 30 46.2 16.2 1 2
20 9 Lung 183.5 1.1 6 19.6 13.6 1 2
21 15 Uterus -64.5 -1.5 72 85.3 13.3 1 2
22 8 Larynx -42.2 -0.8 52 64.5 12.5 1 2
23 21 Brain 55.9 0.4 21 33.4 12.4 1 2
24 14 Cervix Uteri -81.9 -3.1 59 70.8 11.8 1 2
25 7 Pancreas 27.4 0.1 1 8.9 7.9 1 2
<!Doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Learning D3 </title>
<link rel="stylsheet" href="main.css">
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<style type = "text/css">
body,html{
margin: 0;
padding: 0;
font-family: "Arial", sans-serif;
font-size: 11px;
text-align: left;
background-color: #F5F2EB;
}
#chart{
background-color: #F5F2EB;
border: 1px solid #F5F2EB;
}
.yaxis path{
fill: none;
stroke: none;
}
.yaxis line{
fill: none;
stroke: #dcd9d3;
stroke-width: 2px;
shape-rendering: crispEdges;
opacity: 0.7;
stroke-dasharray: 3,3;
}
.xaxis path,
.xaxis line{
fill: none;
stroke: none;
}
</style>
<body>
<!-- Place all DOM elements here -->
<script>
var w = 143
var h = 270
var margin = {
right: 20,
left: 70,
top: 50,
bottom: 10
}
var width = w - margin.right - margin.left;
var height = h - margin.top - margin.bottom;
// var svg = d3.select("body")
// .append("svg")
// .attr("id", "chart")
// .attr("width", w)
// .attr("height", h)
// .append("g")
// .attr("transform", "translate(0" + margin.left + "," + margin.top + ")");
var ytickValues = [0, 25, 50, 75, 100];
var xScale = d3.scaleLinear()
.range([0, width])
var yScale = d3.scaleLinear()
.range([height, 0])
var xAxis = d3.axisBottom()
.scale(xScale)
.tickSizeOuter(0)
var yAxis = d3.axisLeft()
.scale(yScale)
.tickValues(ytickValues)
.tickSize(-width, 0, 0)
.tickSizeOuter(0)
d3.csv("cancer_survival_rates.csv", function(error, data){
data.forEach(function(d){
d.X_1 = +d.X_1;
d.X_2 = +d.X_2;
d.Survival_rate_1950_1954 = +d.Survival_rate_1950_1954;
d.Survival_rate_2007_2013 = +d.Survival_rate_2007_2013;
d.Primary_Site = d.Primary_Site;
});
xScale.domain([0, 2]);
yScale.domain([0, 100]);
var svgs = d3.select("body")
.selectAll("svg")
.data(data)
.enter()
.append("svg")
.attr("id", "chart")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
svgs.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0" + height + ")")
.call(xAxis)
svgs.append("g")
.attr("class", "yaxis")
.attr("transform", "translate(0,0)")
.call(yAxis)
.selectAll("text")
.style("fill", "none")
// lines
svgs.append("line")
.attr("class", "lines")
.attr("x1", function(d){
return xScale(d.X_1)
})
.attr("x2", function(d){
return xScale(d.X_2)
})
.attr("y1", function(d){
return yScale(d.Survival_rate_1950_1954)
})
.attr("y2", function(d){
return yScale(d.Survival_rate_2007_2013)
})
.style("stroke", "steelblue")
.style("stroke-width", 1.5)
// 1950-1954
svgs.append("circle")
.attr("class", "first")
.attr("r", 2.5)
.attr("cx", function(d){
return xScale(d.X_1);
})
.attr("cy", function(d){
return yScale(d.Survival_rate_1950_1954)
})
.style("fill", "steelblue")
// 2007-2013
svgs.append("circle")
.attr("class", "first")
.attr("r", 2.5)
.attr("cx", function(d){
return xScale(d.X_2);
})
.attr("cy", function(d){
return yScale(d.Survival_rate_2007_2013)
})
.style("fill", "steelblue")
// adding text
//https://stackoverflow.com/questions/19447321/how-to-linebreak-an-svg-text-in-javascript
svgs.append("text")
.each(function(d){
var arr = d.Primary_Site.split(/[\s]/)
for (i = 0; i < arr.length; i++){
d3.select(this)
.append("tspan") // applying a tspan which is how you can create line breaks with svgs
.text(arr[i])
.attr("dy", 10)
.attr("x", -5)
.attr("class", "tspan" + i);
}
})
.attr("y", height - 250)
.style("font-size", "12px")
svgs.append("text")
.attr("x", -45)
.attr("y", 35)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "Biggest increase"
}
})
.style("font-size", "12px")
svgs.append("text")
.attr("x", -45)
.attr("y", 50)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "in survival"
}
})
.style("font-size", "12px")
svgs.append("text")
.attr("x", -25)
.attr("y", 155)
.text(function(d){
if(d.Primary_Site == "Pancreas"){
return "Smallest increase"
}
})
.style("font-size", "12px")
svgs.append("text")
.attr("x", -25)
.attr("y", 170)
.text(function(d){
if(d.Primary_Site == "Pancreas"){
return "in survival"
}
})
.style("font-size", "12px")
svgs.append("text")
.attr("x", -30)
.attr("y", 5)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "100%"
}
})
.style("font-size", "11px")
svgs.append("text")
.attr("x", -20)
.attr("y", 215)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "0%"
}
})
.style("font-size", "11px")
svgs.append("text")
.attr("x", 20)
.attr("y", 10)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "2007-2013"
}
})
.style("font-size", "11px")
.style("font-weight", "bold")
svgs.append("text")
.attr("x", -20)
.attr("y", 165)
.text(function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "1950-1954"
}
})
.style("font-size", "11px")
.style("font-weight", "bold")
svgs.append("text")
.attr("x", 50)
.attr("y", -10)
.text(function(d){
if(d.Primary_Site == "Prostate"){
return "99%"
}
})
.style("font-size", "11px")
svgs.append("text")
.attr("x", 0)
.attr("y", 135)
.text(function(d){
if(d.Primary_Site == "Prostate"){
return "43% survival"
}
})
.style("font-size", "11px")
svgs.append("line")
.attr("x1", 5)
.attr("y1", 5)
.attr("x2", 15)
.attr("y2", 18)
.attr("stroke-width", 1)
.attr("transform", "translate(20,175)")
.attr("stroke", function(d){
if(d.Primary_Site == "Pancreas"){
return "black"
}
})
.style("opacity", 0.7);
svgs.append("line")
.attr("x1", 5)
.attr("y1", 1)
.attr("x2", 23)
.attr("y2", 28)
.attr("stroke-width", 1)
.attr("transform", "translate(12,60)")
.attr("stroke", function(d){
if(d.Primary_Site == "Hodgkin (lymphoma)"){
return "black"
}
})
.style("opacity", 0.7);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment