Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Last active May 7, 2017 21:09
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 jfsiii/15fc73e7facbbdb30b3afc384f6b5188 to your computer and use it in GitHub Desktop.
Save jfsiii/15fc73e7facbbdb30b3afc384f6b5188 to your computer and use it in GitHub Desktop.
Transform CSV to object, totaled by date
license: mit
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
<!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>
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