display the number of distinct items with data count widget

醉酒当歌 提交于 2019-12-25 04:12:37

问题


I have a list of items, some of them have several rows for the same item (different variants of the same item).

I want to count how many items exists in total, and how many are selected. however, when I'm using the data count widget, it only works with the number of rows by default, but can't figure out how to change that behaviour.

var data=[{"item":"Category A","color":"blue"},
   {"item":"Category B","color":"blue"},
   {"item":"Category A","color":"pink"}];


var ndx           = crossfilter(data);

var dimension  = ndx.dimension(function(d) {return d.item;});
var all = ndx.groupAll();


 dc.dataCount('.dc-data-count')
    .dimension(ndx)
    .group(all)
    .html ({some: "%filter-count selected out of %total-count items <a class='btn btn-default btn-xs' href='javascript:dc.filterAll(); dc.renderAll();'  role='button'>Reset filters</a>", all: "%total-count items. Click on the graphs to filter them"})

According to dc doc, the only dimension possible is the entire data set and the only group is the one returned by dimension.groupAll()

Is there a workaround to count something else than the number of records (eg. ndx.dimension(function(d) {return d.item;});?


回答1:


Well, it's pretty ugly but you could easily hack it to produce other behavior.

If you look at the code, it's only calling .size() on the dimension, and only .value() on the group. That's why the documents say that only a groupAll is appropriate; regular groups don't have .value().

https://github.com/dc-js/dc.js/blob/develop/src/data-count.js#L93-94

But it calls no other methods on the dimension or group, so you can fake this pretty easily:

chart.dimension({size: function() { 
    return 42; // calculate something here
} });

It might be somewhat more appropriate to use two number display widgets instead, but this should work.



来源:https://stackoverflow.com/questions/27789872/display-the-number-of-distinct-items-with-data-count-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!