From 4f84cc59824718a4905175535ac8d397bca3b0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Wed, 23 Aug 2023 17:23:36 +0200 Subject: [PATCH 1/4] the denseInterval option for k must also reduce k1 and k2 if present closes #1784 --- src/transforms/bin.js | 9 ++++-- test/output/aaplInterval.svg | 53 ++++++++++++++++++++++++++++++++++++ test/plots/aapl-interval.ts | 13 +++++++++ test/plots/index.ts | 1 + 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 test/output/aaplInterval.svg create mode 100644 test/plots/aapl-interval.ts diff --git a/src/transforms/bin.js b/src/transforms/bin.js index f929abfedf..ac1177458f 100644 --- a/src/transforms/bin.js +++ b/src/transforms/bin.js @@ -65,9 +65,12 @@ export function bin(outputs = {fill: "count"}, options = {}) { } function maybeDenseInterval(bin, k, options = {}) { - return options?.interval == null - ? options - : bin({[k]: options?.reduce === undefined ? reduceFirst : options.reduce, filter: null}, options); + if (options?.interval == null) return options; + const reduce = options?.reduce === undefined ? reduceFirst : options.reduce; + const outputs = {[k]: reduce, filter: null}; + if (options[`${k}1`] != null) outputs[`${k}1`] = reduce; + if (options[`${k}2`] != null) outputs[`${k}2`] = reduce; + return bin(outputs, options); } export function maybeDenseIntervalX(options = {}) { diff --git a/test/output/aaplInterval.svg b/test/output/aaplInterval.svg new file mode 100644 index 0000000000..608f36adc7 --- /dev/null +++ b/test/output/aaplInterval.svg @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + 56 + 58 + 60 + 62 + 64 + 66 + 68 + 70 + 72 + + + + + + + + + Jun2013 + Jul + Aug + Sep + + + + + \ No newline at end of file diff --git a/test/plots/aapl-interval.ts b/test/plots/aapl-interval.ts new file mode 100644 index 0000000000..8720e1adf1 --- /dev/null +++ b/test/plots/aapl-interval.ts @@ -0,0 +1,13 @@ +import * as Plot from "@observablehq/plot"; +import * as d3 from "d3"; + +export async function aaplInterval() { + const aapl = await d3.csv("data/aapl.csv", d3.autoType); + return Plot.areaY(aapl.slice(0, 81), { + x: "Date", + y1: "Low", + y2: "High", + interval: "day", + curve: "step" + }).plot(); +} diff --git a/test/plots/index.ts b/test/plots/index.ts index 82b3c2e88a..1e6d306afa 100644 --- a/test/plots/index.ts +++ b/test/plots/index.ts @@ -4,6 +4,7 @@ export * from "./aapl-change-volume.js"; export * from "./aapl-close-untyped.js"; export * from "./aapl-close.js"; export * from "./aapl-fancy-axis.js"; +export * from "./aapl-interval.js"; export * from "./aapl-monthly.js"; export * from "./aapl-volume-rect.js"; export * from "./aapl-volume.js"; From 2524cab606330f474f01eacb13392cfe1a018ef2 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 23 Aug 2023 20:56:27 -0400 Subject: [PATCH 2/4] Update src/transforms/bin.js --- src/transforms/bin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transforms/bin.js b/src/transforms/bin.js index ac1177458f..57a55748dc 100644 --- a/src/transforms/bin.js +++ b/src/transforms/bin.js @@ -66,7 +66,7 @@ export function bin(outputs = {fill: "count"}, options = {}) { function maybeDenseInterval(bin, k, options = {}) { if (options?.interval == null) return options; - const reduce = options?.reduce === undefined ? reduceFirst : options.reduce; + const {reduce = reduceFirst} = options; const outputs = {[k]: reduce, filter: null}; if (options[`${k}1`] != null) outputs[`${k}1`] = reduce; if (options[`${k}2`] != null) outputs[`${k}2`] = reduce; From 452be909bffc53c0ee2614271dd27598b74085ee Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 23 Aug 2023 18:00:05 -0700 Subject: [PATCH 3/4] conditional reduce of k, too --- src/transforms/bin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transforms/bin.js b/src/transforms/bin.js index 57a55748dc..edb5d8e8d4 100644 --- a/src/transforms/bin.js +++ b/src/transforms/bin.js @@ -67,7 +67,8 @@ export function bin(outputs = {fill: "count"}, options = {}) { function maybeDenseInterval(bin, k, options = {}) { if (options?.interval == null) return options; const {reduce = reduceFirst} = options; - const outputs = {[k]: reduce, filter: null}; + const outputs = {filter: null}; + if (options[k] != null) outputs[k] = reduce; if (options[`${k}1`] != null) outputs[`${k}1`] = reduce; if (options[`${k}2`] != null) outputs[`${k}2`] = reduce; return bin(outputs, options); From 59d871c936b6c86fbbe4d9405f3b3392c81ebf4f Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 23 Aug 2023 18:00:42 -0700 Subject: [PATCH 4/4] prettier --- test/plots/aapl-interval.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/plots/aapl-interval.ts b/test/plots/aapl-interval.ts index 8720e1adf1..bc66b29275 100644 --- a/test/plots/aapl-interval.ts +++ b/test/plots/aapl-interval.ts @@ -3,11 +3,5 @@ import * as d3 from "d3"; export async function aaplInterval() { const aapl = await d3.csv("data/aapl.csv", d3.autoType); - return Plot.areaY(aapl.slice(0, 81), { - x: "Date", - y1: "Low", - y2: "High", - interval: "day", - curve: "step" - }).plot(); + return Plot.areaY(aapl.slice(0, 81), {x: "Date", y1: "Low", y2: "High", interval: "day", curve: "step"}).plot(); }