diff --git a/src/transforms/stack.js b/src/transforms/stack.js index 9c96997af0..11af5bf5bb 100644 --- a/src/transforms/stack.js +++ b/src/transforms/stack.js @@ -106,7 +106,7 @@ function stack(x, y = one, kx, ky, {offset, order, reverse}, options) { const y = Y[i]; if (y < 0) yn = Y2[i] = (Y1[i] = yn) + y; else if (y > 0) yp = Y2[i] = (Y1[i] = yp) + y; - else Y2[i] = Y1[i] = yp; // NaN or zero + else Y2[i] = Y1[i] = yp || yn; // NaN or zero } } facetstacks.push(stacks); diff --git a/test/output/aaplStackGains.html b/test/output/aaplStackGains.html new file mode 100644 index 0000000000..3cda49bf8c --- /dev/null +++ b/test/output/aaplStackGains.html @@ -0,0 +1,105 @@ +
+
+ + + Mon + + Tue + + Wed + + Thu + + Fri +
+ + + + + + + + + + + + + + −20 + −15 + −10 + −5 + 0 + 5 + 10 + 15 + 20 + + + + + + + + + + 2014 + 2015 + 2016 + 2017 + 2018 + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/test/plots/aapl-stack-gains.ts b/test/plots/aapl-stack-gains.ts new file mode 100644 index 0000000000..a6d2781943 --- /dev/null +++ b/test/plots/aapl-stack-gains.ts @@ -0,0 +1,22 @@ +import * as Plot from "@observablehq/plot"; +import * as d3 from "d3"; + +export async function aaplStackGains() { + const aapl = await d3.csv("data/aapl.csv", d3.autoType); + const day = (d) => d.Date.toLocaleDateString("en-US", {weekday: "short"}); + return Plot.plot({ + color: {legend: true, domain: ["Mon", "Tue", "Wed", "Thu", "Fri"]}, + marks: [ + [Math.min, Math.max].map((op) => + Plot.areaY( + aapl, + Plot.binX( + {y: (d) => op(0, d3.sum(d)), interval: "2 months"}, + {x: "Date", y: (d) => d.Close - d.Open, fill: day, curve: "monotone-x"} + ) + ) + ), + Plot.ruleY([0]) + ] + }); +} diff --git a/test/plots/index.ts b/test/plots/index.ts index 0a3ee3eb0d..2f3eddf516 100644 --- a/test/plots/index.ts +++ b/test/plots/index.ts @@ -6,6 +6,7 @@ export * from "./aapl-close.js"; export * from "./aapl-fancy-axis.js"; export * from "./aapl-interval.js"; export * from "./aapl-monthly.js"; +export * from "./aapl-stack-gains.js"; export * from "./aapl-volume-rect.js"; export * from "./aapl-volume.js"; export * from "./anscombe-quartet.js";