Skip to content

Commit

Permalink
Put zero values at zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 16, 2019
1 parent 394507c commit bf15db4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ Applies a zero baseline and normalizes the values for each point such that the t

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

Positive values are stacked above zero, while negative values are [stacked below zero](https://bl.ocks.org/mbostock/b5935342c6d21928111928401e2c8608).
Positive values are stacked above zero, negative values are [stacked below zero](https://bl.ocks.org/mbostock/b5935342c6d21928111928401e2c8608), and zero values are stacked at zero.

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

Expand Down
4 changes: 2 additions & 2 deletions src/offset/diverging.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ export default function(series, order) {
if (!((n = series.length) > 0)) return;
for (var i, j = 0, d, dy, yp, yn, n, m = series[order[0]].length; j < m; ++j) {
for (yp = yn = 0, i = 0; i < n; ++i) {
if ((dy = (d = series[order[i]][j])[1] - d[0]) >= 0) {
if ((dy = (d = series[order[i]][j])[1] - d[0]) > 0) {
d[0] = yp, d[1] = yp += dy;
} else if (dy < 0) {
d[1] = yn, d[0] = yn += dy;
} else {
d[0] = yp;
d[0] = 0, d[1] = dy;
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions test/offset/diverging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tape("stackOffsetDiverging(series, order) applies a zero baseline, ignoring exis
test.end();
});

tape.only("stackOffsetDiverging(series, order) handles a single series", function(test) {
tape("stackOffsetDiverging(series, order) handles a single series", function(test) {
var series = [
[[1, 2], [2, 4], [3, 4]]
];
Expand All @@ -38,7 +38,7 @@ tape("stackOffsetDiverging(series, order) treats NaN as zero", function(test) {
series[1][1][1] = "NaN"; // can’t test.equal NaN
test.deepEqual(series, [
[[0, 1], [0, 2], [0, 1]],
[[1, 4], [2, "NaN"], [1, 3]],
[[1, 4], [0, "NaN"], [1, 3]],
[[4, 9], [2, 4], [3, 7]]
]);
test.end();
Expand Down Expand Up @@ -73,3 +73,18 @@ tape("stackOffsetDiverging(series, order) puts negative values below zero, in or
]);
test.end();
});

tape("stackOffsetDiverging(series, order) puts zero values at zero, in order", function(test) {
var series = [
[[0, 1], [0, 2], [0, -1]],
[[0, 3], [0, 0], [0, 0]],
[[0, 5], [0, 2], [0, 4]]
];
shape.stackOffsetDiverging(series, shape.stackOrderNone(series));
test.deepEqual(series, [
[[0, 1], [0, 2], [-1, 0]],
[[1, 4], [0, 0], [0, 0]],
[[4, 9], [2, 4], [0, 4]]
]);
test.end();
});

0 comments on commit bf15db4

Please sign in to comment.