Created
October 5, 2015 20:35
-
-
Save DimsumPanda/d71d53cfa99de58e1b22 to your computer and use it in GitHub Desktop.
Week 6: My Line Plot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!-- Modification of an example by Scott Murray from Knight D3 course --> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Line Chart with Multiple Lines</title> | |
<link href="style.css" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
<h1>Maternal Mortality Rate</h1> | |
<p>Maternal Mortality Rate, 1990-2013. Subsaharan Africa and South Asia compared to the United States. Source: WHO (2015) Global Health Observatory Data Repository, Source: World Bank</p> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script type="text/javascript" src="multipleLines2.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Dimensions and padding | |
var width = 700; | |
var height = 600; | |
var margin = {top: 20, right: 10, bottom: 40, left:100}; | |
//Set up date formatting and years | |
var dateFormat = d3.time.format("%Y"); | |
//Set up scales | |
var xScale = d3.time.scale() | |
.range([ margin.left, width - margin.right - margin.left ]); | |
var yScale = d3.scale.linear() | |
.range([ margin.top, height - margin.bottom ]); | |
//Configure axis generators | |
var xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
.ticks(6) | |
.tickFormat(function(d) { | |
return dateFormat(d); | |
}) | |
.outerTickSize([0]); | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left") | |
.outerTickSize([0]); | |
//Configure line generator | |
// each line dataset must have a d.year and a d.rate for this to work. | |
var line = d3.svg.line() | |
.x(function(d) { | |
return xScale(dateFormat.parse(d.year)); | |
}) | |
.y(function(d) { | |
return yScale(+d.rate); | |
}); | |
//Create the empty SVG image | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
//Load data | |
d3.csv("regionsMaternalMortality.csv", function(data) { | |
//Data is loaded in, but we need to restructure it. | |
//Remember, each line requires an array of x/y pairs; | |
//that is, an array of arrays, like so: | |
// | |
// [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ] | |
// | |
//We, however, are using 'year' as x and 'rate' as y. | |
//We also need to know which country belongs to each | |
//line, so we will build an array of objects that is | |
//structured like this: | |
/* | |
[ | |
{ | |
country: "Australia", | |
mortalityRate: [ | |
{ year: 1961, rate: 90589.568 }, | |
{ year: 1962, rate: 94912.961 }, | |
{ year: 1963, rate: 101029.517 }, | |
… | |
] | |
}, | |
{ | |
country: "Bermuda", | |
mortalityRate: [ | |
{ year: 1961, rate: 176.016 }, | |
{ year: 1962, rate: 157.681 }, | |
{ year: 1963, rate: 150.347 }, | |
… | |
] | |
}, | |
… | |
] | |
*/ | |
//Note that this is an array of objects. Each object | |
//contains two values, 'country' and 'mortalityRate'. | |
//The 'mortalityRate' value is itself an array, containing | |
//more objects, each one holding 'year' and 'rate' values. | |
//New array with all the years, for referencing later | |
var years = ["1990", "1995", "2000", "2005", "2010", "2013"]; | |
// or you could get this by doing: | |
// var years = d3.keys(data[0]).slice(0, 54-4); // | |
//Create a new, empty array to hold our restructured dataset | |
var dataset = []; | |
//Loop once for each row in data | |
data.forEach(function (d, i) { | |
var mortality = []; | |
//Loop through all the years - and get the mortalityRate for this data element | |
years.forEach(function (y) { | |
// If value is not empty | |
if (d[y]) { | |
//Add a new object to the new mortalityRate data array - for year, rate | |
mortality.push({ | |
year: y, | |
rate: d[y] // this is the value for, for example, d["2004"] | |
}); | |
} | |
}); | |
//Create new object with this country's name and empty array | |
// d is the current data row... from data.forEach above. | |
dataset.push( { | |
country: d.name, | |
mortalityRate: mortality // we just built this! | |
} ); | |
}); | |
//Uncomment to log the original data to the console | |
// console.log(data); | |
//Uncomment to log the newly restructured dataset to the console | |
console.log(dataset); | |
//Set scale domains - max and mine of the years | |
xScale.domain( | |
d3.extent(years, function(d) { | |
return dateFormat.parse(d); | |
})); | |
// max of mortalityRate to 0 (reversed, remember) | |
yScale.domain([ | |
d3.max(dataset, function(d) { | |
return d3.max(d.mortalityRate, function(d) { | |
return +d.rate; | |
}); | |
}), | |
0 | |
]); | |
//Make a group for each country | |
var groups = svg.selectAll("g") | |
.data(dataset) | |
.enter() | |
.append("g"); | |
//Append a title with the country name (so we get easy tooltips) | |
groups.append("title") | |
.text(function(d) { | |
return d.country; | |
}); | |
//Within each group, create a new line/path, | |
//binding just the mortalityRate data to each one | |
var lines = groups.selectAll("path") | |
.data(function(d) { // because there's a group with data already... | |
return [ d.mortalityRate ]; // it has to be an array for the line function | |
}) | |
.enter() | |
.append("path") | |
.attr("class", "line") | |
.attr("d", line) | |
lines.attr("stroke", function (d) { | |
if(d.name === "United States"){ | |
return "#FF0099";} | |
else{ | |
return "#0099FF"; | |
}}); | |
lines.on("mouseover", mouseoverFunc) | |
.on("mousemove", mousemoveFunc) | |
.on("mouseout", mouseoutFunc); | |
//Axes | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (height - margin.bottom) + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + margin.left + ",0)") | |
.call(yAxis); | |
svg.append("text") | |
.attr("class", "xlabel") | |
.attr("transform", "translate(" + width/2 + "," + (height - 5) + ")") | |
.style("text-anchor", "middle") | |
.text("Maternal Mortality Rate (per 100,000 live births)"); | |
svg.append("text") | |
.attr("class", "ylabel") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 60) | |
.attr("x",0 - (height / 2)) | |
// .attr("dy", "1em") | |
.style("text-anchor", "end") | |
.text("Year"); | |
}); | |
function mouseoverFunc(d) { | |
return tooltip | |
.style("display", null) // removes the display none setting from it | |
.html("<p>Country: " + d.name ); | |
} | |
function mousemoveFunc(d) { | |
return tooltip | |
.style("top", (d3.event.pageY - 10) + "px") | |
.style("left", (d3.event.pageX + 10) + "px"); | |
} | |
function mouseoutFunc(d) { | |
return tooltip.style("display", "none"); //sets it up to be invisible | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name | 1990 | 1995 | 2000 | 2005 | 2010 | 2013 | |
---|---|---|---|---|---|---|---|
Australia and New Zealand | 12.5 | 10.5 | 10.5 | 9 | 8.5 | 7 | |
Caribbean | 146.5 | 123.9 | 107.4 | 100.2 | 102 | 91.5 | |
Central America | 142.375 | 115.5 | 103.75 | 90 | 82.875 | 80.75 | |
Central Asia | 75.2 | 92.8 | 77.8 | 64.2 | 54.4 | 48.4 | |
Eastern Africa | 965.8823529 | 862.8235294 | 728.1176471 | 605 | 496.5882353 | 437.8235294 | |
Eastern Asia | 62.8 | 61.4 | 66.4 | 54.8 | 47 | 44 | |
Eastern Europe | 48.5 | 35.5 | 28.2 | 18.3 | 17.8 | 13.6 | |
Melanesia | 262.25 | 209.75 | 185.5 | 154.75 | 133 | 123.75 | |
Micronesia | 210 | 190 | 165 | 145 | 120 | 113 | |
Middle Africa | 1008.888889 | 967.7777778 | 852.2222222 | 693.3333333 | 590 | 532.2222222 | |
North America | 9 | 9 | 10 | 14 | 20 | 19.5 | |
Northern Africa | 238.6666667 | 203.6666667 | 170.1666667 | 142.3333333 | 120.8333333 | 112.5 | |
Northern Europe | 19.2 | 17.7 | 14 | 10.6 | 9.5 | 7.3 | |
Polynesia | 110.5 | 99.5 | 90 | 86.5 | 91 | 89 | |
South America | 152.0833333 | 131.8333333 | 126.9166667 | 113.3333333 | 108 | 102.75 | |
South-Eastern Asia | 444.7272727 | 352.2727273 | 255.9090909 | 185 | 135.5454545 | 118.8181818 | |
Southern Africa | 420 | 380 | 402 | 380 | 280 | 248 | |
Southern Asia | 551.3333333 | 440.1111111 | 346.5555556 | 242.1111111 | 173.8888889 | 147 | |
Southern Europe | 13.33333333 | 12 | 10.83333333 | 10.58333333 | 9.583333333 | 9.083333333 | |
Western Africa | 903.75 | 868.125 | 766.5 | 643.3125 | 547.375 | 506.4375 | |
Western Asia | 72.58823529 | 65 | 55.11764706 | 46.47058824 | 41.23529412 | 38.64705882 | |
Western Europe | 10 | 9.428571429 | 9.142857143 | 9 | 8.142857143 | 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background-color: white; | |
font-family: Helvetica, Arial, sans-serif; | |
color: rgb(0,153,255); | |
max-width: 900px; | |
min-width: 400px; | |
/*margin: 1em auto; | |
padding: 1em 3em;*/ | |
background-color: #fff; | |
} | |
h1 { | |
font-size: 24px; | |
margin: 0; | |
} | |
p { | |
font-size: 14px; | |
margin: 10px 0 0 0; | |
} | |
svg { | |
background-color: white; | |
} | |
path.line { | |
fill: none; | |
stroke-width: 2px; | |
} | |
path:hover | |
{ | |
stroke: #FF9900; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
stroke-width: 1px; | |
} | |
.axis text { | |
font-family: sans-serif; | |
font-size: 11px; | |
} | |
.ylabel,.xlabel { | |
font-size: 14px; | |
color: #0099FF; | |
} | |
text.ylabel { | |
font-size: 20px; | |
color: #0099FF; | |
} | |
var tooltip = d3.select("body") | |
.append("div") | |
.attr("class", "tooltip"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment