9

Hi I'm a newbie in JS and Crossfilter. I'm using crossfilter with my data (.csv file) and retrieved distinct values in a column using

var scoreDim = ppr.dimension(function (d) {
    return d.score;
});

Also I could get the counts for each value using

var scoreDimGroup = scoreDim.group().reduceCount();

I could use dc.js to plot the chart and the result looks correct. But how do I retrieve the values in scoreDim and scoreDimGroup so that I can use it for further processing in my code. When I look at the object using a debugger, I could see a bunch of functions but could not see the actual values contained in the objects.

enter image description here

user203617
  • 523
  • 8
  • 20

3 Answers3

12
scoreDim.top(Infinity)

will retrieve the records.

scoreDimGroup.top(Infinity)

will retrieve the groups (key-value pairs of the dimension value and the count).

Generally, this kind of thing is covered well in the Crossfilter API documentation.

Ethan Jewett
  • 6,002
  • 16
  • 25
  • 1
    For groups only you can shorten to `.all()` – geotheory Sep 23 '15 at 09:00
  • Be careful that this will not return the filtered dim values. If you apply a filter on another dimension, the list of key for the dimension you get is always the full one with the method above ! – leroyse Mar 30 '16 at 15:11
3

You can use the top method of the group object:

var groupings = teamMemberGroup.top(Infinity);

This returns an array of groups, which will have the structure that you built in the reduce method. For example, to output the key and value you can do this: groupings.forEach(function (x) { console.log(x.key + x.value.projectCount); });

You can access the dimension values in the same way:

var dimData = teamMemberDimension.top(Infinity);
    dimData.forEach(function (x) {
        console.log(JSON.stringify(x));
    });

Here is a simple example of this: http://jsfiddle.net/djmartin_umich/T5v4N/

Rusty has a nice tutorial on how this works at http://blog.rusty.io/2012/09/17/crossfilter-tutorial/

DJ Martin
  • 2,579
  • 20
  • 24
2

If you are looking to view these values in the console then you can use this print_filter function that was mentioned in the tutorial!

(http://www.codeproject.com/Articles/693841/Making-Dashboards-with-Dc-js-Part-1-Using-Crossfil)

Basically you would include this bit of code in your javascript rendering of the crossfilter charts before you define your data source or your ndx variable:

function print_filter(filter) {
    var f = eval(filter);
    if (typeof(f.length) != "undefined") {}else{}
    if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
    if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
    console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
    };

Then you can simply run print_filter(scoreDim) in your console! It's that simple! You can use this to see all of the objects you create using crossfilter including groups, etc.

Hope this helps!

Felix Dasgupta
  • 127
  • 1
  • 2
  • 10