Skip to content

Commit e7001f9

Browse files
committed
group[XY] for each [yx]
1 parent b3aabc7 commit e7001f9

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/transforms/group.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import {group as grouper, sort, InternSet} from "d3";
22
import {defined, firstof} from "../defined.js";
33
import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, identity, take, maybeTuple} from "../mark.js";
44

5-
export function groupX({x, ...options} = {}) {
6-
const [transform, X, y] = group1(x, options);
7-
return {x: X, y, ...transform};
5+
// Group on y, z, fill, or stroke, if any, then group on x.
6+
export function groupX({x, y, out = y == null ? "y" : "fill", ...options} = {}) {
7+
const [transform, X, l] = group1(x, "y", {y, ...options});
8+
return {x: X, ...transform, [out]: l};
89
}
910

10-
export function groupY({y, ...options} = {}) {
11-
const [transform, Y, x] = group1(y, options);
12-
return {y: Y, x, ...transform};
11+
// Group on x, z, fill, or stroke, if any, then group on y.
12+
export function groupY({y, x, out = x == null ? "x" : "fill", ...options} = {}) {
13+
const [transform, Y, l] = group1(y, "x", {x, ...options});
14+
return {y: Y, ...transform, [out]: l};
1315
}
1416

17+
// Group on z, fill, or stroke, if any.
1518
export function groupR(options) {
1619
return group({...options, out: "r"});
1720
}
@@ -21,35 +24,39 @@ export function group({x, y, out = "fill", ...options} = {}) {
2124
return {x: X, y: Y, ...transform, [out]: L};
2225
}
2326

24-
function group1(x = identity, {domain, normalize, z, fill, stroke, ...options} = {}) {
25-
const k = normalize === true || normalize === "z" ? 100 : +normalize;
27+
function group1(x = identity, key, {[key]: k, domain, normalize, z, fill, stroke, ...options} = {}) {
28+
const m = normalize === true || normalize === "z" ? 100 : +normalize;
2629
const [X, setX] = lazyChannel(x);
27-
const [Y, setY] = lazyChannel(`Frequency${k === 100 ? " (%)" : ""}`);
28-
const [Z, setZ] = maybeLazyChannel(z);
30+
const [L, setL] = lazyChannel(`Frequency${m === 100 ? " (%)" : ""}`);
2931
const [vfill] = maybeColor(fill);
3032
const [vstroke] = maybeColor(stroke);
31-
const [F = fill, setF] = maybeLazyChannel(vfill);
32-
const [S = stroke, setS] = maybeLazyChannel(vstroke);
33+
const [BK, setBK] = maybeLazyChannel(k);
34+
const [BZ, setBZ] = maybeLazyChannel(z);
35+
const [BF = fill, setBF] = maybeLazyChannel(vfill);
36+
const [BS = stroke, setBS] = maybeLazyChannel(vstroke);
3337
const defined = maybeDomain(domain);
3438
return [
3539
{
36-
z: Z,
37-
fill: F,
38-
stroke: S,
40+
...key && {[key]: BK},
41+
z: BZ,
42+
fill: BF,
43+
stroke: BS,
3944
...options,
4045
transform: maybeTransform(options, (data, facets) => {
4146
const X = valueof(data, x);
47+
const K = valueof(data, k);
4248
const Z = valueof(data, z);
4349
const F = valueof(data, vfill);
4450
const S = valueof(data, vstroke);
4551
const groupFacets = [];
4652
const groupData = [];
47-
const G = firstof(Z, F, S);
4853
const BX = setX([]);
49-
const BY = setY([]);
50-
const BZ = Z && setZ([]);
51-
const BF = F && setF([]);
52-
const BS = S && setS([]);
54+
const L = setL([]);
55+
const G = firstof(K, Z, F, S);
56+
const BK = K && setBK([]);
57+
const BZ = Z && setBZ([]);
58+
const BF = F && setBF([]);
59+
const BS = S && setBS([]);
5360
let n = data.length;
5461
let i = 0;
5562
for (const facet of facets) {
@@ -62,7 +69,8 @@ function group1(x = identity, {domain, normalize, z, fill, stroke, ...options} =
6269
groupFacet.push(i++);
6370
groupData.push(take(data, f));
6471
BX.push(x);
65-
BY.push(k ? l * k / n : l);
72+
L.push(m ? l * m / n : l);
73+
if (K) BK.push(K[f[0]]);
6674
if (Z) BZ.push(Z[f[0]]);
6775
if (F) BF.push(F[f[0]]);
6876
if (S) BS.push(S[f[0]]);
@@ -74,7 +82,7 @@ function group1(x = identity, {domain, normalize, z, fill, stroke, ...options} =
7482
})
7583
},
7684
X,
77-
Y
85+
L
7886
];
7987
}
8088

0 commit comments

Comments
 (0)