Skip to content

sort bins #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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, reduceIdentity} from "./group.js";
import {extractOutputs, maybeGroup, maybeOutputs, maybeReduce, maybeSort, 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 @@ -33,7 +33,7 @@ function binn(
by, // optionally bin on y (exclusive with gy)
gx, // optionally group on x (exclusive with bx and gy)
gy, // optionally group on y (exclusive with by and gx)
{data: reduceData = reduceIdentity, ...outputs} = {}, // output channel definitions
{data: reduceData = reduceIdentity, reverse, ...outputs} = {}, // output channel definitions
inputs = {} // input channels and options
) {
bx = maybeBin(bx);
Expand Down Expand Up @@ -116,12 +116,13 @@ function binn(
}
groupFacets.push(groupFacet);
}
maybeSort(groupFacets, outputs, reverse);
return {data: groupData, facets: groupFacets};
}),
...BX1 ? {x1: BX1, x2: BX2, x: mid(BX1, BX2)} : {x},
...BY1 ? {y1: BY1, y2: BY2, y: mid(BY1, BY2)} : {y},
...GK && {[gk]: GK},
...Object.fromEntries(outputs.map(({name, output}) => [name, output]))
...extractOutputs(outputs)
};
}

Expand Down
25 changes: 22 additions & 3 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet} from "d3";
import {firstof} from "../defined.js";
import {ascendingDefined, firstof} from "../defined.js";
import {valueof, maybeColor, maybeInput, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range} from "../mark.js";
import {basic} from "./basic.js";

Expand Down Expand Up @@ -34,7 +34,7 @@ export function group(outputs, options = {}) {
function groupn(
x, // optionally group on x
y, // optionally group on y
{data: reduceData = reduceIdentity, ...outputs} = {}, // output channel definitions
{data: reduceData = reduceIdentity, reverse, ...outputs} = {}, // output channel definitions
inputs = {} // input channels and options
) {
reduceData = maybeReduce(reduceData, identity);
Expand Down Expand Up @@ -93,11 +93,12 @@ function groupn(
}
groupFacets.push(groupFacet);
}
maybeSort(groupFacets, outputs, reverse);
return {data: groupData, facets: groupFacets};
}),
...GX && {x: GX},
...GY && {y: GY},
...Object.fromEntries(outputs.map(({name, output}) => [name, output]))
...extractOutputs(outputs)
};
}

Expand Down Expand Up @@ -163,6 +164,24 @@ export function maybeSubgroup(outputs, Z, F, S) {
);
}

export function maybeSort(facets, outputs, reverse) {
const sort = outputs.find(o => o.name === "sort");
if (sort) {
const S = sort.output.transform();
const compare = (i, j) => ascendingDefined(S[i], S[j]);
facets.forEach(f => f.sort(compare));
}
if (reverse) {
facets.forEach(f => f.reverse());
}
}

export function extractOutputs(outputs) {
return Object.fromEntries(outputs
.filter(({name}) => name !== "sort")
.map(({name, output}) => [name, output]));
}

function reduceFunction(f) {
return {
reduce(I, X) {
Expand Down
Loading