D3 CSV, calculations

68 views
Skip to first unread message

Jeremy Jirik

unread,
Apr 5, 2018, 12:06:38 PM4/5/18
to d3-js
I am trying to use the csv loader to populate my d3 graph. Before I can do that, I need to calculate some data from the csv.
The first thing I need to calculate is the difference in duration between two keys ('ticket create date") and ('resolved date') for each entry in the object.
Then I need to get the average.
Here is what I have so far:

d3.csv('RiverTixExample.csv', function(d){
ddates = moment.duration(moment(d['resolved date'], "MM/DD/YYYY HH:mm") - moment(d['ticket create date'], "MM/DD/YYYY HH:mm"));


if (ddates._isValid == true) {mttr = ddates._milliseconds + mttr;
count = count +1 };

mttr = mttr/count;

console.log(mttr);
});

The final mttr number, if the "MTTR = MTTR/count" is not included is correct. However, when I add the line to get the average ("mttr = mttr/count") I get every line divided by the count.
How can I either pass this value outside of the csv function or get the right answer?

Erik Stel

unread,
Apr 5, 2018, 1:51:32 PM4/5/18
to d3-js
Jeremy,

The function you provide as second parameter in the call to d3.csv (the so called row-function, see https://github.com/d3/d3-request#csv) is called for every row in your csv file. For every row it performs some calculations. But in this approach you cannot calculate the average yet, since you do not know how many rows will eventually be read. Only the last row will have the correct amount.

You probably have to split the two tasks (adding duration and calculating the average):

///////////////////////

// Add duration to row
function addDuration(d) {
    d.duration = someCalc(d['resolved date'], d['ticket create date']);
    // Or to keep field-access similar:
    // d['duration'] = someCalc(d['resolved date'], d['ticket create date']);
    // You probably have to do something with: moment.duration(moment(d['resolved date'], "MM/DD/YYYY HH:mm") - moment(d['ticket create date'], "MM/DD/YYYY HH:mm"));

    // Return the row object!
    return d;
}

// Handle the final result consisting of an array of row-objects
function handleData(error, data) {

    // Validate result
    if(error || !data) {
        console.error("Failed to read CSV", error, data);
        return;
    }

    // data contains an array of rows (array of row-objects)

    var mttrTotal = data.reduce(function(sum, row) {
        return sum + row.duration;
    }, 0);

    // Calculate average
    var average = mttrTotal / data.length;

    // Show result and do other interesting stuff with the data
    console.log("The average mttr is " + average);
}

d3.csv('RiverTixExample.csv', addDuration, handleData);

///////////////////////

This is all somewhat pseudo-code. I did not test it, but should hopefully give you some idea where to go.

I did not understand the stuff with 'moment' so left it out and replaced it by a call to someCalc which is fake.

Hope this helps,
Erik
Reply all
Reply to author
Forward
0 new messages