Suppose I have this CSV file
Day,What
2013-10-27,Apple
2013-10-27,Cake
2013-10-27,Apple
2013-10-28,Apple
2013-10-28,Apple
2013-10-28,Blueberry
2013-10-28,Orange
I would like to drawn with D3.js a time-series graph. All I need to do is to display the sum of the number of rows for each day. As an example, the 27 of November should have 3 as value, the 28th should have 4.
Is there a way I can archive it? I've been trying to create a new dataset out of the CSV one with no positive results.
var dataset;
d3.csv("data.csv", function(d) {
return {
Day: new Date(d.Day),
What: d.What
};
},
function(error, rows) {
dataset = rows;
// I SUPPOSE THE FUNCTION THAT GENERATE THE NEW DATASET SHOULD GO THERE
});
Suppose I have this CSV file
Day,What
2013-10-27,Apple
2013-10-27,Cake
2013-10-27,Apple
2013-10-28,Apple
2013-10-28,Apple
2013-10-28,Blueberry
2013-10-28,Orange
I would like to drawn with D3.js a time-series graph. All I need to do is to display the sum of the number of rows for each day. As an example, the 27 of November should have 3 as value, the 28th should have 4.
Is there a way I can archive it? I've been trying to create a new dataset out of the CSV one with no positive results.
var dataset;
d3.csv("data.csv", function(d) {
return {
Day: new Date(d.Day),
What: d.What
};
},
function(error, rows) {
dataset = rows;
// I SUPPOSE THE FUNCTION THAT GENERATE THE NEW DATASET SHOULD GO THERE
});
Share
Improve this question
asked Oct 31, 2013 at 15:52
philapplephilapple
631 silver badge4 bronze badges
2 Answers
Reset to default 2You can use the fast lookup of JavaScript Object properties as an index while you accumulate your counts:
var counts = {};
rows.forEach(function(r) {
if (!counts[r.Day]) {
counts[r.Day] = 0;
}
counts[r.Day]++;
});
Then you'll need to convert this back into an array so you can use it with D3:
var data = [];
Object.keys(counts).forEach(function(key) {
data.push({
day: key,
count: counts[key]
});
});
I'm assuming here that you want to ignore the "What" column because you only talked about aggregating a count of days and there are different "What" values for the same day. If you want to consider "What" while you count you can just build it into the key of your original counts
object (r.Day + r.What
).
EDIT: Based on ment, adding the code with a pound key.
var counts = {};
rows.forEach(function(r) {
var key = r.Day + r.What;
if (!counts[key]) {
counts[key] = {
day: r.Day,
what: r.What,
count: 0
};
}
counts[key].count++;
});
var data = [];
Object.keys(counts).forEach(function(key) {
data.push(counts[key]);
});
A function along these lines should do the trick. I can't test it out at a puter at the moment so let me know if it doesn't work as I may have made a typo!
function createNewArray(dataset) {
var lastDate = dataset[0].Date; // initialise
var count = 0;
var newArray = new Array();
for (var i = 0; i < dataset.length; i++) {
if (dataset[i].Date != lastDate || i == dataset.length-1) {
// on a new day (or the final row), so push value and reset count variable
newArray.push({ Day: lastDate, Count: count });
count = 0;
}
count++;
lastDate = dataset[i].Date;
}
return newArray;
}