Skip to content

Commit ce19abb

Browse files
mbostockFil
andauthored
expose window, normalize (#551)
* expose window, normalize * Update test/plots/aapl-bollinger.js Co-authored-by: Philippe Rivière <[email protected]> * update test snapshot Co-authored-by: Philippe Rivière <[email protected]>
1 parent 550fe5b commit ce19abb

File tree

7 files changed

+127
-4
lines changed

7 files changed

+127
-4
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,14 @@ Plot.mapY("cumsum", {y: d3.randomNormal()})
13401340

13411341
Equivalent to Plot.map({y: *map*, y1: *map*, y2: *map*}, *options*), but ignores any of **y**, **y1**, and **y2** not present in *options*.
13421342

1343+
#### Plot.normalize(*basis*)
1344+
1345+
```js
1346+
Plot.map({y: Plot.normalize("first")}, {x: "Date", y: "Close", stroke: "Symbol"})
1347+
```
1348+
1349+
Returns a normalize map method for the given *basis*, suitable for use with Plot.map.
1350+
13431351
#### Plot.normalizeX(*basis*, *options*)
13441352

13451353
```js
@@ -1356,6 +1364,14 @@ Plot.normalizeY("first", {x: "Date", y: "Close", stroke: "Symbol"})
13561364

13571365
Like [Plot.mapY](#plotmapymap-options), but applies the normalize map method with the given *basis*.
13581366

1367+
#### Plot.window(*k*)
1368+
1369+
```js
1370+
Plot.map({y: Plot.window(24)}, {x: "Date", y: "Close", stroke: "Symbol"})
1371+
```
1372+
1373+
Returns a window map method for the given window size *k*, suitable for use with Plot.map. For additional options to the window transform, replace the number *k* with an object with properties *k*, *anchor*, or *reduce*.
1374+
13591375
#### Plot.windowX(*k*, *options*)
13601376

13611377
```js

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export {reverse} from "./transforms/reverse.js";
1616
export {sort} from "./transforms/sort.js";
1717
export {bin, binX, binY} from "./transforms/bin.js";
1818
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
19-
export {normalizeX, normalizeY} from "./transforms/normalize.js";
19+
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";
2020
export {map, mapX, mapY} from "./transforms/map.js";
21-
export {windowX, windowY} from "./transforms/window.js";
21+
export {window, windowX, windowY} from "./transforms/window.js";
2222
export {selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY} from "./transforms/select.js";
2323
export {stackX, stackX1, stackX2, stackY, stackY1, stackY2} from "./transforms/stack.js";
2424
export {formatIsoDate, formatWeekday, formatMonth} from "./format.js";

src/transforms/normalize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function normalizeY(basis, options) {
1313
return mapY(normalize(basis), options);
1414
}
1515

16-
function normalize(basis) {
16+
export function normalize(basis) {
1717
if (basis === undefined) return normalizeFirst;
1818
if (typeof basis === "function") return normalizeBasis((I, S) => basis(take(S, I)));
1919
switch ((basis + "").toLowerCase()) {

src/transforms/window.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function windowY(windowOptions = {}, options) {
1111
return mapY(window(windowOptions), options);
1212
}
1313

14-
function window(options = {}) {
14+
export function window(options = {}) {
1515
if (typeof options === "number") options = {k: options};
1616
let {k, reduce, shift, anchor = maybeShift(shift)} = options;
1717
if (!((k = Math.floor(k)) > 0)) throw new Error("invalid k");

test/output/aaplBollinger.svg

Lines changed: 86 additions & 0 deletions
Loading

test/plots/aapl-bollinger.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const AAPL = await d3.csv("data/aapl.csv", d3.autoType);
6+
return Plot.plot({
7+
y: {
8+
grid: true
9+
},
10+
marks: [
11+
Plot.areaY(AAPL, Plot.map({y1: bollinger(20, -2), y2: bollinger(20, 2)}, {x: "Date", y: "Close", fillOpacity: 0.2})),
12+
Plot.line(AAPL, Plot.map({y: bollinger(20, 0)}, {x: "Date", y: "Close", stroke: "blue"})),
13+
Plot.line(AAPL, {x: "Date", y: "Close", strokeWidth: 1})
14+
]
15+
});
16+
}
17+
18+
function bollinger(N, K) {
19+
return Plot.window({k: N, reduce: Y => d3.mean(Y) + K * d3.deviation(Y), anchor: "end"});
20+
}

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export {default as aaplBollinger} from "./aapl-bollinger.js";
12
export {default as aaplCandlestick} from "./aapl-candlestick.js";
23
export {default as aaplChangeVolume} from "./aapl-change-volume.js";
34
export {default as aaplClose} from "./aapl-close.js";

0 commit comments

Comments
 (0)