用分组元素dc.js的百分比汇总数据表

时间:2018-10-18 09:41:01

标签: javascript dc.js crossfilter

如何在数据表中显示失败百分比?我有以下csv,我正在计算每个“名称”字段的失败百分比,我想在数据表中显示每个“名称”的总失败百分比

Customer | year| Week |Failure_Reason|     name        |    Type  |   Count 
___________________________________________________________________________
A         2018  29      D              Express Air        PR         27
___________________________________________________________________________
A         2018  26      M              Express Air        PR         58
___________________________________________________________________________
A         2018  26      D              Domestic                      5
___________________________________________________________________________
A         2018  27      N              Domestic          SPEED       29
___________________________________________________________________________
A         2018  30    Missed           International     PR          11
___________________________________________________________________________
A         2018  32      N              Domestic          PR          53

维度和组的代码:

        var ndx= crossfilter(data);
        var all= ndx.groupAll();  
        var CTypedim=ndx.dimension(function(d) {return d["name"]+ ',' + d["Customer"];})
        var CTypeGroup=CTypedim.group();

        var percentFailures = CTypedim.group().reduce(
        function(p, v) {
             p.counter+= Number(v.count);
             p.failures += Number(v.Failure_Reason != 'N' ? v.count : 0);
             p.failPercent = p.counter ? p.failures/p.counter : 0;
             return p;
         },
         function(p, v) {

             p.counter-= Number(v.count);
             p.failures -= Number(v.Failure_Reason != 'N' ? v.count : 0);
             p.failPercent = p.counter ? p.failures/p.counter : 0;
             return p;
         },
         function() {
             return {
                 counter: 0,
                 failures: 0,
                 failPercent: 0
             };
         });

        dataTable.width(800).height(800)
        .dimension(percentFailures)
        .group(function(d) { return "" })
        .size(100)
        .columns([
            function(d) { return d.Customer; },
            function(d) { return d.year; },
            function(d) { return d.value.failpercent*100; },
            function(d) { return d.name; },

        ])
        .sortBy(function(d){ return d.year; })
        .order(d3.descending);

需要显示汇总的datavtable如下:

Customer | Year| Failure%|     name        |    
_____________________________________________
A         2018      100%     Express Air       
_____________________________________________
A         2018      5.7%       Domestic     
_____________________________________________
A         2018      100%      International       
_____________________________________________

https://wiki.eclipse.org/Configure_Eclipse_for_Java_9

1 个答案:

答案 0 :(得分:2)

您在正确的轨道上,继续前进!

您需要使用表中的汇总字段:

    .columns([
        function(d) { return d.key.split(',')[1]; }, // customer part of multikey
        function(d) { return d.year; },
        function(d) { return d.value.failPercent*100; }, // capitalize consistently
        function(d) { return d.key.split(',')[0]; }, // name part of multikey

    ])

使用年份有问题!因为一旦汇总,您就不会真正知道每个汇总值代表的年份-而且可能代表多个年份。如果您可以删除该列,我推荐它。