Count the number of lines of a CSV file, with d3.js

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 draw a time series chart with D3.js. All I have to do is display the sum of the number of rows for each day. As an example, November 27 should have a value of 3 as a value, 28 should be 4.

Is there any way to archive it? I am trying to create a new dataset from CSV without 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 }); 
+6
source share
2 answers

You can use a quick search for the properties of a JavaScript object as an index when accumulating your accounts:

 var counts = {}; rows.forEach(function(r) { if (!counts[r.Day]) { counts[r.Day] = 0; } counts[r.Day]++; }); 

Then you will need to convert this back to 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 assume that you want to ignore the What column because you were only talking about combining the number of days, and there are different values โ€‹โ€‹of What on the same day. If you want to consider the โ€œWhatโ€ while you are calculating, you can simply create it in the key of your original object counts ( r.Day + r.What ).

EDIT: based on the comment add code with a composite 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]); }); 
+2
source

The function in these lines should do the trick. I cannot check this on the computer at the moment, so let me know if it does not 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; } 
0
source

Source: https://habr.com/ru/post/957132/


All Articles