Created
September 17, 2015 22:32
-
-
Save jowang0319/83552dce753ec338a504 to your computer and use it in GitHub Desktop.
Week4: Heatmap table
This file contains 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
Year | High-income | Upper-middle-income | Lower-middle-income | Low-income | |
---|---|---|---|---|---|
1995 | 61.2 | 50.1 | 35.9 | 33.8 | |
1996 | 61.1 | 48.5 | 35 | 33.1 | |
1997 | 60.6 | 49.4 | 33.4 | 34.3 | |
1998 | 59.5 | 48.7 | 33 | 35.4 | |
1999 | 59.3 | 48 | 34 | 34.1 | |
2000 | 59.3 | 46.7 | 34 | 36.6 | |
2001 | 59.8 | 46.4 | 33.4 | 36.6 | |
2002 | 59.4 | 46.9 | 32.8 | 34.5 | |
2003 | 58.8 | 47.2 | 32.2 | 35.2 | |
2004 | 58.9 | 47.6 | 31.1 | 35.5 | |
2005 | 59.1 | 46.8 | 31.2 | 35.1 | |
2006 | 59.7 | 48 | 33.1 | 37.8 | |
2007 | 59.7 | 50.6 | 34.2 | 37.8 | |
2008 | 60.2 | 52.4 | 35 | 37.2 | |
2009 | 61.3 | 54.3 | 35.6 | 37.1 | |
2010 | 61.2 | 54.8 | 36.3 | 39.1 | |
2011 | 61.1 | 56 | 36 | 39.2 | |
2012 | 60.6 | 56.2 | 36.5 | 38.1 | |
2013 | 60.9 | 56.2 | 37.1 | 39 |
This file contains 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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>CSV Data in a Table with D3</title> | |
<style> | |
#container { | |
position:relative; | |
font-family:Arial,Helvetica,sans-serif; | |
max-width:300px; | |
padding:0px 20px 0px 20px; | |
float:right; | |
margin-right:290px; | |
background-color:rgba(0,153,255,0.05); | |
text-align:justify; | |
border-radius:20px; | |
} | |
#header { | |
margin-left:310px; | |
margin-right:300px; | |
margin-top:30px; | |
text-align:left; | |
font-family:Helvetica,sans-serif; | |
color:#0099FF; | |
} | |
.strong { | |
font-size:18px; | |
line-heigh:50%; | |
color:#0099FF; | |
} | |
.text { | |
max-width:300px; | |
line-height: 130%; | |
} | |
a:visited { | |
color: grey; | |
} | |
a:hover { | |
color: #FF0099; | |
} | |
a:link { | |
color: #0099FF; | |
} | |
table { | |
margin-left:310px; | |
padding: 10px; | |
float:left; | |
padding:10px; | |
font-family:Arial, sans-serif; | |
border-collapse:collapse; | |
} | |
td { | |
text-align:center; | |
font-size: 1em; | |
border: 1px solid rgba(0,153,255,0.05); | |
padding: 3px 7px 2px 7px; | |
} | |
th { | |
cursor: pointer; | |
font-size: 1em; | |
border: 1px solid rgba(0,153,255,0.05); | |
padding: 3px 7px 2px 7px; | |
background-color: rgba(0,153,255,0.8); | |
color:white; | |
} | |
</style> | |
</head> | |
<body> | |
<h1 id="header">Government Resources for Health as a Percentage of Total Expenditure on Health</h1> | |
<div id="container"> | |
<h4 class="strong">INTRODUCTION:</h4> | |
<p class= "text">The table below shows the government resources for health as a percentage of total expenditure on health according to the World Bank Income Group from year 1995 to year 2013.</p> | |
<p class="text">The difference indicates the gap between the percentage of government resource in High-income group countries and that in Low-income group.</p> | |
<h4 class="strong">SOURCE:</h4><a href="http://www.who.int/gho/en/"><p id="link">World Health Organization</p></a> | |
</div> | |
<div id="table"></div> | |
</body> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> | |
<script type="text/javascript" src="tabulate.js"></script> | |
<script type="text/javascript" src="stupidtable.js"></script> | |
<script type="text/javascript"> | |
d3.csv("data.csv",function(error,myData){ | |
if (error){ | |
console.log("Had an error loading file."); | |
} | |
var myArray = []; | |
var differences = []; | |
myData.forEach(function(d,i){ | |
d.difference = Math.round(d["High-income"] - d["Low-income"]); | |
d.highIncome = d["High-income"]; | |
d.lowIncome = d["Low-income"]; | |
d.upperMiddleIncome=d["Upper-middle-income"]; | |
d.lowerMiddleIncome=d["Lower-middle-income"]; | |
differences.push(d.difference); | |
myArray.push([d.Year, d.highIncome, d.lowIncome, d.difference]); | |
}) | |
console.log(myArray); | |
console.log(differences); | |
var table = d3.select("#table").append("table"); | |
var header = table.append("thead").append("tr"); | |
var headerObjs = [ | |
{ label: "Year" , sort_type: "int" }, | |
{ label: "High Income Group", sort_type: "int" }, | |
{ label: "Low Income Group", sort_type: "int" }, | |
{ label: "Difference", sort_type:"int"}, | |
]; | |
header | |
.selectAll("th") | |
.data(headerObjs) | |
.enter() | |
.append("th") | |
.attr("data-sort",function(d){return d.sort_type; }) | |
.text(function(d){ return d.label;}); | |
var tablebody = table.append("tbody"); | |
rows = tablebody | |
.selectAll("tr") | |
.data(myArray) | |
.enter() | |
.append("tr"); | |
console.log('Extent is ', d3.extent(differences)); | |
var colorScale = d3.scale.linear() | |
.domain(d3.extent(differences)) | |
.range(["rgba(0,153,255,0.1)","rgba(0,153,255,0.8"]); | |
cell = rows.selectAll("td") | |
.data(function(d){ | |
return d; | |
}) | |
.enter() | |
.append("td") | |
.style("background-color",function(d,i){ | |
if ( i === 3 ){ | |
return colorScale(d); | |
} | |
}) | |
.text(function(d){ | |
return d; | |
}); | |
$("table").stupidtable(); | |
}); | |
</script> | |
</html> |
This file contains 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
// Stupid jQuery table plugin. | |
(function($) { | |
$.fn.stupidtable = function(sortFns) { | |
return this.each(function() { | |
var $table = $(this); | |
sortFns = sortFns || {}; | |
sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns); | |
$table.data('sortFns', sortFns); | |
$table.on("click.stupidtable", "thead th", function() { | |
$(this).stupidsort(); | |
}); | |
}); | |
}; | |
// Expects $("#mytable").stupidtable() to have already been called. | |
// Call on a table header. | |
$.fn.stupidsort = function(force_direction){ | |
var $this_th = $(this); | |
var th_index = 0; // we'll increment this soon | |
var dir = $.fn.stupidtable.dir; | |
var $table = $this_th.closest("table"); | |
var datatype = $this_th.data("sort") || null; | |
// No datatype? Nothing to do. | |
if (datatype === null) { | |
return; | |
} | |
// Account for colspans | |
$this_th.parents("tr").find("th").slice(0, $(this).index()).each(function() { | |
var cols = $(this).attr("colspan") || 1; | |
th_index += parseInt(cols,10); | |
}); | |
var sort_dir; | |
if(arguments.length == 1){ | |
sort_dir = force_direction; | |
} | |
else{ | |
sort_dir = force_direction || $this_th.data("sort-default") || dir.ASC; | |
if ($this_th.data("sort-dir")) | |
sort_dir = $this_th.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC; | |
} | |
$table.trigger("beforetablesort", {column: th_index, direction: sort_dir}); | |
// More reliable method of forcing a redraw | |
$table.css("display"); | |
// Run sorting asynchronously on a timout to force browser redraw after | |
// `beforetablesort` callback. Also avoids locking up the browser too much. | |
setTimeout(function() { | |
// Gather the elements for this column | |
var column = []; | |
var sortFns = $table.data('sortFns'); | |
var sortMethod = sortFns[datatype]; | |
var trs = $table.children("tbody").children("tr"); | |
// Extract the data for the column that needs to be sorted and pair it up | |
// with the TR itself into a tuple. This way sorting the values will | |
// incidentally sort the trs. | |
trs.each(function(index,tr) { | |
var $e = $(tr).children().eq(th_index); | |
var sort_val = $e.data("sort-value"); | |
// Store and read from the .data cache for display text only sorts | |
// instead of looking through the DOM every time | |
if(typeof(sort_val) === "undefined"){ | |
var txt = $e.text(); | |
$e.data('sort-value', txt); | |
sort_val = txt; | |
} | |
column.push([sort_val, tr]); | |
}); | |
// Sort by the data-order-by value | |
column.sort(function(a, b) { return sortMethod(a[0], b[0]); }); | |
if (sort_dir != dir.ASC) | |
column.reverse(); | |
// Replace the content of tbody with the sorted rows. Strangely | |
// enough, .append accomplishes this for us. | |
trs = $.map(column, function(kv) { return kv[1]; }); | |
$table.children("tbody").append(trs); | |
// Reset siblings | |
$table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc"); | |
$this_th.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir); | |
$table.trigger("aftertablesort", {column: th_index, direction: sort_dir}); | |
$table.css("display"); | |
}, 10); | |
return $this_th; | |
}; | |
// Call on a sortable td to update its value in the sort. This should be the | |
// only mechanism used to update a cell's sort value. If your display value is | |
// different from your sort value, use jQuery's .text() or .html() to update | |
// the td contents, Assumes stupidtable has already been called for the table. | |
$.fn.updateSortVal = function(new_sort_val){ | |
var $this_td = $(this); | |
if($this_td.is('[data-sort-value]')){ | |
// For visual consistency with the .data cache | |
$this_td.attr('data-sort-value', new_sort_val); | |
} | |
$this_td.data("sort-value", new_sort_val); | |
return $this_td; | |
}; | |
// ------------------------------------------------------------------ | |
// Default settings | |
// ------------------------------------------------------------------ | |
$.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"}; | |
$.fn.stupidtable.default_sort_fns = { | |
"int": function(a, b) { | |
return parseInt(a, 10) - parseInt(b, 10); | |
}, | |
"float": function(a, b) { | |
return parseFloat(a) - parseFloat(b); | |
}, | |
"string": function(a, b) { | |
return a.localeCompare(b); | |
}, | |
"string-ins": function(a, b) { | |
a = a.toLocaleLowerCase(); | |
b = b.toLocaleLowerCase(); | |
return a.localeCompare(b); | |
} | |
}; | |
})(jQuery); |
This file contains 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
/* | |
Code by Shawn Allen (@shawnbot) repro'd in d3noob's book, | |
http://www.d3noob.org/2013/02/add-html-table-to-your-d3js-graph.html, | |
but with minor modification by Lynn. | |
*/ | |
function tabulate(data, columns, id) { | |
var table = d3.select(id).append("table"), | |
thead = table.append("thead"), | |
tbody = table.append("tbody"); | |
// append the header row | |
thead.append("tr") | |
.selectAll("th") | |
.data(columns) | |
.enter() | |
.append("th") | |
.text(function(column) { return column; }); | |
// create a row for each object in the data | |
var rows = tbody.selectAll("tr") | |
.data(data) | |
.enter() | |
.append("tr"); | |
// create a cell in each row for each column | |
// At this point, the rows have data associated. | |
// So the data function accesses it. | |
var cells = rows.selectAll("td") | |
.data(function(row) { | |
// he does it this way to guarantee you only use the | |
// values for the columns you provide. | |
return columns.map(function(column) { | |
// return a new object with a value set to the row's column value. | |
return {value: row[column]}; | |
}); | |
}) | |
.enter() | |
.append("td") | |
.text(function(d) { return d.value; }); | |
return table; | |
} | |
// render the table | |
// var peopleTable = tabulate(data, ["date", "close"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment