Skip to content

Commit ce2ffd3

Browse files
committed
Add min/max grouping
1 parent 9246547 commit ce2ffd3

8 files changed

+322
-12
lines changed

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ gulp.task('watch', function() {
2222
});
2323

2424
gulp.task('default', ['scripts', 'watch']);
25-
gulp.task('all', ['scripts', 'html']);
25+
gulp.task('all', ['scripts']);

reductio.js

+110-8
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ var reductio_histogram = {
171171

172172
module.exports = reductio_histogram;
173173
},{}],6:[function(_dereq_,module,exports){
174+
var reductio_max = {
175+
add: function (prior) {
176+
return function (p, v) {
177+
if(prior) prior(p, v);
178+
179+
p.max = p.valueList[p.valueList.length - 1];
180+
181+
return p;
182+
};
183+
},
184+
remove: function (prior) {
185+
return function (p, v) {
186+
if(prior) prior(p, v);
187+
188+
// Check for undefined.
189+
if(p.valueList.length === 0) {
190+
p.max = undefined;
191+
return p;
192+
}
193+
194+
p.max = p.valueList[p.valueList.length - 1];
195+
196+
return p;
197+
};
198+
},
199+
initial: function (prior) {
200+
return function (p) {
201+
p = prior(p);
202+
p.max = undefined;
203+
return p;
204+
};
205+
}
206+
};
207+
208+
module.exports = reductio_max;
209+
},{}],7:[function(_dereq_,module,exports){
174210
var reductio_median = {
175211
add: function (prior) {
176212
var half;
@@ -220,11 +256,49 @@ var reductio_median = {
220256
};
221257

222258
module.exports = reductio_median;
223-
},{}],7:[function(_dereq_,module,exports){
259+
},{}],8:[function(_dereq_,module,exports){
260+
var reductio_min = {
261+
add: function (prior) {
262+
return function (p, v) {
263+
if(prior) prior(p, v);
264+
265+
p.min = p.valueList[0];
266+
267+
return p;
268+
};
269+
},
270+
remove: function (prior) {
271+
return function (p, v) {
272+
if(prior) prior(p, v);
273+
274+
// Check for undefined.
275+
if(p.valueList.length === 0) {
276+
p.min = undefined;
277+
return p;
278+
}
279+
280+
p.min = p.valueList[0];
281+
282+
return p;
283+
};
284+
},
285+
initial: function (prior) {
286+
return function (p) {
287+
p = prior(p);
288+
p.min = undefined;
289+
return p;
290+
};
291+
}
292+
};
293+
294+
module.exports = reductio_min;
295+
},{}],9:[function(_dereq_,module,exports){
224296
reductio_count = _dereq_('./count.js');
225297
reductio_sum = _dereq_('./sum.js');
226298
reductio_avg = _dereq_('./avg.js');
227299
reductio_median = _dereq_('./median.js');
300+
reductio_min = _dereq_('./min.js');
301+
reductio_max = _dereq_('./max.js');
228302
reductio_value_count = _dereq_('./value-count.js');
229303
reductio_value_list = _dereq_('./value-list.js');
230304
reductio_exception_count = _dereq_('./exception-count.js');
@@ -233,7 +307,7 @@ reductio_histogram = _dereq_('./histogram.js');
233307

234308
function reductio() {
235309
var order, avg, count, sum, exceptionAccessor, exceptionCount,
236-
exceptionSum, valueList, median, histogramValue,
310+
exceptionSum, valueList, median, histogramValue, min, max,
237311
histogramThresholds,
238312
reduceAdd, reduceRemove, reduceInitial;
239313

@@ -300,7 +374,7 @@ function reductio() {
300374
}
301375

302376
// Maintain the values array.
303-
if(valueList || median) {
377+
if(valueList || median || min || max) {
304378
reduceAdd = reductio_value_list.add(valueList, reduceAdd);
305379
reduceRemove = reductio_value_list.remove(valueList, reduceRemove);
306380
reduceInitial = reductio_value_list.initial(reduceInitial);
@@ -312,6 +386,18 @@ function reductio() {
312386
reduceInitial = reductio_median.initial(reduceInitial);
313387
}
314388

389+
if(min) {
390+
reduceAdd = reductio_min.add(reduceAdd);
391+
reduceRemove = reductio_min.remove(reduceRemove);
392+
reduceInitial = reductio_min.initial(reduceInitial);
393+
}
394+
395+
if(max) {
396+
reduceAdd = reductio_max.add(reduceAdd);
397+
reduceRemove = reductio_max.remove(reduceRemove);
398+
reduceInitial = reductio_max.initial(reduceInitial);
399+
}
400+
315401
// Maintain the values count array.
316402
if(exceptionAccessor) {
317403
reduceAdd = reductio_value_count.add(exceptionAccessor, reduceAdd);
@@ -379,6 +465,22 @@ function reductio() {
379465
return my;
380466
};
381467

468+
my.min = function(value) {
469+
if (!arguments.length) return min;
470+
if(valueList) console.warn('VALUELIST accessor is being overwritten by min aggregation');
471+
valueList = value;
472+
min = value;
473+
return my;
474+
};
475+
476+
my.max = function(value) {
477+
if (!arguments.length) return max;
478+
if(valueList) console.warn('VALUELIST accessor is being overwritten by max aggregation');
479+
valueList = value;
480+
max = value;
481+
return my;
482+
};
483+
382484
my.exceptionCount = function(value) {
383485
if (!arguments.length) return exceptionCount;
384486
if( typeof value === 'function' ) {
@@ -413,7 +515,7 @@ function reductio() {
413515
}
414516

415517
module.exports = reductio;
416-
},{"./avg.js":1,"./count.js":2,"./exception-count.js":3,"./exception-sum.js":4,"./histogram.js":5,"./median.js":6,"./sum.js":8,"./value-count.js":9,"./value-list.js":10}],8:[function(_dereq_,module,exports){
518+
},{"./avg.js":1,"./count.js":2,"./exception-count.js":3,"./exception-sum.js":4,"./histogram.js":5,"./max.js":6,"./median.js":7,"./min.js":8,"./sum.js":10,"./value-count.js":11,"./value-list.js":12}],10:[function(_dereq_,module,exports){
417519
var reductio_sum = {
418520
add: function (a, prior) {
419521
return function (p, v) {
@@ -439,7 +541,7 @@ var reductio_sum = {
439541
};
440542

441543
module.exports = reductio_sum;
442-
},{}],9:[function(_dereq_,module,exports){
544+
},{}],11:[function(_dereq_,module,exports){
443545
var reductio_value_count = {
444546
add: function (a, prior) {
445547
var i, curr;
@@ -480,7 +582,7 @@ var reductio_value_count = {
480582
};
481583

482584
module.exports = reductio_value_count;
483-
},{}],10:[function(_dereq_,module,exports){
585+
},{}],12:[function(_dereq_,module,exports){
484586
var reductio_value_list = {
485587
add: function (a, prior) {
486588
var i;
@@ -514,6 +616,6 @@ var reductio_value_list = {
514616
};
515617

516618
module.exports = reductio_value_list;
517-
},{}]},{},[7])
518-
(7)
619+
},{}]},{},[9])
620+
(9)
519621
});

reductio.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/max.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var reductio_max = {
2+
add: function (prior) {
3+
return function (p, v) {
4+
if(prior) prior(p, v);
5+
6+
p.max = p.valueList[p.valueList.length - 1];
7+
8+
return p;
9+
};
10+
},
11+
remove: function (prior) {
12+
return function (p, v) {
13+
if(prior) prior(p, v);
14+
15+
// Check for undefined.
16+
if(p.valueList.length === 0) {
17+
p.max = undefined;
18+
return p;
19+
}
20+
21+
p.max = p.valueList[p.valueList.length - 1];
22+
23+
return p;
24+
};
25+
},
26+
initial: function (prior) {
27+
return function (p) {
28+
p = prior(p);
29+
p.max = undefined;
30+
return p;
31+
};
32+
}
33+
};
34+
35+
module.exports = reductio_max;

src/min.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var reductio_min = {
2+
add: function (prior) {
3+
return function (p, v) {
4+
if(prior) prior(p, v);
5+
6+
p.min = p.valueList[0];
7+
8+
return p;
9+
};
10+
},
11+
remove: function (prior) {
12+
return function (p, v) {
13+
if(prior) prior(p, v);
14+
15+
// Check for undefined.
16+
if(p.valueList.length === 0) {
17+
p.min = undefined;
18+
return p;
19+
}
20+
21+
p.min = p.valueList[0];
22+
23+
return p;
24+
};
25+
},
26+
initial: function (prior) {
27+
return function (p) {
28+
p = prior(p);
29+
p.min = undefined;
30+
return p;
31+
};
32+
}
33+
};
34+
35+
module.exports = reductio_min;

src/reductio.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ reductio_count = require('./count.js');
22
reductio_sum = require('./sum.js');
33
reductio_avg = require('./avg.js');
44
reductio_median = require('./median.js');
5+
reductio_min = require('./min.js');
6+
reductio_max = require('./max.js');
57
reductio_value_count = require('./value-count.js');
68
reductio_value_list = require('./value-list.js');
79
reductio_exception_count = require('./exception-count.js');
@@ -10,7 +12,7 @@ reductio_histogram = require('./histogram.js');
1012

1113
function reductio() {
1214
var order, avg, count, sum, exceptionAccessor, exceptionCount,
13-
exceptionSum, valueList, median, histogramValue,
15+
exceptionSum, valueList, median, histogramValue, min, max,
1416
histogramThresholds,
1517
reduceAdd, reduceRemove, reduceInitial;
1618

@@ -77,7 +79,7 @@ function reductio() {
7779
}
7880

7981
// Maintain the values array.
80-
if(valueList || median) {
82+
if(valueList || median || min || max) {
8183
reduceAdd = reductio_value_list.add(valueList, reduceAdd);
8284
reduceRemove = reductio_value_list.remove(valueList, reduceRemove);
8385
reduceInitial = reductio_value_list.initial(reduceInitial);
@@ -89,6 +91,18 @@ function reductio() {
8991
reduceInitial = reductio_median.initial(reduceInitial);
9092
}
9193

94+
if(min) {
95+
reduceAdd = reductio_min.add(reduceAdd);
96+
reduceRemove = reductio_min.remove(reduceRemove);
97+
reduceInitial = reductio_min.initial(reduceInitial);
98+
}
99+
100+
if(max) {
101+
reduceAdd = reductio_max.add(reduceAdd);
102+
reduceRemove = reductio_max.remove(reduceRemove);
103+
reduceInitial = reductio_max.initial(reduceInitial);
104+
}
105+
92106
// Maintain the values count array.
93107
if(exceptionAccessor) {
94108
reduceAdd = reductio_value_count.add(exceptionAccessor, reduceAdd);
@@ -156,6 +170,22 @@ function reductio() {
156170
return my;
157171
};
158172

173+
my.min = function(value) {
174+
if (!arguments.length) return min;
175+
if(valueList) console.warn('VALUELIST accessor is being overwritten by min aggregation');
176+
valueList = value;
177+
min = value;
178+
return my;
179+
};
180+
181+
my.max = function(value) {
182+
if (!arguments.length) return max;
183+
if(valueList) console.warn('VALUELIST accessor is being overwritten by max aggregation');
184+
valueList = value;
185+
max = value;
186+
return my;
187+
};
188+
159189
my.exceptionCount = function(value) {
160190
if (!arguments.length) return exceptionCount;
161191
if( typeof value === 'function' ) {

test/max.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Counting tests
2+
describe('Reductio maximum', function () {
3+
4+
var max = {}, filterDim;
5+
6+
beforeEach(function () {
7+
var data = crossfilter([
8+
{ foo: 'one', bar: 1 },
9+
{ foo: 'two', bar: 2 },
10+
{ foo: 'three', bar: 3 },
11+
{ foo: 'one', bar: 4 },
12+
{ foo: 'one', bar: 5 },
13+
{ foo: 'two', bar: 6 },
14+
]);
15+
16+
var dim = data.dimension(function(d) { return d.foo; });
17+
var group = dim.group();
18+
19+
filterDim = data.dimension(function(d) { return d.bar; });
20+
21+
reductio()
22+
.max(function(d) { return d.bar; })(group);
23+
24+
max = group;
25+
});
26+
27+
it('has three groups', function (topic) {
28+
expect(max.top(Infinity).length).toEqual(3);
29+
});
30+
31+
it('grouping have the right maximums', function (topic) {
32+
var values = {};
33+
max.top(Infinity).forEach(function (d) {
34+
values[d.key] = d.value;
35+
});
36+
37+
expect(Math.round(values['one'].max)).toEqual(Math.round(5));
38+
expect(Math.round(values['two'].max)).toEqual(Math.round(6));
39+
expect(Math.round(values['three'].max)).toEqual(Math.round(3));
40+
41+
filterDim.filter(1);
42+
43+
expect(Math.round(values['one'].max)).toEqual(Math.round(1));
44+
expect(values['two'].max).toBeUndefined();
45+
expect(values['three'].max).toBeUndefined();
46+
47+
filterDim.filterAll();
48+
49+
expect(Math.round(values['one'].max)).toEqual(Math.round(5));
50+
expect(Math.round(values['two'].max)).toEqual(Math.round(6));
51+
expect(Math.round(values['three'].max)).toEqual(Math.round(3));
52+
53+
});
54+
});

0 commit comments

Comments
 (0)