Skip to content

Commit

Permalink
Merge branch 'release/2.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonwoodhull committed Feb 8, 2017
2 parents 6dc209d + be16c5f commit b01a6ff
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 197 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.4
* Simplified `capMixin.othersGrouper` default implementation by passing the rest of the items as well as those before the cap. This is possible because of [#934](https://github.com/dc-js/dc.js/issues/934) relying on sorting of `group.all()` instead of `group.top()`. The default implementation is now easy to understand and it should be easier to customize (if anyone should want to).
* Added example filtering segments of stack ([#657](https://github.com/dc-js/dc.js/issues/657))

## 2.1.3
* 2.1.2 did not observe the common convention of having the rows/pie slices ordered from greatest to least - now we take from the front by default. ([#1296](https://github.com/dc-js/dc.js/issues/1296)
* Add [`takeFront`](http://dc-js.github.io/dc.js/docs/html/dc.capMixin.html#takeFront) option, defaulted true, in case you want to take from the back isntead.
Expand Down
81 changes: 31 additions & 50 deletions dc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dc.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dc.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dc",
"version": "2.1.3",
"version": "2.1.4",
"license": "Apache-2.0",
"copyright": "2017",
"description": "A multi-dimensional charting library built to work natively with crossfilter and rendered using d3.js ",
Expand Down
75 changes: 28 additions & 47 deletions src/cap-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,17 @@ dc.capMixin = function (_chart) {
var _cap = Infinity, _takeFront = true;
var _othersLabel = 'Others';

var _othersGrouper = function (topRows) {
var topRowsSum = d3.sum(topRows, _chart.valueAccessor()),
allRows = _chart.group().all(),
allRowsSum = d3.sum(allRows, _chart.valueAccessor()),
topKeys = topRows.map(_chart.keyAccessor()),
allKeys = allRows.map(_chart.keyAccessor()),
topSet = d3.set(topKeys),
others = allKeys.filter(function (d) {return !topSet.has(d);});
if (allRowsSum > topRowsSum) {
return topRows.concat([{
'others': others,
'key': _chart.othersLabel(),
'value': allRowsSum - topRowsSum
var _othersGrouper = function (topItems, restItems) {
var restItemsSum = d3.sum(restItems, _chart.valueAccessor()),
restKeys = restItems.map(_chart.keyAccessor());
if (restItemsSum > 0) {
return topItems.concat([{
others: restKeys,
key: _chart.othersLabel(),
value: restItemsSum
}]);
}
return topRows;
return topItems;
};

_chart.cappedKeyAccessor = function (d, i) {
Expand All @@ -54,20 +49,22 @@ dc.capMixin = function (_chart) {
if (_cap === Infinity) {
return _chart._computeOrderedGroups(group.all());
} else {
var items = group.all();
var items = group.all(), rest;
items = _chart._computeOrderedGroups(items); // sort by baseMixin.ordering

if (_cap) {
if (_takeFront) {
rest = items.slice(_cap);
items = items.slice(0, _cap);
} else {
var start = Math.max(0, items.length - _cap);
rest = items.slice(0, start);
items = items.slice(start);
}
}

if (_othersGrouper) {
return _othersGrouper(items);
return _othersGrouper(items, rest);
}
return items;
}
Expand Down Expand Up @@ -146,44 +143,28 @@ dc.capMixin = function (_chart) {

/**
* Get or set the grouper function that will perform the insertion of data for the *Others* slice
* if the slices cap is specified. If set to a falsy value, no others will be added. By default the
* grouper function computes the sum of all values below the cap.
* if the slices cap is specified. If set to a falsy value, no others will be added.
*
* The grouper function takes an array of included ("top") items, and an array of the rest of
* the items. By default the grouper function computes the sum of the rest.
* @method othersGrouper
* @memberof dc.capMixin
* @instance
* @example
* // Do not show others
* chart.othersGrouper(null);
* // Default others grouper
* chart.othersGrouper(function (topRows) {
* var topRowsSum = d3.sum(topRows, _chart.valueAccessor()),
* allRows = _chart.group().all(),
* allRowsSum = d3.sum(allRows, _chart.valueAccessor()),
* topKeys = topRows.map(_chart.keyAccessor()),
* allKeys = allRows.map(_chart.keyAccessor()),
* topSet = d3.set(topKeys),
* others = allKeys.filter(function (d) {return !topSet.has(d);});
* if (allRowsSum > topRowsSum) {
* return topRows.concat([{
* 'others': others,
* 'key': _chart.othersLabel(),
* 'value': allRowsSum - topRowsSum
* }]);
* }
* return topRows;
* });
* // Custom others grouper
* chart.othersGrouper(function (data) {
* // compute the value for others, presumably the sum of all values below the cap
* var othersSum = yourComputeOthersValueLogic(data)
*
* // the keys are needed to properly filter when the others element is clicked
* var othersKeys = yourComputeOthersKeysArrayLogic(data);
*
* // add the others row to the dataset
* data.push({'key': 'Others', 'value': othersSum, 'others': othersKeys });
*
* return data;
* chart.othersGrouper(function (topItems, restItems) {
* var restItemsSum = d3.sum(restItems, _chart.valueAccessor()),
* restKeys = restItems.map(_chart.keyAccessor());
* if (restItemsSum > 0) {
* return topItems.concat([{
* others: restKeys,
* key: _chart.othersLabel(),
* value: restItemsSum
* }]);
* }
* return topItems;
* });
* @param {Function} [grouperFunction]
* @returns {Function|dc.capMixin}
Expand Down
48 changes: 16 additions & 32 deletions web/docs/api-latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ such as [.svg](#dc.baseMixin+svg) and [.xAxis](#dc.coordinateGridMixin+xAxis),
return values that are themselves chainable d3 objects.

**Kind**: global namespace
**Version**: 2.1.3
**Version**: 2.1.4
**Example**
```js
// Example chaining
Expand Down Expand Up @@ -4665,8 +4665,10 @@ Get or set the label for *Others* slice when slices cap is specified.
#### capMixin.othersGrouper([grouperFunction]) ⇒ <code>function</code> &#124; <code>[capMixin](#dc.capMixin)</code>
Get or set the grouper function that will perform the insertion of data for the *Others* slice
if the slices cap is specified. If set to a falsy value, no others will be added. By default the
grouper function computes the sum of all values below the cap.
if the slices cap is specified. If set to a falsy value, no others will be added.
The grouper function takes an array of included ("top") items, and an array of the rest of
the items. By default the grouper function computes the sum of the rest.
**Kind**: instance method of <code>[capMixin](#dc.capMixin)</code>
Expand All @@ -4679,35 +4681,17 @@ grouper function computes the sum of all values below the cap.
// Do not show others
chart.othersGrouper(null);
// Default others grouper
chart.othersGrouper(function (topRows) {
var topRowsSum = d3.sum(topRows, _chart.valueAccessor()),
allRows = _chart.group().all(),
allRowsSum = d3.sum(allRows, _chart.valueAccessor()),
topKeys = topRows.map(_chart.keyAccessor()),
allKeys = allRows.map(_chart.keyAccessor()),
topSet = d3.set(topKeys),
others = allKeys.filter(function (d) {return !topSet.has(d);});
if (allRowsSum > topRowsSum) {
return topRows.concat([{
'others': others,
'key': _chart.othersLabel(),
'value': allRowsSum - topRowsSum
}]);
}
return topRows;
});
// Custom others grouper
chart.othersGrouper(function (data) {
// compute the value for others, presumably the sum of all values below the cap
var othersSum = yourComputeOthersValueLogic(data)

// the keys are needed to properly filter when the others element is clicked
var othersKeys = yourComputeOthersKeysArrayLogic(data);

// add the others row to the dataset
data.push({'key': 'Others', 'value': othersSum, 'others': othersKeys });

return data;
chart.othersGrouper(function (topItems, restItems) {
var restItemsSum = d3.sum(restItems, _chart.valueAccessor()),
restKeys = restItems.map(_chart.keyAccessor());
if (restItemsSum > 0) {
return topItems.concat([{
others: restKeys,
key: _chart.othersLabel(),
value: restItemsSum
}]);
}
return topItems;
});
```
<a name="dc.bubbleMixin"></a>
Expand Down
Loading

0 comments on commit b01a6ff

Please sign in to comment.