Skip to content

expose window, normalize #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,14 @@ Plot.mapY("cumsum", {y: d3.randomNormal()})

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

#### Plot.normalize(*basis*)

```js
Plot.map({y: Plot.normalize("first")}, {x: "Date", y: "Close", stroke: "Symbol"})
```

Returns a normalize map method for the given *basis*, suitable for use with Plot.map.

#### Plot.normalizeX(*basis*, *options*)

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

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

#### Plot.window(*k*)

```js
Plot.map({y: Plot.window(24)}, {x: "Date", y: "Close", stroke: "Symbol"})
```

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*.

#### Plot.windowX(*k*, *options*)

```js
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export {reverse} from "./transforms/reverse.js";
export {sort} from "./transforms/sort.js";
export {bin, binX, binY} from "./transforms/bin.js";
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
export {normalizeX, normalizeY} from "./transforms/normalize.js";
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";
export {map, mapX, mapY} from "./transforms/map.js";
export {windowX, windowY} from "./transforms/window.js";
export {window, windowX, windowY} from "./transforms/window.js";
export {selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY} from "./transforms/select.js";
export {stackX, stackX1, stackX2, stackY, stackY1, stackY2} from "./transforms/stack.js";
export {formatIsoDate, formatWeekday, formatMonth} from "./format.js";
2 changes: 1 addition & 1 deletion src/transforms/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function normalizeY(basis, options) {
return mapY(normalize(basis), options);
}

function normalize(basis) {
export function normalize(basis) {
if (basis === undefined) return normalizeFirst;
if (typeof basis === "function") return normalizeBasis((I, S) => basis(take(S, I)));
switch ((basis + "").toLowerCase()) {
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function windowY(windowOptions = {}, options) {
return mapY(window(windowOptions), options);
}

function window(options = {}) {
export function window(options = {}) {
if (typeof options === "number") options = {k: options};
let {k, reduce, shift, anchor = maybeShift(shift)} = options;
if (!((k = Math.floor(k)) > 0)) throw new Error("invalid k");
Expand Down
86 changes: 86 additions & 0 deletions test/output/aaplBollinger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plots/aapl-bollinger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const AAPL = await d3.csv("data/aapl.csv", d3.autoType);
return Plot.plot({
y: {
grid: true
},
marks: [
Plot.areaY(AAPL, Plot.map({y1: bollinger(20, -2), y2: bollinger(20, 2)}, {x: "Date", y: "Close", fillOpacity: 0.2})),
Plot.line(AAPL, Plot.map({y: bollinger(20, 0)}, {x: "Date", y: "Close", stroke: "blue"})),
Plot.line(AAPL, {x: "Date", y: "Close", strokeWidth: 1})
]
});
}

function bollinger(N, K) {
return Plot.window({k: N, reduce: Y => d3.mean(Y) + K * d3.deviation(Y), anchor: "end"});
}
1 change: 1 addition & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {default as aaplBollinger} from "./aapl-bollinger.js";
export {default as aaplCandlestick} from "./aapl-candlestick.js";
export {default as aaplChangeVolume} from "./aapl-change-volume.js";
export {default as aaplClose} from "./aapl-close.js";
Expand Down