Skip to content

Commit c8384ab

Browse files
authored
default identity map (#1549)
* default identity map * identity docs
1 parent b09d5a6 commit c8384ab

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

docs/transforms/map.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ Groups on the first channel of **z**, **fill**, or **stroke**, if any, and then
132132
Plot.mapX("cumsum", {x: d3.randomNormal()})
133133
```
134134

135-
Equivalent to Plot.map({x: *map*, x1: *map*, x2: *map*}, *options*), but ignores any of **x**, **x1**, and **x2** not present in *options*.
135+
Equivalent to Plot.map({x: *map*, x1: *map*, x2: *map*}, *options*), but ignores any of **x**, **x1**, and **x2** not present in *options*. In addition, if none of **x**, **x1**, or **x2** are specified, then **x** defaults to [identity](../features/transforms.md#identity).
136136

137137
## mapY(*map*, *options*)
138138

139139
```js
140140
Plot.mapY("cumsum", {y: d3.randomNormal()})
141141
```
142142

143-
Equivalent to Plot.map({y: *map*, y1: *map*, y2: *map*}, *options*), but ignores any of **y**, **y1**, and **y2** not present in *options*.
143+
Equivalent to Plot.map({y: *map*, y1: *map*, y2: *map*}, *options*), but ignores any of **y**, **y1**, and **y2** not present in *options*. In addition, if none of **y**, **y1**, or **y2** are specified, then **y** defaults to [identity](../features/transforms.md#identity).

src/transforms/map.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import {count, group, rank} from "d3";
2-
import {column, isObject, maybeInput, maybeZ, take, valueof} from "../options.js";
2+
import {column, identity, isObject, maybeInput, maybeZ, take, valueof} from "../options.js";
33
import {basic} from "./basic.js";
44

55
export function mapX(mapper, options = {}) {
6-
return map(
7-
Object.fromEntries(["x", "x1", "x2"].filter((key) => options[key] != null).map((key) => [key, mapper])),
8-
options
9-
);
6+
let {x, x1, x2} = options;
7+
if (x === undefined && x1 === undefined && x2 === undefined) options = {...options, x: (x = identity)};
8+
const outputs = {};
9+
if (x != null) outputs.x = mapper;
10+
if (x1 != null) outputs.x1 = mapper;
11+
if (x2 != null) outputs.x2 = mapper;
12+
return map(outputs, options);
1013
}
1114

1215
export function mapY(mapper, options = {}) {
13-
return map(
14-
Object.fromEntries(["y", "y1", "y2"].filter((key) => options[key] != null).map((key) => [key, mapper])),
15-
options
16-
);
16+
let {y, y1, y2} = options;
17+
if (y === undefined && y1 === undefined && y2 === undefined) options = {...options, y: (y = identity)};
18+
const outputs = {};
19+
if (y != null) outputs.y = mapper;
20+
if (y1 != null) outputs.y1 = mapper;
21+
if (y2 != null) outputs.y2 = mapper;
22+
return map(outputs, options);
1723
}
1824

1925
export function map(outputs = {}, options = {}) {

test/output/shorthandLineYWindow.svg

Lines changed: 61 additions & 0 deletions
Loading

test/plots/shorthand-lineY.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ export async function shorthandLineY() {
88
];
99
return Plot.lineY(numbers).plot();
1010
}
11+
12+
export async function shorthandLineYWindow() {
13+
const numbers = [
14+
170.16, 172.53, 172.54, 173.44, 174.35, 174.55, 173.16, 174.59, 176.18, 177.9, 176.15, 179.37, 178.61, 177.3, 177.3,
15+
177.25, 174.51, 172.0, 170.16, 165.53, 166.87, 167.17, 166.0, 159.1, 154.83, 163.09, 160.29, 157.07, 158.5, 161.95,
16+
163.04, 169.79, 172.36, 172.05, 172.83, 171.8, 173.67, 176.35, 179.1, 179.26
17+
];
18+
return Plot.lineY(numbers, Plot.windowY(7)).plot();
19+
}

0 commit comments

Comments
 (0)