Search code examples
crossfilterreductio

Crossfilter reductio post cap


I am having trouble trying to use reductio's post().cap functionality. My dataset is like so.

[{foo: 'one', bar: 'B', hits:10},
{foo: 'one', bar: 'B', hits:20},
{foo: 'two', bar: 'B', hits:50},
{foo: 'two', bar: 'B', hits:100},
{foo: 'one', bar: 'A', hits:150}.........]

What I am looking for is

[key: 'B', value:{count=4, sum=180}, 
key: 'A', value:{count=1, sum=150},
key: 'others', value:{count=7, sum=60}]

I have a foo dim setup as

var barDim = ndx.dimension(function(d){ return d.bar; });
var barGroup = reductio().count(true).sum('hits')(barDim.group());

Thanks in advance!

reductio cap functionality


Solution

  • Unfortunately during the comment thread above I was not familiar enough with the Reductio post API as I don't use it myself. It doesn't currently respect group ordering, but it does provide its own API to control order. For example:

    group.post().sortBy('value.sum', d3.descending).cap(3)()
    

    Note that the ordering function here is d3.descending, which is available if you are using D3.js. Otherwise you can use any ordering function with a similar API.

    I also note that the sortBy API isn't documented. I will try to get this done so that others can discover it.