@@ -61,12 +61,6 @@ export function plot(options = {}) {
61
61
autoScaleLabels ( scaleChannels , scaleDescriptors , axes , dimensions , options ) ;
62
62
autoAxisTicks ( scaleDescriptors , axes ) ;
63
63
64
- // When faceting, render axes for fx and fy instead of x and y.
65
- const x = facet !== undefined && scales . fx ? "fx" : "x" ;
66
- const y = facet !== undefined && scales . fy ? "fy" : "y" ;
67
- if ( axes [ x ] ) marks . unshift ( axes [ x ] ) ;
68
- if ( axes [ y ] ) marks . unshift ( axes [ y ] ) ;
69
-
70
64
const { width, height} = dimensions ;
71
65
72
66
const svg = create ( "svg" )
@@ -96,11 +90,17 @@ export function plot(options = {}) {
96
90
. call ( applyInlineStyles , style )
97
91
. node ( ) ;
98
92
93
+ // When faceting, render axes for fx and fy instead of x and y.
94
+ const axisX = axes [ facet !== undefined && scales . fx ? "fx" : "x" ] ;
95
+ const axisY = axes [ facet !== undefined && scales . fy ? "fy" : "y" ] ;
96
+ if ( axisY ) svg . appendChild ( axisY . render ( null , scales , dimensions ) ) ;
97
+ if ( axisX ) svg . appendChild ( axisX . render ( null , scales , dimensions ) ) ;
98
+
99
+ // Render marks.
99
100
for ( const mark of marks ) {
100
- const channels = markChannels . get ( mark ) ?? [ ] ;
101
+ const channels = markChannels . get ( mark ) ;
101
102
const values = applyScales ( channels , scales ) ;
102
- let index = markIndex . get ( mark ) ;
103
- if ( mark . filter != null ) index = mark . filter ( index , channels , values ) ;
103
+ const index = mark . filter ( markIndex . get ( mark ) , channels , values ) ;
104
104
const node = mark . render ( index , scales , values , dimensions , axes ) ;
105
105
if ( node != null ) svg . appendChild ( node ) ;
106
106
}
@@ -138,16 +138,6 @@ export function plot(options = {}) {
138
138
return figure ;
139
139
}
140
140
141
- function defaultFilter ( index , channels , values ) {
142
- for ( const [ name , { filter = defined } ] of channels ) {
143
- if ( name !== undefined && filter !== null ) {
144
- const value = values [ name ] ;
145
- index = index . filter ( i => filter ( value [ i ] ) ) ;
146
- }
147
- }
148
- return index ;
149
- }
150
-
151
141
export class Mark {
152
142
constructor ( data , channels = [ ] , options = { } , defaults ) {
153
143
const { facet = "auto" , sort, dx, dy, clip} = options ;
@@ -156,7 +146,6 @@ export class Mark {
156
146
this . sort = isOptions ( sort ) ? sort : null ;
157
147
this . facet = facet == null || facet === false ? null : keyword ( facet === true ? "include" : facet , "facet" , [ "auto" , "include" , "exclude" ] ) ;
158
148
const { transform} = basic ( options ) ;
159
- this . filter = defaults ?. filter === undefined ? defaultFilter : defaults . filter ;
160
149
this . transform = transform ;
161
150
if ( defaults !== undefined ) channels = styles ( this , options , channels , defaults ) ;
162
151
this . channels = channels . filter ( channel => {
@@ -193,6 +182,15 @@ export class Mark {
193
182
if ( this . sort != null ) channelSort ( channels , facetChannels , data , this . sort ) ;
194
183
return { index, channels} ;
195
184
}
185
+ filter ( index , channels , values ) {
186
+ for ( const [ name , { filter = defined } ] of channels ) {
187
+ if ( name !== undefined && filter !== null ) {
188
+ const value = values [ name ] ;
189
+ index = index . filter ( i => filter ( value [ i ] ) ) ;
190
+ }
191
+ }
192
+ return index ;
193
+ }
196
194
plot ( { marks = [ ] , ...options } = { } ) {
197
195
return plot ( { ...options , marks : [ ...marks , this ] } ) ;
198
196
}
@@ -240,16 +238,14 @@ class Facet extends Mark {
240
238
this . marksIndexByFacet = undefined ; // map from facet key to array of mark indexes
241
239
}
242
240
initialize ( ) {
243
- const { index, channels} = super . initialize ( ) ;
244
- const facets = index === undefined ? [ ] : facetGroups ( index , channels ) ;
241
+ const { index, channels : facetChannels } = super . initialize ( ) ;
242
+ const facets = index === undefined ? [ ] : facetGroups ( index , facetChannels ) ;
245
243
const facetsKeys = Array . from ( facets , first ) ;
246
244
const facetsIndex = Array . from ( facets , second ) ;
247
- const subchannels = [ ] ;
245
+ const channels = facetChannels . slice ( ) ;
248
246
const marksChannels = this . marksChannels = [ ] ;
249
- const marksIndexByFacet = this . marksIndexByFacet = facetMap ( channels ) ;
250
- for ( const facetKey of facetsKeys ) {
251
- marksIndexByFacet . set ( facetKey , new Array ( this . marks . length ) ) ;
252
- }
247
+ const marksIndexByFacet = this . marksIndexByFacet = facetMap ( facetChannels ) ;
248
+ for ( const key of facetsKeys ) marksIndexByFacet . set ( key , new Array ( this . marks . length ) ) ;
253
249
let facetsExclude ;
254
250
for ( let i = 0 ; i < this . marks . length ; ++ i ) {
255
251
const mark = this . marks [ i ] ;
@@ -258,30 +254,31 @@ class Facet extends Mark {
258
254
: facet === "include" ? facetsIndex
259
255
: facet === "exclude" ? facetsExclude || ( facetsExclude = facetsIndex . map ( f => Uint32Array . from ( difference ( index , f ) ) ) )
260
256
: undefined ;
261
- const { index : I , channels : markChannels } = mark . initialize ( markFacets , channels ) ;
257
+ const { index : markIndex , channels : markChannels } = mark . initialize ( markFacets , facetChannels ) ;
262
258
// If an index is returned by mark.initialize, its structure depends on
263
259
// whether or not faceting has been applied: it is a flat index ([0, 1, 2,
264
260
// …]) when not faceted, and a nested index ([[0, 1, …], [2, 3, …], …])
265
261
// when faceted.
266
- if ( I !== undefined ) {
262
+ if ( markIndex !== undefined ) {
267
263
if ( markFacets ) {
268
264
for ( let j = 0 ; j < facetsKeys . length ; ++ j ) {
269
- marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = I [ j ] ;
265
+ marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = markIndex [ j ] ;
270
266
}
271
267
} else {
272
268
for ( let j = 0 ; j < facetsKeys . length ; ++ j ) {
273
- marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = I ;
269
+ marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = markIndex ;
274
270
}
275
271
}
276
272
}
277
- for ( const [ , channel ] of markChannels ) {
278
- subchannels . push ( [ , channel ] ) ;
279
- }
273
+ for ( const [ , channel ] of markChannels ) channels . push ( [ , channel ] ) ; // anonymize channels
280
274
marksChannels . push ( markChannels ) ;
281
275
}
282
- return { index, channels : [ ...channels , ...subchannels ] } ;
276
+ return { index, channels} ;
277
+ }
278
+ filter ( index ) {
279
+ return index ;
283
280
}
284
- render ( I , scales , _ , dimensions , axes ) {
281
+ render ( index , scales , values , dimensions , axes ) {
285
282
const { marks, marksChannels, marksIndexByFacet} = this ;
286
283
const { fx, fy} = scales ;
287
284
const fyDomain = fy && fy . domain ( ) ;
@@ -303,7 +300,6 @@ class Facet extends Mark {
303
300
. append ( ( ky , i ) => ( i === j ? axis1 : axis2 ) . render (
304
301
fx && where ( fxDomain , kx => marksIndexByFacet . has ( [ kx , ky ] ) ) ,
305
302
scales ,
306
- null ,
307
303
fyDimensions
308
304
) ) ;
309
305
}
@@ -319,7 +315,6 @@ class Facet extends Mark {
319
315
. append ( ( kx , i ) => ( i === j ? axis1 : axis2 ) . render (
320
316
fy && where ( fyDomain , ky => marksIndexByFacet . has ( [ kx , ky ] ) ) ,
321
317
scales ,
322
- null ,
323
318
fxDimensions
324
319
) ) ;
325
320
}
@@ -333,8 +328,7 @@ class Facet extends Mark {
333
328
for ( let i = 0 ; i < marks . length ; ++ i ) {
334
329
const mark = marks [ i ] ;
335
330
const values = marksValues [ i ] ;
336
- let index = marksFacetIndex [ i ] ;
337
- if ( mark . filter != null ) index = mark . filter ( index , marksChannels [ i ] , values ) ;
331
+ const index = mark . filter ( marksFacetIndex [ i ] , marksChannels [ i ] , values ) ;
338
332
const node = mark . render ( index , scales , values , subdimensions ) ;
339
333
if ( node != null ) this . appendChild ( node ) ;
340
334
}
0 commit comments