Skip to content

Commit a7bca01

Browse files
simplify others grouper
by passing in rest of items, something we did not know before this is a consequence of #1269 should be backward-compatible for anyone using a custom others grouper, since it's just extra information being passed in this makes the others grouper self-documenting, so we don't need the bogus and vague custom others grouper example
1 parent 3d228f9 commit a7bca01

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

src/cap-mixin.js

+27-46
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@ dc.capMixin = function (_chart) {
1616
var _cap = Infinity, _takeFront = true;
1717
var _othersLabel = 'Others';
1818

19-
var _othersGrouper = function (topRows) {
20-
var topRowsSum = d3.sum(topRows, _chart.valueAccessor()),
21-
allRows = _chart.group().all(),
22-
allRowsSum = d3.sum(allRows, _chart.valueAccessor()),
23-
topKeys = topRows.map(_chart.keyAccessor()),
24-
allKeys = allRows.map(_chart.keyAccessor()),
25-
topSet = d3.set(topKeys),
26-
others = allKeys.filter(function (d) {return !topSet.has(d);});
27-
if (allRowsSum > topRowsSum) {
28-
return topRows.concat([{
29-
'others': others,
19+
var _othersGrouper = function (topItems, restItems) {
20+
var restItemsSum = d3.sum(restItems, _chart.valueAccessor()),
21+
restKeys = restItems.map(_chart.keyAccessor());
22+
if (restItemsSum > 0) {
23+
return topItems.concat([{
24+
'others': restKeys,
3025
'key': _chart.othersLabel(),
31-
'value': allRowsSum - topRowsSum
26+
'value': restItemsSum
3227
}]);
3328
}
34-
return topRows;
29+
return topItems;
3530
};
3631

3732
_chart.cappedKeyAccessor = function (d, i) {
@@ -54,20 +49,22 @@ dc.capMixin = function (_chart) {
5449
if (_cap === Infinity) {
5550
return _chart._computeOrderedGroups(group.all());
5651
} else {
57-
var items = group.all();
52+
var items = group.all(), rest;
5853
items = _chart._computeOrderedGroups(items); // sort by baseMixin.ordering
5954

6055
if (_cap) {
6156
if (_takeFront) {
57+
rest = items.slice(_cap);
6258
items = items.slice(0, _cap);
6359
} else {
6460
var start = Math.max(0, items.length - _cap);
61+
rest = items.slice(0, start);
6562
items = items.slice(start);
6663
}
6764
}
6865

6966
if (_othersGrouper) {
70-
return _othersGrouper(items);
67+
return _othersGrouper(items, rest);
7168
}
7269
return items;
7370
}
@@ -146,44 +143,28 @@ dc.capMixin = function (_chart) {
146143

147144
/**
148145
* Get or set the grouper function that will perform the insertion of data for the *Others* slice
149-
* if the slices cap is specified. If set to a falsy value, no others will be added. By default the
150-
* grouper function computes the sum of all values below the cap.
146+
* if the slices cap is specified. If set to a falsy value, no others will be added.
147+
*
148+
* The grouper function takes an array of included ("top") items, and an array of the rest of
149+
* the items. By default the grouper function computes the sum of the rest.
151150
* @method othersGrouper
152151
* @memberof dc.capMixin
153152
* @instance
154153
* @example
155154
* // Do not show others
156155
* chart.othersGrouper(null);
157156
* // Default others grouper
158-
* chart.othersGrouper(function (topRows) {
159-
* var topRowsSum = d3.sum(topRows, _chart.valueAccessor()),
160-
* allRows = _chart.group().all(),
161-
* allRowsSum = d3.sum(allRows, _chart.valueAccessor()),
162-
* topKeys = topRows.map(_chart.keyAccessor()),
163-
* allKeys = allRows.map(_chart.keyAccessor()),
164-
* topSet = d3.set(topKeys),
165-
* others = allKeys.filter(function (d) {return !topSet.has(d);});
166-
* if (allRowsSum > topRowsSum) {
167-
* return topRows.concat([{
168-
* 'others': others,
169-
* 'key': _chart.othersLabel(),
170-
* 'value': allRowsSum - topRowsSum
171-
* }]);
172-
* }
173-
* return topRows;
174-
* });
175-
* // Custom others grouper
176-
* chart.othersGrouper(function (data) {
177-
* // compute the value for others, presumably the sum of all values below the cap
178-
* var othersSum = yourComputeOthersValueLogic(data)
179-
*
180-
* // the keys are needed to properly filter when the others element is clicked
181-
* var othersKeys = yourComputeOthersKeysArrayLogic(data);
182-
*
183-
* // add the others row to the dataset
184-
* data.push({'key': 'Others', 'value': othersSum, 'others': othersKeys });
185-
*
186-
* return data;
157+
* chart.othersGrouper(function (topItems, restItems) {
158+
* var restItemsSum = d3.sum(restItems, _chart.valueAccessor()),
159+
* restKeys = restItems.map(_chart.keyAccessor());
160+
* if (restItemsSum > 0) {
161+
* return topItems.concat([{
162+
* 'others': restKeys,
163+
* 'key': _chart.othersLabel(),
164+
* 'value': restItemsSum
165+
* }]);
166+
* }
167+
* return topItems;
187168
* });
188169
* @param {Function} [grouperFunction]
189170
* @returns {Function|dc.capMixin}

0 commit comments

Comments
 (0)