最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrieving values in crossfilter.dimension - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Feb 24, 2014 at 16:10 user203617 asked Feb 24, 2014 at 15:37 user203617user203617 5331 gold badge9 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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/

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!

发布评论

评论列表(0)

  1. 暂无评论