Created
February 4, 2014 20:01
-
-
Save vicapow/8811228 to your computer and use it in GitHub Desktop.
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> | |
<body> | |
<script src="d3.js" charset="utf-8"></script> | |
<script> | |
// `data.csv` | |
// date,pass,fail,compid | |
// 01/2014,1,2,001 | |
// 01/2014,11,5,002 | |
// 02/2014,7,3,001 | |
// 02/2014,8,1,002 | |
// 03/2014,2,3,001 | |
// 03/2014,6,1,002 | |
d3.csv('data.csv', function(err, data){ | |
if(err) throw err | |
data = d3.nest() | |
.key(function(d){ return d.date }) // `GROUP BY date` | |
.rollup(function(values){ | |
// `values` is all the rows of a particular date | |
var counts = {}, keys = ['pass', 'fail'] | |
keys.forEach(function(key){ | |
counts[key] = d3.sum(values, function(d){ return d[key] }) | |
}) | |
return counts | |
}) | |
.entries(data) | |
// `data` will now look like: | |
// [ | |
// { | |
// "key": "01/2014", | |
// "values": { | |
// "pass": 12, | |
// "fail": 7 | |
// } | |
// }, | |
// { | |
// "key": "02/2014", | |
// "values": { | |
// "pass": 15, | |
// "fail": 4 | |
// } | |
// }, | |
// { | |
// "key": "03/2014", | |
// "values": { | |
// "pass": 8, | |
// "fail": 4 | |
// } | |
// } | |
// ] | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment