Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created February 4, 2014 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/8811228 to your computer and use it in GitHub Desktop.
Save vicapow/8811228 to your computer and use it in GitHub Desktop.
<!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