简体   繁体   中英

pivot.js summary with pseudofunctions

I've been having some trouble though with a pseudofunction of a summary field with division of two fields.

the original two fields are:

{name: 'purchasers',    type: 'integer',  rowLabelable: false, summarizable: 'sum', displayFunction: function(value){ return accounting.formatNumber(value)}},    
{name: 'spend',    type: 'float',  rowLabelable: false, summarizable: 'sum', displayFunction: function(value){ return accounting.formatMoney(value)}}

with the pseudofunction being:

{
    name: 'spendperpurch', type: 'float', pseudo: true,      
    pseudoFunction: function(row){ return row.spend / row.purchasers },       
    summarizable: 'sum', displayFunction: function(value){ return accounting.formatMoney(value)}    
}

and it's just not aggregating right from the granu

What I would like to do is essentially do a sum(row.spend) / sum(row.purchasers) group by filters instead of a

for i in row Σ (row.spend/row.purchasers) , which is what is currently happening.

eg if i have 100 rows with the calculated field spend/purchasers

date, purchasers, spend, spendperpurch
1   , 10        , 100  , 10
2   , 15        , 200  , 13.3

if in my table i only want

purchasers, spend, spendperpurch

the current code gives me:

purchasers, spend, spendperpurch
25        , 300  , 23.3

where what i really want is:

purchasers, spend, spendperpurch
25        , 300  , 12

any ideas? I would greatly appreciate any help!

I researched this since I recently needed to do the same.

The pseudoFunction property does not support this, as it only runs on a CSV-row-by-row basis as the CSV is loaded.

Instead, you want to use a custom summary function. Try this example:

var fields=[
    {name:"purchasers", type:"float", summarizable:"sum"},
    {name:"spend", type:"float" summarizable:"sum"},
    {name:"spendperpurch", type:"float",
     summarizable:true,
     summaryFunction:function(rows,field){
        var totalPurchasers=0, totalSpend=0, i=0;
        for(i=0;i<rows.length;i++){
            totalPurchasers+=rows[i]["purchasers"];
            totalSpend+=rows[i]["spend"];
        }
        if(totalPurchasers==0){return 0; /*or whatever you want*/}
        return totalSpend/totalPurchasers;
     }
    ];

EDIT: According to another user (I've not tested it myself), in a newer version of the library, the field property summaryFunction is now called summarizeFunction , so if the above does not work for you, try changing the name of that property

{
  name: 'spendperpurch', type: 'float', pseudo: true, summarizable: 'avg',      
  pseudoFunction: function(row)
  { 
     return row.spend / row.purchasers 
  },       
  displayFunction: function(value)
  { 
     return accounting.formatMoney(value)
  }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM