Skip to content

Commit

Permalink
alternative to bin{filter} : bin {transform}, see #495 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Aug 11, 2021
1 parent 21aaf4d commit acb9bb8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {bin as binner, extent, thresholdFreedmanDiaconis, thresholdScott, thresh
import {valueof, range, identity, maybeLazyChannel, maybeTuple, maybeColor, maybeValue, mid, labelof, isTemporal} from "../mark.js";
import {offset} from "../style.js";
import {basic} from "./basic.js";
import {maybeGroup, maybeOutputs, maybeReduce, maybeSubgroup, reduceCount, reduceIdentity} from "./group.js";
import {maybeGroup, maybeOutputs, maybeReduce, maybeSubgroup, reduceIdentity} from "./group.js";

// Group on {z, fill, stroke}, then optionally on y, then bin x.
export function binX(outputs = {y: "count"}, {inset, insetLeft, insetRight, ...options} = {}) {
Expand Down Expand Up @@ -35,15 +35,14 @@ function binn(
gy, // optionally group on y (exclusive with by and gx)
{
data: reduceData = reduceIdentity,
filter: reduceFilter = reduceCount,
transform = ignoreEmpty,
...outputs // output channel definitions
} = {},
inputs = {} // input channels and options
) {
bx = maybeBin(bx);
by = maybeBin(by);
reduceData = maybeReduce(reduceData, identity);
reduceFilter = reduceFilter == null ? reduceTrue : maybeReduce(reduceFilter, identity);

// Compute the outputs. Don’t group on a channel if one of the output channels
// requires it as an input!
Expand Down Expand Up @@ -103,8 +102,8 @@ function binn(
for (const [x1, x2, fx] of BX) {
const bb = fx(g);
for (const [y1, y2, fy] of BY) {
const b = fy(bb);
if (!reduceFilter.reduce(b, data)) continue;
const b = transform(fy(bb));
if (!b) continue;
groupFacet.push(i++);
groupData.push(reduceData.reduce(b, data));
if (K) GK.push(k);
Expand Down Expand Up @@ -248,8 +247,6 @@ function maybeInset(inset, inset1, inset2) {
: [inset1, inset2];
}

const reduceTrue = {
reduce() {
return true;
}
};
function ignoreEmpty(bin) {
return bin.length ? bin : null;
}
2 changes: 1 addition & 1 deletion test/plots/athletes-height-weight-bin-stroke.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function() {
},
marks: [
Plot.rect(athletes, Plot.bin({fill: "count"}, {x: "weight", y: "height", thresholds: 50})),
Plot.rect(athletes, Plot.bin({filter: d => d.length > 20}, {x: "weight", y: "height", stroke: "grey", inset: 0, thresholds: 50}))
Plot.rect(athletes, Plot.bin({transform: bin => bin.length > 20 ? bin : null}, {x: "weight", y: "height", stroke: "grey", inset: 0, thresholds: 50}))
]
});
}
4 changes: 2 additions & 2 deletions test/plots/availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function() {
data,
Plot.binX(
{
filter: null,
transform: bin => bin,
y: "sum"
},
{
Expand All @@ -27,7 +27,7 @@ export default async function() {
data,
Plot.binX(
{
filter: null,
transform: bin => bin,
y: d => d.length ? d3.sum(d) : null
},
{
Expand Down

0 comments on commit acb9bb8

Please sign in to comment.