|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>Maternal Mortality Rates</title> |
|
<link href="style.css" rel="stylesheet" type="text/css" /> |
|
</head> |
|
<body> |
|
<div id="page"> |
|
<h1>Maternal mortality ratio (per 100 000 live births)</h1> |
|
<p>Millennium Development Goal Indicator for monitoring Goal 5: improving maternal health. The maternal mortality ratio represents the risk associated with each pregnancy, i.e. the obstetric risk.</p> |
|
<p>Source: WHO (2015) Global Health Observatory Data Repository</p> |
|
<div id="table"></div> |
|
|
|
</div> |
|
|
|
</body> |
|
|
|
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> |
|
|
|
<!-- load the function file you need before you call it... --> |
|
<script type="text/javascript" src="tabulate.js"></script> |
|
|
|
<script type="text/javascript"> |
|
|
|
//Load in contents of CSV file, and do things to the data. |
|
|
|
d3.csv("maternalMortalityRegion.csv", function(error, myData) { |
|
|
|
if (error) { |
|
console.log("Had an error loading file."); |
|
} |
|
// myData.forEach(function(data_item, index){ |
|
|
|
// // NB: it's much more common to see (d, i) as shorthand in D3 code. |
|
|
|
// console.log(index, data_item); |
|
|
|
// // here we are making strings into numbers using type coercion: |
|
// data_item.year1990 = +data_item.year1990; |
|
// data_item.year1995 = +data_item.year1995; |
|
// data_item.year2000 = +data_item.year2000; |
|
// data_item.year2010 = +data_item.year2010; |
|
// data_item.year2013 = +data_item.year2013; |
|
// data_item.region = +data_item.region; |
|
|
|
// // now we add another data object value, a calculated value. |
|
// data_item.difference = data_item.year2013 - data_item.year1990; |
|
|
|
// data_item.sum = data_item.year1990 + data_item.year1995 + data_item.year2000 + data_item.year2010 + data_item.year2013; |
|
|
|
// total1990 = total1990 + data_item.year1990; |
|
// total1995 = total1995 + data_item.year1995; |
|
// total2000 = total2000 + data_item.year2000; |
|
// total2010 = total2010 + data_item.year2010; |
|
// total2013 = total2013 + data_item.year2013; |
|
|
|
// console.log(index, data_item); |
|
|
|
// }); |
|
|
|
|
|
myData.sort(function(a, b){ |
|
return d3.ascending(a.region, b.region); |
|
}); |
|
|
|
// We'll be using simpler data as values, not objects. |
|
var myArray = []; |
|
|
|
myData.forEach(function(d, i){ |
|
|
|
d.difference = d.year2013 - d.year1990; |
|
|
|
|
|
// Add a new array with the values of each: |
|
myArray.push([d.name, d.region, d.year1990, d.year1995, d.year2000, d.year2005, d.year2010, d.year2013, d.difference]); |
|
|
|
}); |
|
|
|
console.log(myData); |
|
console.log(myArray); |
|
|
|
|
|
console.log("sorted", myData); |
|
|
|
|
|
// You could also have made the new array with a map function! |
|
|
|
var table = d3.select("#table").append("table"); |
|
|
|
var header = table.append("thead").append("tr"); |
|
|
|
header |
|
.selectAll("th") |
|
.data(["Country", "Region", "1990", "1995", "2000", "2005", "2010", "2013", "Difference"]) |
|
.enter() |
|
.append("th") |
|
.text(function(d) { return d; }); |
|
|
|
var tablebody = table.append("tbody"); |
|
|
|
rows = tablebody |
|
.selectAll("tr") |
|
.data(myArray) |
|
.enter() |
|
.append("tr"); |
|
|
|
// We built the rows using the nested array - now each row has its own array. |
|
|
|
cells = rows.selectAll("td") |
|
// each row has data associated; we get it and enter it for the cells. |
|
.data(function(d) { |
|
console.log(d); |
|
return d; |
|
}) |
|
.enter() |
|
.append("td") |
|
.text(function(d) { |
|
return d; |
|
}); |
|
|
|
table.selectAll("tbody tr") |
|
|
|
}); |
|
|
|
|
|
</script> |
|
|
|
</html> |