-->

Crossfilter filters not filtering (dc.js)

2019-03-06 04:02发布

问题:

I'm trying to use filters in crossfilter, but nope.

Here's the JSBin

Here's the most relevant code:

var userDimension = data.dimension(function(d) {return d.user;}).filter(['John', 'Paul']);
var totalSalesByUser = userDimension.group();
var pieChartUserSales = dc.pieChart("#pie-chart-sales-by-user");
pieChartUserSales
  .width(150).height(150)
  .dimension(userDimension)
  .group(totalSalesByUser); 

dc.renderAll();

The pie chart looks the same with and without the filter, but the effect I'm looking for is narrowing it down to the items where John or Paul is the user.

回答1:

Crossfilter filters don't apply to groups defined on the same dimension as the filter. Create a second dimension and filter on that.

Here's an updated example. Also note that the filter you defined doesn't do what you probably meant it to do, so I changed that to a function that filters to only Paul and John: http://jsbin.com/yugakire/1/edit



回答2:

So it turns out crossfilter doesn't apply the filter to the dimension you put the filter on. It applies it to everything else.

This makes sense after you think about it for a long long time.