Skip to content

Commit

Permalink
consistent groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 23, 2021
1 parent 98bfc38 commit c973c69
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {bin as binner, cross, group, sum} from "d3";
import {firstof} from "../defined.js";
import {valueof, first, second, range, identity, lazyChannel, maybeLazyChannel, maybeTransform, maybeColor, maybeValue, mid, take, labelof} from "../mark.js";
import {offset} from "../style.js";
import {groups} from "./group.js";

// Group on y, z, fill, or stroke, if any, then bin on x.
export function binX({x, y, out = y == null ? "y" : "fill", inset, insetLeft, insetRight, ...options} = {}) {
Expand Down Expand Up @@ -162,7 +163,7 @@ function bin2(x, y, {weight, domain, thresholds, normalize, z, fill, stroke, ...
let i = 0;
for (const facet of facets) {
const binFacet = [];
for (const I of G ? group(facet, i => G[i]).values() : [facet]) {
for (const [, I] of groups(facet, G)) {
if (normalize === "z") n = W ? sum(I, i => W[i]) : I.length;
const set = new Set(I);
for (const b of B) {
Expand Down
18 changes: 11 additions & 7 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
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 G ? grouper(facet, i => G[i]).values() : [facet]) {
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 Y ? sort(grouper(I, i => Y[i]), first).filter(ydefined) : [[, I]]) {
for (const [x, f] of X ? sort(grouper(fy, i => X[i]), first).filter(xdefined) : [[, fy]]) {
for (const [y, fy] of groups(I, Y, ydefined)) {
for (const [x, f] of groups(fy, X, xdefined)) {
const l = W ? sum(f, i => W[i]) : f.length;
groupFacet.push(i++);
groupData.push(take(data, f));
Expand Down Expand Up @@ -119,10 +119,6 @@ function maybeDomain(domain) {
return ([key]) => domain.has(key);
}

function defined1([key]) {
return defined(key);
}

function maybeNormalize(normalize) {
if (!normalize) return;
if (normalize === true) return 100;
Expand All @@ -132,3 +128,11 @@ 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]];
}
34 changes: 12 additions & 22 deletions test/output/penguinSpeciesGroup.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/penguinSpeciesIsland.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions test/plots/penguin-species-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import * as d3 from "d3";
export default async function() {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
x: {
grid: true
},
marks: [
Plot.barX(penguins, Plot.stackX(Plot.groupZX({fill: "species", normalize: true}))),
Plot.text(penguins, Plot.stackXMid(Plot.groupZX({z: "species", normalize: true, text: ([d]) => d.species}))),
Plot.ruleX([0, 100])
]
});
Expand Down

0 comments on commit c973c69

Please sign in to comment.