-->

dataCount graph filtered by a dimension

2019-02-19 08:11发布

问题:

I have a list of participants to various events as the data source

eventid,participant_name
42,xavier
42,gordon
11,john

... by default, dataCount will say they are 3 participants, I need to display the number of events (so 2)

I tried creating a dimension

var event = ndx.dimension(function(d) {
  return d.eventid;
})

but can't manage to use it in dataCount

dc.dataCount(".dc-data-count")
  //.dimension(ndx) //working, but counts participants
  .dimension(event) // not working

How do I do that?

回答1:

It sounds to me like you are trying to use the data count widget to count group bins rather than rows.

The data count widget is only designed to count records, not keys or groups or anything else. But you could fake out the objects, since the widget is calling just .size() on the dimension, and just .value() on the group.

But what to put there? The value is actually sort of easy, since it's the count of groups with non-zero value:

var eventGroup = event.group();
widget.group({value: function() {
    return eventGroup.all().filter(function(kv) { return kv.value>0; }).length;
})

But what is the size? Well, according to the crossfilter documentation, group.size actually returns what we want, "the number of distinct values in the group, independent of any filters; the cardinality."

So oddly, it seems like

widget.dimension(eventGroup)

should work. Of course, I haven't tested any of this, so please comment if this doesn't work!

(Sigh, what I wouldn't do for a real data model in dc.js. It is rather confusing that the "dimension" for this widget is actually the crossfilter object. Another place where there is kind of a weird economy of methods, like the dimension's group, which is just a function.)



标签: dc.js