Skip to content

Commit

Permalink
Rewrite stackOrderAppearance.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 23, 2019
1 parent 8b17156 commit 50e115f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ function offsetNone(series, order) {

Stack orders are typically not used directly, but are instead passed to [*stack*.order](#stack_order).

<a name="stackOrderAppearance" href="#stackOrderAppearance">#</a> d3.<b>stackOrderAppearance</b>(<i>series</i>) [<>](https://github.com/d3/d3-shape/blob/master/src/order/appearance.js "Source")

Returns a series order such that the earliest series (according to the first non-zero value) is at the bottom.

<a name="stackOrderAscending" href="#stackOrderAscending">#</a> d3.<b>stackOrderAscending</b>(<i>series</i>) [<>](https://github.com/d3/d3-shape/blob/master/src/order/ascending.js "Source")

Returns a series order such that the smallest series (according to the sum of values) is at the bottom.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export {default as stackOffsetDiverging} from "./offset/diverging";
export {default as stackOffsetNone} from "./offset/none";
export {default as stackOffsetSilhouette} from "./offset/silhouette";
export {default as stackOffsetWiggle} from "./offset/wiggle";
export {default as stackOrderAppearance} from "./order/appearance";
export {default as stackOrderAscending} from "./order/ascending";
export {default as stackOrderDescending} from "./order/descending";
export {default as stackOrderInsideOut} from "./order/insideOut";
Expand Down
18 changes: 10 additions & 8 deletions src/order/appearance.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import none from "./none";

export default function(series) {
var appearanceIndex = series.map(function(d,i){
for(var k = 0, n = d.length;k < n;++k){
if(d[k][1] > 0) break;
}
return [k, i];
});
appearanceIndex.sort(function(a,b){return a[0] - b[0];})
return appearanceIndex.map(function(d){return d[1]});
var starts = series.map(start);
return none(series).sort(function(a, b) { return starts[a] - starts[b]; });
}

function start(series) {
var i = -1, n = series.length;
while (++i < n && !+series[i][1]);
return i;
}
20 changes: 20 additions & 0 deletions test/order/appearance-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var tape = require("tape"),
shape = require("../../");

tape("stackOrderAppearance(series) returns an order by appearance", function(test) {
test.deepEqual(shape.stackOrderAppearance([
[[0, 0], [0, 0], [0, 0]],
[[0, 0], [0, 2], [0, 3]],
[[0, 0], [0, 0], [0, 4]]
]), [1, 2, 0]);
test.end();
});

tape("stackOrderAppearance(series) treats NaN values as zero", function(test) {
test.deepEqual(shape.stackOrderAppearance([
[[0, 0], [0, NaN], [0, NaN]],
[[0, 0], [0, 2], [0, 3]],
[[0, 0], [0, NaN], [0, 4]]
]), [1, 2, 0]);
test.end();
});

0 comments on commit 50e115f

Please sign in to comment.