Skip to content

Commit b44a874

Browse files
authored
x and y reducers (#771)
1 parent ed0848d commit b44a874

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,10 @@ The following aggregation methods are supported:
13571357
* *deviation* - the standard deviation
13581358
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
13591359
* *mode* - the value with the most occurrences
1360+
* *x* - the middle the bin’s *x*-extent (when binning on *x*)
13601361
* *x1* - the lower bound of the bin’s *x*-extent (when binning on *x*)
13611362
* *x2* - the upper bound of the bin’s *x*-extent (when binning on *x*)
1363+
* *y* - the middle the bin’s *y*-extent (when binning on *y*)
13621364
* *y1* - the lower bound of the bin’s *y*-extent (when binning on *y*)
13631365
* *y2* - the upper bound of the bin’s *y*-extent (when binning on *y*)
13641366
* a function to be passed the array of values for each bin and the extent of the bin

src/transforms/group.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ export function maybeReduce(reduce, value) {
215215
case "median": return reduceAccessor(median);
216216
case "variance": return reduceAccessor(variance);
217217
case "mode": return reduceAccessor(mode);
218+
case "x": return reduceX;
218219
case "x1": return reduceX1;
219220
case "x2": return reduceX2;
221+
case "y": return reduceY;
220222
case "y1": return reduceY1;
221223
case "y2": return reduceY2;
222224
}
@@ -313,6 +315,23 @@ function reduceProportion(value, scope) {
313315
: {scope, reduce: (I, V, basis = 1) => sum(I, i => V[i]) / basis};
314316
}
315317

318+
function mid(x1, x2) {
319+
const m = (+x1 + +x2) / 2;
320+
return x1 instanceof Date ? new Date(m) : m;
321+
}
322+
323+
const reduceX = {
324+
reduce(I, X, {x1, x2}) {
325+
return mid(x1, x2);
326+
}
327+
};
328+
329+
const reduceY = {
330+
reduce(I, X, {y1, y2}) {
331+
return mid(y1, y2);
332+
}
333+
};
334+
316335
const reduceX1 = {
317336
reduce(I, X, {x1}) {
318337
return x1;

test/plots/athletes-bins-colors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default async function() {
55
const athletes = await d3.csv("data/athletes.csv", d3.autoType);
66
return Plot.plot({
77
marks: [
8-
Plot.rectY(athletes, Plot.binX({fill: "x1", y: "count"}, {x: "weight"})),
8+
Plot.rectY(athletes, Plot.binX({fill: "x", y: "count"}, {x: "weight"})),
99
Plot.ruleY([0])
1010
]
1111
});

0 commit comments

Comments
 (0)