问题
I need to display the top20 alarms out of a large dataset.
The standard dc.js works great and using .rowsCap(20) in a rowChart gives me the top20.
I am now trying to remove the zero rows when the data is filtered below 20 entries. Several similar posted questions pointed to the remove_empty_bins() from https://github.com/dc-js/dc.js/wiki/FAQ#filter-the-data-before-its-charted which works correctly if I remove the .rowsCap(20) but fails I combine the two.
Using dc.js-2.0.0-beta.1 it fails on line 3415 for the group.top(_cap) call because the .top attribute is not available for the fake group generated by remove_empty_bins().
Same error when trying to add .top(20) when defining the fake group.
Is there an easy way to combine remove_empty_bins(original_group) with .top() or .rowCaps() for a rowChart ?
--Nico
回答1:
It is just a little bit more complicated to add .top(n)
to the fake group:
function remove_empty_bins(source_group) {
function non_zero_pred(d) {
return d.value != 0;
}
return {
all: function () {
return source_group.all().filter(non_zero_pred);
},
top: function(n) {
return source_group.top(Infinity)
.filter(non_zero_pred)
.slice(0, n);
}
};
}
The efficiency is not perfect, because this fetches all the groups in sorted order and then throws out all but the first n
, while crossfilter is able to only pull the first n
using a heap. But this shouldn't matter unless the number of groups is huge.
Working fork of your fiddle: http://jsfiddle.net/gordonwoodhull/za8ksj45/3/
EDIT: note that the need to provide group.top()
is being eliminated in dc.js 2.1.2, since the functionality overlaps with chart.ordering()
and leads to confusing bugs. (As well as making these data preprocessors difficult to write.)
来源:https://stackoverflow.com/questions/28145149/dc-js-rowchart-topn-without-zeros