Search code examples
dc.jscrossfilter

How to sum all values for numberDisplay, excluding a category


I have set of data where I want to apply filters by default to a numberDisplay. The data is something like this.

data = [{category:'A',value:10},
{category:'B',value:10},
{category:'C',value:10},
{category:'S',value:10},
{category:'C',value:10},
{category:'A',value:10}]

I am trying to create a number display which will show sum of values other than category 'S', I tried using fake groups but they are failing. What would be the best method to achieve this ?


Solution

  • You don’t need a fake group for this, since you’re not trying to change the shape/structure of the aggregation. Ordinary crossfilter reductions cover this purpose.

    You can simply do

    cf.groupAll().reduceSum(d => d.category === ‘S’ ? 0 : d.value);
    

    This will sum the value of every row included in the current filters, but will substitute zero if the row’s category is S.