@@ -16,22 +16,17 @@ dc.capMixin = function (_chart) {
16
16
var _cap = Infinity , _takeFront = true ;
17
17
var _othersLabel = 'Others' ;
18
18
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 ,
30
25
'key' : _chart . othersLabel ( ) ,
31
- 'value' : allRowsSum - topRowsSum
26
+ 'value' : restItemsSum
32
27
} ] ) ;
33
28
}
34
- return topRows ;
29
+ return topItems ;
35
30
} ;
36
31
37
32
_chart . cappedKeyAccessor = function ( d , i ) {
@@ -54,20 +49,22 @@ dc.capMixin = function (_chart) {
54
49
if ( _cap === Infinity ) {
55
50
return _chart . _computeOrderedGroups ( group . all ( ) ) ;
56
51
} else {
57
- var items = group . all ( ) ;
52
+ var items = group . all ( ) , rest ;
58
53
items = _chart . _computeOrderedGroups ( items ) ; // sort by baseMixin.ordering
59
54
60
55
if ( _cap ) {
61
56
if ( _takeFront ) {
57
+ rest = items . slice ( _cap ) ;
62
58
items = items . slice ( 0 , _cap ) ;
63
59
} else {
64
60
var start = Math . max ( 0 , items . length - _cap ) ;
61
+ rest = items . slice ( 0 , start ) ;
65
62
items = items . slice ( start ) ;
66
63
}
67
64
}
68
65
69
66
if ( _othersGrouper ) {
70
- return _othersGrouper ( items ) ;
67
+ return _othersGrouper ( items , rest ) ;
71
68
}
72
69
return items ;
73
70
}
@@ -146,44 +143,28 @@ dc.capMixin = function (_chart) {
146
143
147
144
/**
148
145
* 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.
151
150
* @method othersGrouper
152
151
* @memberof dc.capMixin
153
152
* @instance
154
153
* @example
155
154
* // Do not show others
156
155
* chart.othersGrouper(null);
157
156
* // 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;
187
168
* });
188
169
* @param {Function } [grouperFunction]
189
170
* @returns {Function|dc.capMixin }
0 commit comments