问题描述
如何在数据表中显示失败百分比?我有以下csv,我正在计算每个名称字段的失败百分比,我想在数据表中显示每个名称的总失败百分比
How to display failure % in the data table? I have the following csv and I am calculating the percentage of failures for each "name" field , I want to display the total failure percentage for each "name" in datatable
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);
需要显示汇总数据,如下所示:
Need to display aggregated datavtable as follows:
Customer | Year| Failure%| name |
_____________________________________________
A 2018 100% Express Air
_____________________________________________
A 2018 5.7% Domestic
_____________________________________________
A 2018 100% International
_____________________________________________
推荐答案
您在正确的轨道上,继续前进!
You're on the right track, keep going!
您需要使用表中的汇总字段:
You'll need to use the aggregated fields in your table:
.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
])
使用年份有问题!因为一旦进行汇总,您就不会真正知道每个汇总值代表的年份-可能代表多个年份。如果您可以删除该列,我推荐它。
Using the year is problematic! Because once you aggregate, you don't really know what year each aggregated value represents - and it may represent multiple years. If you can drop that column, I recommend it.
这篇关于用分组元素dc.js的百分比汇总数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!