1
- import { group as grouper , sort , sum , InternSet } from "d3" ;
2
- import { defined } from "../defined.js" ;
3
- import { valueof , maybeZ , maybeInput , maybeTransform , maybeValue , maybeLazyChannel , lazyChannel , first , identity , take , maybeTuple , labelof , maybeColor } from "../mark.js" ;
1
+ import { group as grouper , sort , sum } from "d3" ;
2
+ import { valueof , maybeColor , maybeInput , maybeTransform , maybeTuple , maybeLazyChannel , lazyChannel , first , identity , take } from "../mark.js" ;
4
3
5
4
// Group on {z, fill, stroke}.
6
5
export function groupZ ( outputs , options ) {
@@ -27,25 +26,22 @@ export function group(outputs, options = {}) {
27
26
}
28
27
29
28
function groupn (
30
- x , // optionally group on x (either a value or {value, domain})
31
- y , // optionally group on y (either a value or {value, domain})
29
+ x , // optionally group on x
30
+ y , // optionally group on y
32
31
{
33
32
data : reduceData = reduceIdentity ,
34
33
...outputs
35
34
} = { } , // channels to aggregate
36
35
{
37
36
domain,
38
37
// normalize, TODO
39
- // weight, TODO
38
+ z,
39
+ fill,
40
+ stroke,
40
41
...options
41
42
} = { }
42
43
) {
43
44
44
- // Implicit firsts.
45
- if ( outputs . z === undefined && options . z != null ) outputs = { ...outputs , z : reduceFirst } ;
46
- if ( outputs . fill === undefined && maybeColor ( options . fill ) [ 0 ] ) outputs = { ...outputs , fill : reduceFirst } ;
47
- if ( outputs . stroke === undefined && maybeColor ( options . stroke ) [ 0 ] ) outputs = { ...outputs , stroke : reduceFirst } ;
48
-
49
45
// Reconstitute the outputs.
50
46
outputs = Object . entries ( outputs ) . map ( ( [ name , reduce ] ) => {
51
47
const reducer = maybeReduce ( reduce ) ;
@@ -66,32 +62,19 @@ function groupn(
66
62
} ;
67
63
} ) ;
68
64
69
- // Handle per-dimension domains.
70
- // TODO This should be derived from the scale’s domain instead.
71
- // let xdomain, ydomain;
72
- // ({value: x, domain: xdomain} = {domain, ...maybeValue(x)});
73
- // ({value: y, domain: ydomain} = {domain, ...maybeValue(y)});
74
-
75
- // Handle both x and y being undefined.
76
- // TODO Move to group? Needs to handle per-dimension domain with default.
77
- // ([x, y] = maybeTuple(x, y));
78
-
79
- // Determine the z dimension (subgroups within x and y), if any. Note that
80
- // this requires that the z dimension be defined deterministically.
81
- const z = maybeZ ( options ) ;
82
-
83
65
// const m = maybeNormalize(normalize); // TODO
84
66
// const [BL, setBL] = lazyChannel(`${labelof(weight, "Frequency")}${m === 100 ? " (%)" : ""}`);
85
67
const [ BX , setBX ] = maybeLazyChannel ( x ) ;
86
68
const [ BY , setBY ] = maybeLazyChannel ( y ) ;
87
- // const [BZ, setBZ] = maybeLazyChannel(z);
88
- // const [vfill] = maybeColor(fill);
89
- // const [vstroke] = maybeColor(stroke);
90
- // const [BF = fill, setBF] = maybeLazyChannel(vfill);
91
- // const [BS = stroke, setBS] = maybeLazyChannel(vstroke);
92
- const xdefined = defined1 ; // TODO BX && maybeDomain(xdomain);
93
- const ydefined = defined1 ; // TODO BY && maybeDomain(ydomain);
69
+ const [ BZ , setBZ ] = maybeLazyChannel ( z ) ;
70
+ const [ vfill ] = maybeColor ( fill ) ;
71
+ const [ vstroke ] = maybeColor ( stroke ) ;
72
+ const [ BF = fill , setBF ] = maybeLazyChannel ( vfill ) ;
73
+ const [ BS = stroke , setBS ] = maybeLazyChannel ( vstroke ) ;
94
74
return {
75
+ z : BZ ,
76
+ fill : BF ,
77
+ stroke : BS ,
95
78
...options ,
96
79
...BX && { x : BX } ,
97
80
...BY && { y : BY } ,
@@ -100,15 +83,17 @@ function groupn(
100
83
const X = valueof ( data , x ) ;
101
84
const Y = valueof ( data , y ) ;
102
85
const Z = valueof ( data , z ) ;
86
+ const F = valueof ( data , vfill ) ;
87
+ const S = valueof ( data , vstroke ) ;
103
88
// const W = valueof(data, weight); // TODO
104
89
const groupFacets = [ ] ;
105
90
const groupData = [ ] ;
106
91
// const BL = setBL([]);
107
92
const BX = X && setBX ( [ ] ) ;
108
93
const BY = Y && setBY ( [ ] ) ;
109
- // const BZ = Z && setBZ([]);
110
- // const BF = F && setBF([]);
111
- // const BS = S && setBS([]);
94
+ const BZ = Z && setBZ ( [ ] ) ;
95
+ const BF = F && setBF ( [ ] ) ;
96
+ const BS = S && setBS ( [ ] ) ;
112
97
// let n = W ? sum(W) : data.length; // TODO
113
98
let i = 0 ;
114
99
for ( const output of outputs ) {
@@ -117,10 +102,10 @@ function groupn(
117
102
for ( const facet of facets ) {
118
103
const groupFacet = [ ] ;
119
104
// if (normalize === "facet") n = W ? sum(facet, i => W[i]) : facet.length; // TODO
120
- for ( const [ , I ] of groups ( facet , Z , defined1 ) ) {
105
+ for ( const [ , I ] of groups ( facet , Z ) ) {
121
106
// if (normalize === "z") n = W ? sum(I, i => W[i]) : I.length; // TODO
122
- for ( const [ y , fy ] of groups ( I , Y , ydefined ) ) {
123
- for ( const [ x , f ] of groups ( fy , X , xdefined ) ) {
107
+ for ( const [ y , fy ] of groups ( I , Y ) ) {
108
+ for ( const [ x , f ] of groups ( fy , X ) ) {
124
109
// const l = W ? sum(f, i => W[i]) : f.length; // TODO
125
110
groupFacet . push ( i ++ ) ;
126
111
groupData . push ( reduceData . reduce ( f , data ) ) ;
@@ -130,9 +115,9 @@ function groupn(
130
115
for ( const output of outputs ) {
131
116
output . reduce ( f ) ;
132
117
}
133
- // if (Z) BZ.push(Z[f[0]]);
134
- // if (F) BF.push(F[f[0]]);
135
- // if (S) BS.push(S[f[0]]);
118
+ if ( Z ) BZ . push ( Z [ f [ 0 ] ] ) ;
119
+ if ( F ) BF . push ( F [ f [ 0 ] ] ) ;
120
+ if ( S ) BS . push ( S [ f [ 0 ] ] ) ;
136
121
}
137
122
}
138
123
}
@@ -143,13 +128,6 @@ function groupn(
143
128
} ;
144
129
}
145
130
146
- function maybeDomain ( domain ) {
147
- if ( domain === undefined ) return defined1 ;
148
- if ( domain === null ) return ( ) => false ;
149
- domain = new InternSet ( domain ) ;
150
- return ( [ key ] ) => domain . has ( key ) ;
151
- }
152
-
153
131
// function maybeNormalize(normalize) {
154
132
// if (!normalize) return;
155
133
// if (normalize === true) return 100;
@@ -160,12 +138,8 @@ function maybeDomain(domain) {
160
138
// throw new Error("invalid normalize");
161
139
// }
162
140
163
- function defined1 ( [ key ] ) {
164
- return defined ( key ) ;
165
- }
166
-
167
- export function groups ( I , X , defined = defined1 ) {
168
- return X ? sort ( grouper ( I , i => X [ i ] ) , first ) . filter ( defined ) : [ [ , I ] ] ;
141
+ export function groups ( I , X ) {
142
+ return X ? sort ( grouper ( I , i => X [ i ] ) , first ) : [ [ , I ] ] ;
169
143
}
170
144
171
145
function maybeReduce ( reduce ) {
0 commit comments