Skip to content

Commit

Permalink
simplify comment and code
Browse files Browse the repository at this point in the history
the comment was mostly describing invariants for replacing group.top()
with group.all() + computeOrderedGroups + slice (9d07086)

slice's second parameter has a reasonable default
for dc-js#1184
  • Loading branch information
gordonwoodhull committed Jan 13, 2017
1 parent 947c381 commit 900d148
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions src/cap-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,17 @@ dc.capMixin = function (_chart) {
return _chart.valueAccessor()(d, i);
};

/**
* Base function orders by crossfilter's group.all.
* This returns all groups, in ascending order, by value.
*
* This function is expected to return items in the same direction,
* ascending but the N biggest where N is cap.
*
* Since computeOrderedGroups is using crossfilter's quicksort, the
* sorted items will always be in ascending order.
*
* This means if we cap, we need to take from N to the end of the array.
*
* Index
* 0-------N-------length
*
* Value
* small -> big
*
*/
// return N biggest groups, where N is the cap, sorted in ascending order.
_chart.data(function (group) {
if (_cap === Infinity) {
return _chart._computeOrderedGroups(group.all());
} else {
var topRows = group.all(); // ordered by crossfilter group order (default value)

topRows = _chart._computeOrderedGroups(topRows); // re-order using ordering (default key)
var topRows = group.all(); // in key order
topRows = _chart._computeOrderedGroups(topRows); // re-order using ordering (defaults to key)

if (_cap) {
var start = Math.max(0, topRows.length - _cap);
topRows = topRows.slice(start, topRows.length);
topRows = topRows.slice(start);
}

if (_othersGrouper) {
Expand Down

0 comments on commit 900d148

Please sign in to comment.