0

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

bashhike
  • 129
  • 1
  • 10
  • What does `console.log(fooGroup.post().cap(3)()` show you? – Ethan Jewett Jul 26 '16 at 21:58
  • Also, for this group key, I'd think you'd want to define a dimension on `bar` rather than `foo`, but I assume that's just a mistake based on sample data :-) – Ethan Jewett Jul 26 '16 at 21:59
  • It's not showing me in the correct order. It's capped based on natural order. I would like to post cap the result by decreasing sum values. Yes the key should be 'bar'. That's a mistake. :) – bashhike Jul 27 '16 at 04:46
  • [key: 'A', value:{count=4, sum=150}, key: 'B', value:{count=1, sum=180}, key: 'others', value:{count=7, sum=60}] – bashhike Jul 27 '16 at 04:46
  • You'll need to define an ordering on the group using Crossfilter's `group.order` method. Reductio just uses the same ordering as `group.top`. Something like `bargroup.order(function(p) { return p.sum; })` should work. https://github.com/crossfilter/crossfilter/wiki/API-Reference#group_order – Ethan Jewett Jul 27 '16 at 14:23
  • @EthanJewett I have specified a group order. When I do `console.log(bargroup.top(Infinity))` it shows me in the order of sum. When I do `console.log(bargroup.post().cap(3)())` it shows me natural order. I'm stumped. :( – bashhike Aug 08 '16 at 09:51
  • Sounds like a bug. Please open an issue on GitHub and I'll take a look at it. – Ethan Jewett Aug 08 '16 at 11:29

1 Answers1

1

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.

Ethan Jewett
  • 6,002
  • 16
  • 25
  • Thanks Ethan. This works. I intended to use the post cap because of the 'others' aggregation. Till now I was using fake group in crossfilter which didn't provide ;others'. Probably it would but it would require a bit of coding. Anyway after all this I would want to pass this to dc.js chart. However I get a.group.all is not a function. So I've gone ahead and created a fake group. – bashhike Aug 08 '16 at 14:43
  • Yup, you can create a fake group based on the result of `group.post().sortBy....cap(3)` and it should work. :-) Cheers. – Ethan Jewett Aug 08 '16 at 14:44