简体   繁体   English

带伪函数的pivot.js摘要

[英]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 而且它不是从Granu聚合而来的

What I would like to do is essentially do a sum(row.spend) / sum(row.purchasers) group by filters instead of a 我想做的基本上是通过过滤器而不是a来进行sum(row.spend)/ sum(row.purchasers)分组

for i in row Σ (row.spend/row.purchasers) , which is what is currently happening. 对于Σ行(row.spend / row.purchasers)中的i,这是当前正在发生的情况。

eg if i have 100 rows with the calculated field spend/purchasers 例如,如果我有100行计算出的字段支出/购买者

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. pseudoFunction属性不支持此功能,因为它仅在加载CSV时逐行运行。

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 编辑:根据其他用户(我没有测试它自己),在库中,字段属性的新版本summaryFunction现在被称为summarizeFunction ,所以如果上述方法不为你工作,尝试改变这种属性的名称

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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