Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple values from a crossfilter dimension for a pie chart

I'm working with dc.js to display a pie chart. All of the examples I've seen with pie charts in them contain crossfilter dimensions returning a single value

var quarter = ndx.dimension(function (d) {
                var month = d.dd.getMonth();
                if (month <= 3)
                    return "Q1";
                else if (month > 3 && month <= 5)
                    return "Q2";
                else if (month > 5 && month <= 7)
                    return "Q3";
                else
                    return "Q4";
            });
            var quarterGroup = quarter.group().reduceSum(function (d) {
                return d.volume;
            });

The resulting pie chart will contain the 4 possible values Q1,Q2,Q3 and Q4.

I need a pie chart that displays a dimension that returns multiple values. For example a user can an array of subjects within an attribute. In the pie chart I need to show all possible subjects so this means I need to return all the subjects within the array.

i.e Something like :

var subjects = ndx.dimension(function (d) {
           return d.subjectArray
        });
       var subjectGroup = subjects.group()

but this will not work as it will split up the pie chart to display all the different types of arraylist rather then by their contents.

I've been working on this probllem for a while now and can't find a solution. Is it even possible with dc.js and crossfilter to do this?

like image 286
Travis Avatar asked Oct 21 '22 08:10

Travis


2 Answers

I know this question is seven years old. I've been doing a quick review of the most popular unanswered crossfilter questions and this is number 4.

Tag dimensions, or Dimensions with Arrays, was implemented in Crossfilter 1.4.0 with bug fixes in subsequent versions.

When declaring your dimension, pass true for the optional second parameter, and let the dimension key function return an array (as shown in the question). Each record will be counted once for each of its keys.

Warning: since rows are counted more than once, don't use this kind of dimension anywhere (like a pie chart) where the values are expected to add up to 100%.

like image 186
Gordon Avatar answered Oct 27 '22 11:10

Gordon


Not an expert, but I think you need to use .reduce() in the group and create your own reduceAdd, reduceRemove and reduceInitial functions.

.groupAll().reduce(reduceAdd, reduceRemove, reduceInitial).value();

Like explained here except it's for a pie chart, not a barchart.

like image 43
stallingOne Avatar answered Oct 27 '22 11:10

stallingOne