Question

jsFiddle of the issue

I'm trying to filter over two dimensions in Crossfilter. I have an array of data that repeats a given object:

{
    "bioguide_id" : "O000167",
    "first_name" : "Barack",
    "last_name" : "Obama",
    "party" : "Democrat",
    "religion" : "United Church of Christ",
    "pac_id" : "C00404145",
    "pac_name" : "Mendocino County Democratic Central Committee",
    "contribution_count" : 4,
    "contribution_sum" : 1264,
    "congress" : 110
}

I have the following query to crossfilter working perfectly:

var contributions = data.dimension(function(c) { return c.contribution_sum; });

contributions.top(3).forEach(function(p, i) {
    console.log(p.pac_name + " (" + p.congress + ")" + ": $" + p.contribution_sum);
});

However, how can I get the top X contribution_sum's from Y congress? I've attempted the following to no avail:

var congress = data.dimension(function(c) { return c.congress; });

congress.filter(111).group().order(function(c) { return c.contribution_sum; }).top(3).forEach(function(p, i) {
    console.log(p.pac_name + " (" + p.congress + ")" + ": $" + p.contribution_sum);
});

Any thoughts on why?

Was it helpful?

Solution

I believe you will want to filter on your 'congress' dimension and then ask for the top members of your 'contribution_sum' dimension like:

var congress = data.dimension(function(c) { return +c.congress; });
var contributions = data.dimension(function(c) { return +c.contribution_sum; });

congress.filter(111);

contributions.top(3).forEach(function(p, i) {
    console.log(p.pac_name + " (" + p.congress + ")" + ": $" + p.contribution_sum);
});

Complete JSFiddle: http://jsfiddle.net/eWr8Z/5/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top