Skip to content

Commit 5f4b838

Browse files
mbostockFil
andauthored
expose identity reducer (#1382)
* expose identity reducer * test identity * document: "identity" reducer support for bin, group and hexbin test hexbin --------- Co-authored-by: Philippe Rivière <[email protected]>
1 parent 0a87365 commit 5f4b838

9 files changed

+558
-47
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ The following aggregation methods are supported:
21212121
* *pXX* - the percentile value, where XX is a number in [00,99]
21222122
* *deviation* - the standard deviation
21232123
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
2124+
* *identity* - the array of values
21242125
* *x* - the middle of the bin’s *x* extent (when binning on *x*)
21252126
* *x1* - the lower bound of the bin’s *x* extent (when binning on *x*)
21262127
* *x2* - the upper bound of the bin’s *x* extent (when binning on *x*)
@@ -2278,6 +2279,7 @@ The following aggregation methods are supported:
22782279
* *pXX* - the percentile value, where XX is a number in [00,99]
22792280
* *deviation* - the standard deviation
22802281
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
2282+
* *identity* - the array of values
22812283
* a function - passed the array of values for each group
22822284
* an object with a *reduceIndex* method, an optionally a *scope*
22832285
@@ -2852,6 +2854,7 @@ The following aggregation methods are supported:
28522854
* *deviation* - the standard deviation
28532855
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
28542856
* *mode* - the value with the most occurrences
2857+
* *identity* - the array of values
28552858
* a function to be passed the array of values for each bin and the extent of the bin
28562859
* an object with a *reduceIndex* method
28572860

src/reducer.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ export type ReducerPercentile =
2727
* - *variance* - the variance per [Welford’s algorithm][1]
2828
* - *mode* - the value with the most occurrences
2929
* - *pXX* - the percentile value, where XX is a number in [00,99]
30+
* - *identity* - the array of values
3031
*
3132
* [1]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm
3233
*/
3334
export type ReducerName =
3435
| "first"
3536
| "last"
37+
| "identity"
3638
| "count"
3739
| "distinct"
3840
| "sum"

src/transforms/group.js

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ export function maybeReduce(reduce, value, fallback = invalidReduce) {
245245
return reduceFirst;
246246
case "last":
247247
return reduceLast;
248+
case "identity":
249+
return reduceIdentity;
248250
case "count":
249251
return reduceCount;
250252
case "distinct":

test/output/gridReduceIdentity.svg

+79
Loading

test/output/hexbinIdentityReduce.svg

+397
Loading

test/plots/grid-choropleth-dx.ts

-46
This file was deleted.

test/plots/grid-choropleth.ts

+56
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,62 @@ export async function gridChoropleth() {
3636
});
3737
}
3838

39+
export async function gridChoroplethDx() {
40+
const [grid, data] = await Promise.all([
41+
await d3.csv<any>("data/us-state-grid.csv", d3.autoType).then(gridmap),
42+
await d3.csv<any>("data/us-state-population-2010-2019.csv", d3.autoType)
43+
]);
44+
const states = data.filter((d) => grid.has(d.State)).map((d) => ({...d, ...grid.get(d.State)}));
45+
return Plot.plot({
46+
height: 420,
47+
x: {
48+
axis: null
49+
},
50+
y: {
51+
axis: null
52+
},
53+
color: {
54+
type: "diverging-log",
55+
scheme: "piyg"
56+
},
57+
marks: [
58+
Plot.cell(states, {x: "x", y: "y", fill: change, dx: -1.5, dy: -1.5}),
59+
Plot.cell(states, {x: "x", y: "y", stroke: "black", dx: 1.5, dy: 1.5}),
60+
Plot.text(states, {x: "x", y: "y", text: "key", dy: -6}),
61+
Plot.text(states, {
62+
x: "x",
63+
y: "y",
64+
text: (
65+
(f) => (d) =>
66+
f(change(d) - 1)
67+
)(d3.format("+.0%")),
68+
dy: 6,
69+
fillOpacity: 0.6
70+
})
71+
]
72+
});
73+
}
74+
75+
export async function gridReduceIdentity() {
76+
const grid = await d3.csv<any>("data/us-state-grid.csv", d3.autoType);
77+
return Plot.plot({
78+
axis: null,
79+
x: {insetRight: 200},
80+
y: {reverse: true},
81+
aspectRatio: true,
82+
marks: [
83+
Plot.rect(grid, {x: "x", y: "y", stroke: "currentColor", interval: 1, inset: 3}),
84+
Plot.text(
85+
grid,
86+
Plot.groupY(
87+
{y: ([y]) => y + 0.5, text: "identity"},
88+
{sort: "x", frameAnchor: "right", y: "y", text: "key", dx: -10, stroke: "white", fill: "currentColor"}
89+
)
90+
)
91+
]
92+
});
93+
}
94+
3995
function gridmap(states: {name: string}[]) {
4096
return new Map(states.map((state) => [state.name, state]));
4197
}

test/plots/hexbin-z.ts

+19
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,22 @@ export async function hexbinZ() {
1515
]
1616
});
1717
}
18+
19+
export async function hexbinIdentityReduce() {
20+
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
21+
return Plot.plot({
22+
inset: 10,
23+
height: 600,
24+
marks: [
25+
Plot.hexgrid({binWidth: 8, strokeOpacity: 0.05}),
26+
Plot.frame(),
27+
Plot.text(
28+
penguins,
29+
Plot.hexbin(
30+
{text: "identity"},
31+
{x: "culmen_length_mm", y: "body_mass_g", text: (d) => d.species[0], binWidth: 8, fontSize: 7}
32+
)
33+
)
34+
]
35+
});
36+
}

test/plots/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export * from "./gistemp-anomaly.js";
9797
export * from "./google-trends-ridgeline.js";
9898
export * from "./graticule.js";
9999
export * from "./greek-gods.js";
100-
export * from "./grid-choropleth-dx.js";
101100
export * from "./grid-choropleth.js";
102101
export * from "./grouped-rects.js";
103102
export * from "./hadcrut-warming-stripes.js";

0 commit comments

Comments
 (0)