Skip to content

map option for map[XY] #480

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

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1239,53 +1239,53 @@ Plot.map({y: "cumsum"}, {y: d3.randomNormal()})

Groups on the first channel of *z*, *fill*, or *stroke*, if any, and then for each channel declared in the specified *outputs* object, applies the corresponding map method. Each channel in *outputs* must have a corresponding input channel in *options*.

#### Plot.mapX(*map*, *options*)
#### Plot.mapX(*options*)

```js
Plot.mapX("cumsum", {x: d3.randomNormal()})
Plot.mapX({map: "cumsum", x: d3.randomNormal()})
```

Equivalent to Plot.map({x: *map*, x1: *map*, x2: *map*}, *options*), but ignores any of **x**, **x1**, and **x2** not present in *options*.
Equivalent to Plot.map({x: *map*, x1: *map*, x2: *map*}, *options*), but ignores any of **x**, **x1**, and **x2** not present in *options*. The **map** option is required.

#### Plot.mapY(*map*, *options*)
#### Plot.mapY(*options*)

```js
Plot.mapY("cumsum", {y: d3.randomNormal()})
Plot.mapY({map: "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*.
Equivalent to Plot.map({y: *map*, y1: *map*, y2: *map*}, *options*), but ignores any of **y**, **y1**, and **y2** not present in *options*. The **map** option is required.

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

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

Like [Plot.mapX](#plotmapxmap-options), but applies the normalize map method with the given *options*.
Like [Plot.mapX](#plotmapxmap-options), but applies the normalize map method with the given *options*. If the **basis** option is not specified, it defaults to *first*.

#### Plot.normalizeY(*options*)

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

Like [Plot.mapY](#plotmapymap-options), but applies the normalize map method with the given *options*.
Like [Plot.mapY](#plotmapymap-options), but applies the normalize map method with the given *options*. If the **basis** option is not specified, it defaults to *first*.

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

```js
Plot.windowX({y: "Date", x: "Anomaly", k: 24})
```

Like [Plot.mapX](#plotmapxmap-options), but applies the window map method with the given *options*.
Like [Plot.mapX](#plotmapxmap-options), but applies the window map method with the given *options*. The **k** option is required to define the window size, while the **reduce** and **shift** options default to *mean* and *centered* respectively.

#### Plot.windowY(*options*)

```js
Plot.windowY({x: "Date", y: "Anomaly", k: 24})
```

Like [Plot.mapY](#plotmapymap-options), but applies the window map method with the given *options*.
Like [Plot.mapY](#plotmapymap-options), but applies the window map method with the given *options*. The **k** option is required to define the window size, while the **reduce** and **shift** options default to *mean* and *centered* respectively.

### Select

Expand Down
6 changes: 4 additions & 2 deletions src/transforms/map.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {group} from "d3";
import {maybeTransform, maybeZ, take, valueof, maybeInput, lazyChannel} from "../mark.js";

export function mapX(m, options = {}) {
export function mapX(m = {}, options = {}) {
if (arguments.length === 1) ({map: m, ...options} = m);
return map(Object.fromEntries(["x", "x1", "x2"]
.filter(key => options[key] != null)
.map(key => [key, m])), options);
}

export function mapY(m, options = {}) {
export function mapY(m = {}, options = {}) {
if (arguments.length === 1) ({map: m, ...options} = m);
return map(Object.fromEntries(["y", "y1", "y2"]
.filter(key => options[key] != null)
.map(key => [key, m])), options);
Expand Down
2 changes: 1 addition & 1 deletion test/plots/random-walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async function() {
return Plot.plot({
marks: [
Plot.lineY(d3.cumsum({length: 500}, randomNormal), {stroke: "red"}),
Plot.lineY({length: 500}, Plot.mapY("cumsum", {y: randomNormal, stroke: "blue"}))
Plot.lineY({length: 500}, Plot.mapY({map: "cumsum", y: randomNormal, stroke: "blue"}))
]
});
}