0

I'm building a dc.js / d3.js dashboard in which I often have to make crossfilter groups containing a quantity for each key and a percentage of the total value for each key. That is why I want to make generic reduceAdd, reduceRemove and reduceInitial functions. I managed doing the first 2, but I don't understand reduceInitial behaviour :

function reduceAdd(dim,grouping,col) {
    var keys = getKeys(dim); // get the keys name in an array of string
    return function(p,v) {
        p.total += parseInt(v[col]); // get the running total
        for (var i = 0; i < keys.length; i++) {
            if(v[grouping] == keys[i]) {p[keys[i]] += +v[col];}
            p[keys[i]+"perc"] = p[keys[i]]/p.total; // calculate a percentage for ech key
        }
        return p;
    }
}

function reduceRemove(dim,grouping,col) {
    var keys = getKeys(dim);
    return function(p,v) {
        p.total -= parseInt(v[col]);
        for (var i = 0; i < keys.length; i++) {
            if(v[grouping] == keys[i]) {p[keys[i]] -= +v[col];}
            p[keys[i]+"perc"] = p[keys[i]]/p.total;
        }
        return p;
    }
}

This is the working non generic function reduceInitFC() { return {total:0, LILOU:0, MARIUS:0,ORIANE:0,LILOUperc:0,MARIUSperc:0,ORIANEperc:0}; }

This is what I tried :

function reduceInit(dim) { 
    var keys = getKeys(dim);
    var initArray= {};
    initArray["total"] = 0;
    for (var i = 0; i < keys.length; i++) {
        initArray[keys[i]] = 0;
        initArray[keys[i]+"perc"] = 0;
    }
    console.log(initArray); // (1)
    console.log({total:0, LILOU:0, MARIUS:0,ORIANE:0,LILOUperc:0,MARIUSperc:0,ORIANEperc:0});
    return function() {
            return initArray; 

    }   
}

The result is : (1) The output gives 0 for all the keys every two iterations and some non zero values for the other iterations When I use this function the resulting values in the group are constant respect with the keys what is not the case in reality and not the case when I hand write the zero values.

If anyone can help, it would be super kind and useful.

Best, Theo

4
  • What is getKeys? Can you post a working example? I suspect that using getKeys like this is causing bugs in reduceAdd and reduceRemove that you aren't realizing are there and that in reduceInit there aren't yet any keys so you don't get any values, but it's impossible to tell without a working example where I can see what getKeys does and exactly when the group is created. Commented Nov 19, 2017 at 18:06
  • Ah, I take it back about getKeys introducing bugs. Though it won't update when new data is added, and that's actually good. If it did update you will have bugs. In any case, still need a working example in order to have much chance of diagnosing the problem. Commented Nov 19, 2017 at 18:09
  • I will maybe add a fiddle but it will take a bit of time, btw here is my getKey function, it's just returning an array containing the all the keys of a dimension :
    – Theo
    Commented Nov 21, 2017 at 18:59
  • function getKeys(dim) { var len = dim.group().all().length; var keys = []; for (var i = 0; i < len; i++) { keys[i] = dim.group().all()[i].key; } return keys; }
    – Theo
    Commented Nov 21, 2017 at 18:59

1 Answer 1

0

I think your reduceInit looks fine, but I don't think this is possible through the reduce. The reduce calculates totals incrementally -- and don't make sense until the group runs through all the rows in the dimension. So the percentages for each key can't be calculated until reduce is finished. (Likewise, while the reduce can be used to calculate averages across a single key's values, they can't be used for averages across all keys because that would be dependent on the reduced total.) (UPDATED, also see this answer's comments)

But since you already have the reduced total and the total for the key / category, you can calculate the percentage in your chart's valueAccessor.

function valueAccessor(d){
  return d.value[keyName] / d.value.total
}
7
  • 1
    Maybe I misspoke, the only problem I have is to generalize the reduceInital. When I use a reduceInitial function that is just returning hardcoded 0 values, the calculation works well :)
    – Theo
    Commented Nov 19, 2017 at 16:18
  • 1
    This is correct - you should calculate averages in the valueAccessor and not in the reducer, mostly for performance reasons. Commented Nov 19, 2017 at 18:07
  • 1
    Oh I didn't know that Ethan, can you explain how it impacts the performance ? Thank you for your help both of you !
    – Theo
    Commented Nov 21, 2017 at 18:57
  • 1
    The reduce functions run for every record that is added and for every record that goes in and out of a filter. The average only needs to be calculated at the time you want to display it, so there is no need to do that calculation repeatedly and it shouldn't go in the reducer. Stick a console.log in your reduceAdd function and you'll see how often it is called. Commented Nov 21, 2017 at 19:14
  • 1
    But is it not happening the same thing in the background when we calculate it in the group function ?
    – Theo
    Commented Nov 21, 2017 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.