Built with blockbuilder.org
Last active
May 7, 2017 21:09
-
-
Save jfsiii/15fc73e7facbbdb30b3afc384f6b5188 to your computer and use it in GitHub Desktop.
Transform CSV to object, totaled by date
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
license: mit |
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
Campaign | Click Date | clicked | |
---|---|---|---|
JAN SALES | 30/12/2012 | 1 | |
JAN SALES | 30/12/2012 | 1 | |
JAN SALES | 31/12/2012 | 1 | |
JAN SALES | 27/12/2012 | 0 | |
JAN SALES | 28/12/2012 | 1 | |
JAN SALES | 29/12/2012 | 1 | |
Unknown | 01/02/2013 | 0 | |
Unknown | 01/03/2013 | 1 | |
XMAS | 27/12/2012 | 0 | |
XMAS | 28/12/2012 | 1 | |
XMAS | 29/12/2012 | 1 | |
XMAS | 30/12/2012 | 1 | |
XMAS | 31/12/2012 | 1 |
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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script src="index.js"></script> | |
</body> |
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
d3.csv( | |
"data.csv", | |
function convertRow(row) { | |
// columns are: Campaign,Click Date,clicked | |
return { | |
campaign: row.Campaign, | |
date: row['Click Date'], | |
clicked: parseInt(row.clicked, 10) | |
}; | |
}, | |
function onData(err, data) { | |
console.log('given rows', data) | |
var totals = data.reduce(function (byDate, entry) { | |
var dateParser = d3.timeParse("%d/%m/%Y"); | |
var parsedDate = dateParser(entry.date); | |
var formattedDate = d3.isoFormat(parsedDate); | |
var key = formattedDate; | |
// make sure we have a record for this date | |
if (!byDate[key]) { | |
byDate[key] = { date: formattedDate } | |
} | |
// make sure we have a total for this campaign | |
if (!byDate[key][entry.campaign]) { | |
byDate[key][entry.campaign] = 0 | |
} | |
// increment the total for this campaign | |
byDate[key][entry.campaign] += entry.clicked | |
return byDate | |
}, {}) | |
console.log('totals, by date', totals) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment