.entries(averagerating.all());
return expenseMetrics;
}
};
This fetches the reduced averagerating group data each time it's called, and nests it.
We have two options for faking the dimension. Since all charts except the data table only use the dimension for filtering, you could just fake your dimension as {} and specify a filterHandler to your chart. (See e.g. [1] for an example.) Or you could fake the appropriate filter function(s) of the dimension.
The second approach is a tiny bit longer but I think it's clearer, so let's do that here .
For the fake dimension, we need to figure out how to filter the books by average rating. Assuming that we're not using dimisbn for anything else, we can create a fake dimension that filters on a continuous range of ratings and applies an item-by-item filter to dimisbn:
var averageRatingDimension = {
filter: function(f) { // #1
if(f === null)
dimisbn.filter(null);
else throw new Error("uh oh don't know what to do here");
},
filterRange: function(r) { // #2
var isbns = {}; // #3
averagerating.all().forEach(function(kv) { #4
if(r[0] <= kv.value.average && kv.value.average < r[1]) { // #5
isbns[kv.key] = true;
}
});
dimisbn.filterFunction(function(d) { // #6
return !!isbns[d.ISBN];
});
}
};
This assumes you're using dc.js >= beta.19, which include Ethan's optimizations to call the more efficient filterRange when filtering a range. (With earlier versions, you'd be forced to use filterHandler.)
It also assumes you're using a continuous/quantitative scale for your chart, which will enable range filtering. I just think that's more appropriate. If you want an ordinal scale, I think you're also back to the filterHandler approach, but otherwise it's pretty similar.
Detailed explanation:
1. We expect dc.js to either call .filter(null) or .filterRange(range) - we reset on .filter(null) and barf if we get anything else.
2. .filterRange() will take an array of [low, high) bounds
3. we'll build a hash which maps ISBNs to boolean
4. we'll loop over all the reduced averages and mark ISBNs if they match the range
5. by convention, the range does not include the high value - this is usually the expected behavior for a brush, but you can adjust to taste
6. replace the dimisbn filter with one that only accepts the ISBNs marked with true
NOTE: this code is untested. I may have made some mistakes.
If it doesn't work or if you need further clarification, please build a jsFiddle with example data [2] and charts so that I can help troubleshoot. (bl.ocks are also welcome.) See [3] for fiddles and blocks you can fork to get started with the right dependencies.
Cheers,
Gordon