score:5

Accepted answer

You'll need to build custom grouping/reduce functions to track the count of each status as well as the total count. Then you can just divide in the chart to calculate your percentage. If you are interested in using Reductio, you can probably do the following:

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;})

score:3

You can use a custom groupAll for this. Here is a straight crossfilter solution, based on the jsfiddle you provided in a later question.

(It's a lot easier to answer with a fiddle to work with!)

var all = ndx.groupAll();
var failurePercentGroup = all.reduce(
    function(p, v) {
        ++p.count;
        p.failures += (v.Status === 'Screen Failure' ? 1 : 0);
        p.failPercent = p.count ? p.failures/p.count : 0;
        return p;
    },
    function(p, v) {
        --p.count;
        p.failures -= (v.Status === 'Screen Failure' ? 1 : 0);
        p.failPercent = p.count ? p.failures/p.count : 0;
        return p;
    },
    function() {
        return {
            count: 0,
            failures: 0,
            failPercent: 0
        };
    }
);

failurePercent.valueAccessor(function (x) {
    return x.failPercent;
})
    .group(failurePercentGroup);

@Ethan's answer looks like it should work, but you remarked elsewhere that you couldn't get it to work.

Updated fiddle here: http://jsfiddle.net/gordonwoodhull/vct0dzou/8/

I didn't deal with formatting it as a percent, so it just shows a ratio, but you should be able to figure that part out.


Related Query

More Query from same tag