Skip to content
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
11 changes: 7 additions & 4 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ function group2(xv, yv, {z, fill, stroke, weight, domain, normalize, ...options}
if (normalize === "facet") n = W ? sum(facet, i => W[i]) : facet.length;
for (const [, I] of groups(facet, G, defined1)) {
if (normalize === "z") n = W ? sum(I, i => W[i]) : I.length;
for (const [y, fy] of groups(I, Y, ydefined)) {
for (const [x, f] of groups(fy, X, xdefined)) {
for (const [y, fy] of groups(I, Y, ydefined, ydomain)) {
for (const [x, f] of groups(fy, X, xdefined, xdomain)) {
const l = W ? sum(f, i => W[i]) : f.length;
groupFacet.push(i++);
groupData.push(take(data, f));
Expand Down Expand Up @@ -133,6 +133,9 @@ function defined1([key]) {
return defined(key);
}

export function groups(I, X, defined = defined1) {
return X ? sort(grouper(I, i => X[i]), first).filter(defined) : [[, I]];
export function groups(I, X, defined = defined1, domain) {
if (!X) return [[, I]];
const G = grouper(I, i => X[i]);
return domain ? domain.map(x => [x, G.has(x) ? G.get(x) : []])
: sort(G, first).filter(defined);
}
36 changes: 36 additions & 0 deletions test/transforms/group-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as Plot from "@observablehq/plot";
import tape from "tape-await";

tape("groupX respects the domain option (#255)", test => {
const data = ["A", "A", "C"];
const options = {x: d => d, domain: ["C", "B", "A"]};
const mark = Plot.dot(data, Plot.groupX(options));
const A = mark.initialize();
test.deepEqual(A.index, [0, 1, 2]);
test.deepEqual(A.channels.find(d => d[0] === "x")[1].value, ["C", "B", "A"]);
test.deepEqual(A.channels.find(d => d[0] === "y")[1].value, [1, 0, 2]);
});

tape("groupY respects the domain option (#255)", test => {
const data = ["A", "A", "C"];
const options = {x: d => d, domain: ["C", "B", "A"]};
const mark = Plot.dot(data, Plot.groupY(options));
const A = mark.initialize();
test.deepEqual(A.index, [0, 1, 2]);
test.deepEqual(A.channels.find(d => d[0] === "y")[1].value, ["C", "B", "A"]);
test.deepEqual(A.channels.find(d => d[0] === "x")[1].value, [1, 0, 2]);
});

tape("group respects the domain option (#255)", test => {
const data = ["A", "A", "C", "A", "C"];
const options = {x: d => d, y: d => d, domain: ["C", "B", "A"]};
const mark = Plot.dot(data, Plot.group(options));
const A = mark.initialize();
test.deepEqual(A.index, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
test.deepEqual(A.channels.find(d => d[0] === "fill")[1].value, [
//C, B, A
2, 0, 0, // C
0, 0, 0, // B
0, 0, 3 // A
]);
});
2 changes: 1 addition & 1 deletion test/transforms/reduce-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Plot from "@observablehq/plot";
import tape from "tape-await";

tape("baked-in reducer reduce as expected", test => {
tape("baked-in reducers reduce as expected", test => {
const data = [0, 1, 2, 4, 5, 9];
testReducer(test, data, "deviation", Math.sqrt(10.7));
testReducer(test, data, "max", 9);
Expand Down