Skip to content

Commit 9611663

Browse files
committed
find documentation
1 parent b8659b8 commit 9611663

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

docs/transforms/bin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ In addition, a reducer may be specified as:
280280
* a function to be passed the array of values for each bin and the extent of the bin
281281
* an object with a **reduceIndex** method, and optionally a **scope**
282282

283-
In the last case, the **reduceIndex** method is repeatedly passed three arguments: the index for each bin (an array of integers), the input channel’s array of values, and the extent of the bin (an object {x1, x2, y1, y2}); it must then return the corresponding aggregate value for the bin.
283+
In the last case, the **reduceIndex** method is repeatedly passed three arguments: the index for each bin (an array of integers), the input channel’s array of values, and the extent of the bin (an object {data, x1, x2, y1, y2}); it must then return the corresponding aggregate value for the bin.
284284

285285
If the reducer object’s **scope** is *data*, then the **reduceIndex** method is first invoked for the full data; the return value of the **reduceIndex** method is then made available as a third argument (making the extent the fourth argument). Similarly if the **scope** is *facet*, then the **reduceIndex** method is invoked for each facet, and the resulting reduce value is made available while reducing the facet’s bins. (This optional **scope** is used by the *proportion* and *proportion-facet* reducers.)
286286

docs/transforms/group.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,12 @@ The following named reducers are supported:
369369

370370
In addition, a reducer may be specified as:
371371

372-
* a function - passed the array of values for each group
372+
* a function to be passed the array of values for each group and the extent of the group
373373
* an object with a **reduceIndex** method, an optionally a **scope**
374374

375-
In the last case, the **reduceIndex** method is repeatedly passed two arguments: the index for each group (an array of integers), and the input channel’s array of values; it must then return the corresponding aggregate value for the group.
375+
In the last case, the **reduceIndex** method is repeatedly passed three arguments: the index for each group (an array of integers), the input channel’s array of values, and the extent of the group (an object {data, x, y}); it must then return the corresponding aggregate value for the group.
376376

377-
If the reducer object’s **scope** is *data*, then the **reduceIndex** method is first invoked for the full data; the return value of the **reduceIndex** method is then made available as a third argument. Similarly if the **scope** is *facet*, then the **reduceIndex** method is invoked for each facet, and the resulting reduce value is made available while reducing the facet’s groups. (This optional **scope** is used by the *proportion* and *proportion-facet* reducers.)
377+
If the reducer object’s **scope** is *data*, then the **reduceIndex** method is first invoked for the full data; the return value of the **reduceIndex** method is then made available as a third argument (making the extent the fourth argument). Similarly if the **scope** is *facet*, then the **reduceIndex** method is invoked for each facet, and the resulting reduce value is made available while reducing the facet’s groups. (This optional **scope** is used by the *proportion* and *proportion-facet* reducers.)
378378

379379
Most reducers require binding the output channel to an input channel; for example, if you want the **y** output channel to be a *sum* (not merely a count), there should be a corresponding **y** input channel specifying which values to sum. If there is not, *sum* will be equivalent to *count*.
380380

@@ -435,3 +435,14 @@ Plot.groupZ({x: "proportion"}, {fill: "species"})
435435
```
436436

437437
Groups on the first channel of **z**, **fill**, or **stroke**, if any. If none of **z**, **fill**, or **stroke** are channels, then all data (within each facet) is placed into a single group.
438+
439+
## find(*test*) {#find}
440+
441+
```js
442+
Plot.groupX(
443+
{y1: Plot.find((d) => d.sex === "F"), y2: Plot.find((d) => d.sex === "M")},
444+
{x: "date", y: "value"}
445+
)
446+
```
447+
448+
Returns a reducer that finds the first datum for which the given *test* function returns a truthy value, and returns the corresponding channel value. This may be used with the group or bin transform to implement a “pivot wider” transform; for example, a “tall” dataset with separate rows for male and female observations may be transformed into a “wide” dataset with separate columns for male and female values.

test/plots/find.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,8 @@ export async function findArrow() {
2929
Plot.arrow(
3030
ilc,
3131
Plot.groupX(
32-
{
33-
y1: Plot.find((d) => d.sex === "F"),
34-
y2: Plot.find((d) => d.sex === "M")
35-
},
36-
{
37-
x: "TIME_PERIOD",
38-
y: "OBS_VALUE",
39-
stroke: "age",
40-
bend: true
41-
}
32+
{y1: Plot.find((d) => d.sex === "F"), y2: Plot.find((d) => d.sex === "M")},
33+
{x: "TIME_PERIOD", y: "OBS_VALUE", stroke: "age", bend: true}
4234
)
4335
)
4436
]

0 commit comments

Comments
 (0)