dc.js中的故障百分比

时间:2018-08-16 11:30:40

标签: dc.js crossfilter

如何计算故障百分比,即发生故障的次数/总计数??

Customer | Year| Week |Failure     |       name        |    Type  |   Count 
___________________________________________________________________________
A         2018  29       N              Express Air       PASSENGER      27
___________________________________________________________________________
A         2018  26       N              Express Air       PASSENGER      58
___________________________________________________________________________
A         2018  26   Delay hours        Express Air                       5
___________________________________________________________________________
A         2018  27       N              Express Air       HIGH SPEED     29
___________________________________________________________________________
A         2018  30  Maintenance missed  Express Air       PASSENGER      11
___________________________________________________________________________
A         2018  32      N               Express Air       PASSENGER      53
___________________________________________________________________________
A         2018  25      N               Express Air                       2
___________________________________________________________________________
A        2018   33      N               Express Air       HIGH SPEED      3
____________________________________________________________________________
A        2018   31  Bogey Missing       Express Air       PASSENGER       1

这是我使用dc.js和crossfilter创建的数据表

        var ndx= crossfilter(data);
        var all= ndx.groupAll();  
        var countType = ndx.dimension(function (d) { return d["count"]; }); 
        dataTable.width(800).height(800)
        .dimension(countType)
        .group(function(d) { return "" })
        .size(100)
        .columns([
            function(d) { return d.alias; },
            function(d) { return d.y; },
            function(d) { return d.w; },
            function(d) { return d.failure_reason; },
            function(d) { return d.name; },
            function(d) { return d.train_type; },
            function(d) { return d.count; }
        ])
        .sortBy(function(d){ return d.count; })
        // (optional) sort order, :default ascending
        .order(d3.descending);

编辑:只需很小的更改,总金额就可以使用

         var total = countType.groupAll().reduceSum(function(d) {return 
         d["count"];})
         dc.numberDisplay("#total")
         .valueAccessor(function(d){return d})
         .group(total);

用于计算未发生故障的百分比(N) 在这里,我可以计算出failure_type为'N'的位置,如何计算除N以外的所有其他类型的故障的百分比

         var failureGroup= failureType.group().reduceSum(function(d) {return 
         d.count;});
         var failureType = ndx.dimension(function(d) { return 
         d["failure_reason"]; });
         function choose_bin(group, key) {
          return {
            value: function() {
              return group.all().filter(kv => kv.key === key)[0].value;
            }
          }
        }  
        var allCount = failureType.groupAll().reduceSum(d => d.count);
        var nofailure = choose_bin(failureGroup, 'N');

        dc.numberDisplay("#total1")
        .group(nofailure)
        .valueAccessor(x => (x / allCount.value())*100); 

1 个答案:

答案 0 :(得分:1)

我认为您可能可以像显示的那样使它与组和箱一起使用,但是再次使用groupAll会更简单:

    var allCount = failureType.groupAll().reduceSum(d => d.count);
    var nofailure = failureType.groupAll()
        .reduceSum(d => d.failure_reason === 'N' ? d.count : 0);
    dc.numberDisplay("#total1")
        .group(nofailure)
        .valueAccessor(x => (x / allCount.value())*100);