modify dc-count widget dimension/group in dc.js

177 views Asked by At

I'm trying to add a dc-count-widget but I need to get around the default dimension/ group being all data/groupAll. My data looks like this:

`[{"subjId":"subj1", "temp":"37"},
  {"subjId":"subj1", "temp":"38"},
  {"subjId":"subj2", "temp":"36.5"},
  {"subjId":"subj2", "temp":"37.3"},
  {"subjId":"subj3", "temp":"39"}
]`

I want to add a dimension on temp "temperature" and a dc-count to reflect the count of unique subjects when I filter on ranges in temp.

EDIT I tried:

subjectColumnName = 'subjId'
subjectDim = cfData.dimension(function(d) {return d[subjectColumnName]})
chart.dimension(subjectDim.group())
chart.group(subjectDim.groupAll())

but when filtering I get the duplicates count not unique counts of subjects.

Thanks

1

There are 1 answers

1
bernam On

Thanks @Gordon The solution in dataCount graph filtered by a dimension would work, but the widget would not update if other charts are filtered so I had to create a custom group function to only count unique subjects.

I am not sure yet if the reduceRemoveSubj function is correct. And it looks like it's a very detoured solution!

Here it is:

function reduceAddSubj(p, v) {

            if( v[subjectColumnName] in p.subjects){
                p.subjects[v[subjectColumnName]]++
            }
            else {
                p.subjects[v[subjectColumnName]] = 1;
                ++p.count;
            }
            return p;
        }

function reduceRemoveSubj(p, v) {
            p.subjects[v[subjectColumnName]]--;
            if(p.subjects[v[subjectColumnName]] === 0){
                delete p.subjects[v[subjectColumnName]];
                --p.count;
            }
            return p;
        }

function initialSubj() {
            return {subjects: {},
                    count:0
            };
        }

subjectColumnName = 'subjId'
subjectDim = cfData.dimension(function(d) {return d[subjectColumnName]})
uniqueSubjGrp = subjectDim.groupAll().reduce(reduceAddSubj, reduceRemoveSubj, initialSubj)
uniqueSubjGrpM = {value: function() {
                return uniqueSubjGrp.value().count; 
            } };

chart.dimension(subjectDim.group())
chart.group(uniqueSubjGrpM)