awesome
awesome

Reputation: 317

How to have multiple graphs per dimension in dc.js?

Assume the following dataset:

var data = [
    {
        "userId": "u1",
        "questionId": "q1",
        "answerId": "a1"
    },
    ...
];

I would like to plot a list of pie charts. Each pie chart corresponds to one question, with the pies being the answers of that question.

Now, when someone clicks on a pie to select an answer, it will update the remaining pie charts with the users who also selected this answer.

Basically, the use case is "users who answer "a1" to question "q1" also answered to question "q2" with the distribution given by the pies, etc.

I'm not sure what is the most optimal way to proceed. Should I use several instances of crossfilter? Is it doable with only one? How can I implement this filter? It seems the filter applies on the user, but the dimensions are the answers...

Thanks for your help!

Upvotes: 0

Views: 99

Answers (1)

Gordon
Gordon

Reputation: 20120

Often getting an easy answer from crossfilter means structuring your data so it is a flat array of the rows you want to count. Here you want to count users.

If this is your entire visualization, and you can restructure your data, the easiest way to get what you want will be to have a row per user:

[{"userId": "u1",
    "q1": "a1",
    "q2": "a2",
    ...
},
 {"userId": "u2",
    "q1": "a1",
    "q2": "a2", 
    ... 
}]

Now you'll get what you were asking for automatically just by having dimensions keyed on each question, each with the default reduceCount group.

The dimensions will be like this:

var dim = cf.dimension(function(d) { 
    return d.q1; 
});

Upvotes: 1

Related Questions