Skip to content
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

fix group domain #271

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
31 changes: 20 additions & 11 deletions src/marks/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export class AbstractBar extends Mark {
const {rx, ry} = this;
const {color} = scales;
const {z: Z, title: L, fill: F, stroke: S} = channels;
const index = filter(I, ...this._positions(channels), F, S);
const [X, vx] = maybeCoords(this._x(scales, channels, dimensions), I);
const [Y, vy] = maybeCoords(this._y(scales, channels, dimensions), I);
const [W, vw] = maybeCoords(this._width(scales, channels, dimensions), I);
const [H, vh] = maybeCoords(this._height(scales, channels, dimensions), I);
const index = filter(I, X, Y, W, H, F, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand All @@ -57,10 +61,10 @@ export class AbstractBar extends Mark {
.data(index)
.join("rect")
.call(applyDirectStyles, this)
.attr("x", this._x(scales, channels, dimensions))
.attr("width", this._width(scales, channels, dimensions))
.attr("y", this._y(scales, channels, dimensions))
.attr("height", this._height(scales, channels, dimensions))
.attr("x", X ? i => X[i] : vx)
.attr("width", W ? i => W[i] : vw)
.attr("y", Y ? i => Y[i] : vy)
.attr("height", H ? i => H[i] : vh)
.attr("fill", F && (i => color(F[i])))
.attr("stroke", S && (i => color(S[i])))
.call(rx != null ? rect => rect.attr("rx", rx) : () => {})
Expand Down Expand Up @@ -88,6 +92,17 @@ export class AbstractBar extends Mark {
}
}

function maybeCoords(x, I) {
if (typeof x === "function") {
const X = [];
for (const i of I) {
X[i] = x(i);
}
return [X, undefined];
}
return [undefined, x];
}

export class BarX extends AbstractBar {
constructor(data, {x1, x2, y, ...options} = {}) {
super(
Expand All @@ -103,9 +118,6 @@ export class BarX extends AbstractBar {
_transform(selection, {x}) {
selection.call(applyTransform, x, null);
}
_positions({x1: X1, x2: X2, y: Y}) {
return [X1, X2, Y];
}
_x({x}, {x1: X1, x2: X2}) {
const {insetLeft} = this;
return i => Math.min(x(X1[i]), x(X2[i])) + insetLeft;
Expand All @@ -131,9 +143,6 @@ export class BarY extends AbstractBar {
_transform(selection, {y}) {
selection.call(applyTransform, null, y);
}
_positions({y1: Y1, y2: Y2, x: X}) {
return [Y1, Y2, X];
}
_y({y}, {y1: Y1, y2: Y2}) {
const {insetTop} = this;
return i => Math.min(y(Y1[i]), y(Y2[i])) + insetTop;
Expand Down
22 changes: 11 additions & 11 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, InternSet} from "d3";
import {defined, firstof} from "../defined.js";
import {firstof} from "../defined.js";
import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, identity, take, maybeTuple, labelof} from "../mark.js";

// Group on {z, fill, stroke}.
Expand Down Expand Up @@ -85,10 +85,10 @@ function group2(xv, yv, {z, fill, stroke, weight, domain, normalize, ...options}
for (const facet of facets) {
const groupFacet = [];
if (normalize === "facet") n = W ? sum(facet, i => W[i]) : facet.length;
for (const [, I] of groups(facet, G, defined1)) {
for (const [, I] of groups(facet, G)) {
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)) {
Comment on lines +90 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve been thinking about this a little more, and I think it makes more sense that the corresponding scale’s domain drive which groups are shown, rather than needing to pass the domain explicitly to the group transform. I think this means that we should remove any filtering here, and just group all the data, and then we simply won’t show the resulting marks (e.g., bar will filter it out using maybeCoords).

This now intersects with the “grand unification” of group and reduce I’ve been working on, so I’ll try to merge these two PRs.

const l = W ? sum(f, i => W[i]) : f.length;
groupFacet.push(i++);
groupData.push(take(data, f));
Expand All @@ -113,7 +113,7 @@ function group2(xv, yv, {z, fill, stroke, weight, domain, normalize, ...options}
}

function maybeDomain(domain) {
if (domain === undefined) return defined1;
if (domain === undefined) return () => true;
if (domain === null) return () => false;
domain = new InternSet(domain);
return ([key]) => domain.has(key);
Expand All @@ -129,10 +129,10 @@ function maybeNormalize(normalize) {
throw new Error("invalid normalize");
}

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, 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(defined ? Array.from(G).filter(defined) : G, first);
}
122 changes: 122 additions & 0 deletions test/output/penguinSpeciesIslandSex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export {default as penguinMassSpecies} from "./penguin-mass-species.js";
export {default as penguinSexMassCulmenSpecies} from "./penguin-sex-mass-culmen-species.js";
export {default as penguinSpeciesGroup} from "./penguin-species-group.js";
export {default as penguinSpeciesIsland} from "./penguin-species-island.js";
export {default as penguinSpeciesIslandSex} from "./penguin-species-island-sex.js";
export {default as policeDeaths} from "./police-deaths.js";
export {default as policeDeathsBar} from "./police-deaths-bar.js";
export {default as randomWalk} from "./random-walk.js";
Expand Down
29 changes: 29 additions & 0 deletions test/plots/penguin-species-island-sex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const data = await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
facet: {
data,
x: "species"
},
fx: {
domain: d3.groupSort(data, ({length}) => length, d => d.species).reverse()
},
x: {
domain: ["MALE", "FEMALE", null],
tickFormat: d => d === null ? "N/A" : d
},
y: {
grid: true
},
color: {
scheme: "greys"
},
marks: [
Plot.barY(data, Plot.stackY(Plot.groupX({x: "sex", fill: "island", stroke: "black"}))),
Plot.ruleY([0])
]
});
}
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
]);
});