Retrieving values in crossfilter.dimension
Asked Answered
C

3

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

Cautionary answered 24/2, 2014 at 15:37 Comment(0)
F
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.

Fecundate answered 24/2, 2014 at 16:29 Comment(2)
For groups only you can shorten to .all()Ski
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 !Sobriquet
D
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/

Dola answered 24/2, 2014 at 16:27 Comment(0)
H
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!

Hissing answered 10/2, 2015 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.