crossfilter-计算具有属性的所有记录的百分比

anm*_*oul 3 d3.js crossfilter dc.js

这是我的问题:

我正在使用从mongo db获取json数据的python flask服务器,并在其中指定要导入的字段。此数据为json格式,仅以这种方式获取。一旦通过graphs.js中的交叉过滤器,是否可以对这些字段进行转换?例如,我有一个状态属性,该属性可以采用值“通过”,“进行中”,“保留”或“失败”。我基本上想做一个指标,告诉我失败的百分比。因此,理想情况下,我必须对数据进行一些计算。请对此提供建议。

Sample data (in tabular form for clarity) looks like:
TrialLocation     | Subject Status
Site A            | In progress
Site A            | Pass
Site B            | In progress
Site A            | In progress
Site B            | On Hold
Site A            | Screen Failure
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我应该获得在x轴和y轴上具有站点名称的条形图,我应该获得计算失败百分比的度量。在这种情况下,对于网站A,将为25%;对于网站B,为0%。

因此,我首先创建了图表,该图表为我提供了每个站点的主题计数。

var siteName = ndx.dimension(function(d) { return d["TrialLocation"];});
var numSubjectsBySite = siteName.group();
var siteLevelChart = dc.barChart("#site-level-count", "subjectView");
Run Code Online (Sandbox Code Playgroud)

最后是图表:

siteLevelChart
 .width(2000)
 .height(200)
 .transitionDuration(1000)
 .dimension(siteName)
 .group(numSubjectsBySite)
 .ordering(function(d){return d.value;})
Run Code Online (Sandbox Code Playgroud)

所以我想,我将用SubjectStatus =“ Screen Failure”计算行数,然后将其除以总行数,在这种情况下将是“ numSubjectsBySite”变量。然后,当我引入此代码时:

var countScreenFailures = ndx.dimension(function(d){ return d["SubjectStatus"];});
 countScreenFailures.filter("Off Study");
Run Code Online (Sandbox Code Playgroud)

我的条形图仅显示主题状态为“ ScreenFailure”的行。

如何计算屏幕故障率然后再使用呢?请帮帮我吗?

非常感谢。安莫尔

Eth*_*ett 5

您将需要构建自定义分组/减少功能以跟踪每种状态的计数以及总计数。然后,您可以在图表中除以计算百分比。如果您对使用Reductio感兴趣,则可以执行以下操作:

var reducer = reductio().count(true);

// Do this as many times as you need for different status counts. Each
// call of reducer.value will add a new property to your groups where
// you can store the count for that status.
reducer.value("ScreenFailure").sum(
  function(d) {
    // This counts records with SubjectStatus = "Screen Failure"
    return d["SubjectStatus"] === "Screen Failure" ? 1 : 0;
  });

// Build the group with the Reductio reducers.
var numSubjectsBySite = reducer(siteName.group());

// In your dc.js chart, calculate the % using a value accessor.
siteLevelChart
 .width(2000)
 .height(200)
 .transitionDuration(1000)
 .dimension(siteName)
 .group(numSubjectsBySite)
 .valueAccessor(function(p) { return p.value.ScreenFailure.sum / p.value.count; })
 .ordering(function(d){return d.value;})
Run Code Online (Sandbox Code Playgroud)