Xavier
Xavier

Reputation: 1207

How to draw an aggregated table with dc.js?

I've been bugged by a probably trivial issue, but I want to draw two tables grouped by dimension.

eg:

var data=[{country:"US", medal:"gold",sport:"jump",year:"2015"},
 {country:"US", medal:"silver",sport:"jump",year:"2011"}
 {country:"CN", medal:"gold",sport:"jump",year:"2015"}}

and would like to draw a few tables:

country   medals
US        2
CN        1

and

medal     qty
gold      2
silver    1

so a trivial dim + group on crossfilter, but as dataTable doesn't take "real" groups as groups... bit stuck

Any suggestion?

Upvotes: 1

Views: 106

Answers (1)

Ethan Jewett
Ethan Jewett

Reputation: 6010

Generally speaking, you can use a group as the dimension for dc.dataTable if you just give it a bottom method. You can do something like this:

var cf = crossfilter(["a","a","b","b","c","d","d","d"])
var dim = cf.dimension(function(d) { return d; });
var grp = dim.group()
grp.bottom = grp.top;

var dataTable = dc.dataTable('#dataTable');

dataTable
  .dimension(grp)
  .group(function() { return ""; })
  .size(100)
  .columns(['key', 'value']);

dc.renderAll();

The problem is that you need to put all the necessary information into the group. You can either do this in a custom group, or you can create a fake group wrapping your group where you look up additional information and add it to the group when the dataTable queries the fake group.

Upvotes: 2

Related Questions