Reputation: 421
Straightforward question, but I haven't seen an answer anywhere. Basically, I want to filter my dataset for NaNs for a metric and then display the top 10 highest groups of that metric. I am using the fake group method, but I can't seem to use both top and filter for a bar chart.
function remove_empty_bins(source_group) {
return {
all:function () {
return source_group.top(10).filter(function(d) {
return ! isNaN(d.value.attendance_rate) ;
});
}
};
}
This code won't work since the top I believe only takes the top 10 sorted by key. If I want to sort by metric I would need to use chart.ordering() but obviously I can't use that because I need to sort before I do .top().
Any ideas?
Upvotes: 2
Views: 117
Reputation: 20120
I take it you are not using one of the charts that supports capping (pie, row). For those you should be able to just filter out the NaNs using a fake group, and then pass the data in.
Since 2.1.2, dc.js has not used group.top()
internally for capped charts. The interaction between top and ordering was too confusing.
I would suggest approaching your problem the same way that those charts were changed. Instead of trying to use group.top()
just do the sorting yourself. Then it's pretty easy to take all, filter, sort, and cap (untested):
function filter_sort_cap_bins(source_group) {
return {
all:function () {
return source_group
.all()
.filter(d => !isNaN(d.value.attendance_rate))
.sort((a,b) => b.value.attendance_rate - a.value.attendance_rate)
.slice(0, 10);
}
};
}
Maybe in theory this is a little slower because it's working with the whole set of keys, but I don't think you'll notice any difference unless you have thousands of keys.
Upvotes: 2