rwjblue / pivot.js

Build Pivot Tables from CSV/JSON Data
http://rwjblue.github.com/pivot.js/
Other
783 stars 134 forks source link

View results as cumulative in the pivot table #5

Closed AJNorman closed 11 years ago

AJNorman commented 11 years ago

Hi,

Has anyone attempted to create a function or code to view the results as cumulative?

In excel, it is still the sum of the field but shows values as running total based on the base fields.

Cheers,

AJ

AJNorman commented 11 years ago

I found a solution:

In pivot.js, add in this function;

cumtotal = 0;

function defaultSummarizeFunctionCumSum(rows, field) {

    var runningTotal = 0,
    i = -1,
    m = rows.length;
    while (++i < m) {
        runningTotal += rows[i][field.dataSource];
    };

    cumtotal += runningTotal;
    return cumtotal;
};

in main code when initialising variables:

{ name: 'cumnet', type: 'float', rowLabelable: false, pseudo: true, dataSource: 'net', summarizable: 'cumsum', displayFunction: function (value) { return accounting.formatMoney(value) } }

AJNorman commented 11 years ago

also need to amend pivot.js;

function processSummaryResults() { var i = 0; for (var resultKey in results) { if (i == 0) { cumtotal = 0; } getSummaryResults(results[resultKey]) i++; };

    return results;
};