1
1
import { cross , difference , groups , InternMap } from "d3" ;
2
2
import { create } from "d3" ;
3
- import { Mark , first , second , markify } from "./mark.js" ;
3
+ import { Mark , first , second , markify , where } from "./mark.js" ;
4
4
import { applyScales } from "./scales.js" ;
5
5
import { filterStyles } from "./style.js" ;
6
6
@@ -24,7 +24,6 @@ class Facet extends Mark {
24
24
this . marks = marks . flat ( Infinity ) . map ( markify ) ;
25
25
// The following fields are set by initialize:
26
26
this . marksChannels = undefined ; // array of mark channels
27
- this . marksIndex = undefined ; // array of mark indexes (for non-faceted marks)
28
27
this . marksIndexByFacet = undefined ; // map from facet key to array of mark indexes
29
28
}
30
29
initialize ( ) {
@@ -34,7 +33,6 @@ class Facet extends Mark {
34
33
const facetsIndex = Array . from ( facets , second ) ;
35
34
const subchannels = [ ] ;
36
35
const marksChannels = this . marksChannels = [ ] ;
37
- const marksIndex = this . marksIndex = new Array ( this . marks . length ) ;
38
36
const marksIndexByFacet = this . marksIndexByFacet = facetMap ( channels ) ;
39
37
for ( const facetKey of facetsKeys ) {
40
38
marksIndexByFacet . set ( facetKey , new Array ( this . marks . length ) ) ;
@@ -57,12 +55,10 @@ class Facet extends Mark {
57
55
for ( let j = 0 ; j < facetsKeys . length ; ++ j ) {
58
56
marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = I [ j ] ;
59
57
}
60
- marksIndex [ i ] = [ ] ; // implicit empty index for sparse facets
61
58
} else {
62
59
for ( let j = 0 ; j < facetsKeys . length ; ++ j ) {
63
60
marksIndexByFacet . get ( facetsKeys [ j ] ) [ i ] = I ;
64
61
}
65
- marksIndex [ i ] = I ;
66
62
}
67
63
}
68
64
for ( const [ , channel ] of markChannels ) {
@@ -73,44 +69,54 @@ class Facet extends Mark {
73
69
return { index, channels : [ ...channels , ...subchannels ] } ;
74
70
}
75
71
render ( I , scales , channels , dimensions , axes ) {
76
- const { marks, marksChannels, marksIndex , marksIndexByFacet} = this ;
72
+ const { marks, marksChannels, marksIndexByFacet} = this ;
77
73
const { fx, fy} = scales ;
74
+ const fyDomain = fy && fy . domain ( ) ;
75
+ const fxDomain = fx && fx . domain ( ) ;
78
76
const fyMargins = fy && { marginTop : 0 , marginBottom : 0 , height : fy . bandwidth ( ) } ;
79
77
const fxMargins = fx && { marginRight : 0 , marginLeft : 0 , width : fx . bandwidth ( ) } ;
80
78
const subdimensions = { ...dimensions , ...fxMargins , ...fyMargins } ;
81
79
const marksValues = marksChannels . map ( channels => applyScales ( channels , scales ) ) ;
82
80
return create ( "svg:g" )
83
81
. call ( g => {
84
82
if ( fy && axes . y ) {
85
- const domain = fy . domain ( ) ;
86
83
const axis1 = axes . y , axis2 = nolabel ( axis1 ) ;
87
- const j = axis1 . labelAnchor === "bottom" ? domain . length - 1 : axis1 . labelAnchor === "center" ? domain . length >> 1 : 0 ;
84
+ const j = axis1 . labelAnchor === "bottom" ? fyDomain . length - 1 : axis1 . labelAnchor === "center" ? fyDomain . length >> 1 : 0 ;
88
85
const fyDimensions = { ...dimensions , ...fyMargins } ;
89
86
g . selectAll ( )
90
- . data ( domain )
87
+ . data ( fyDomain )
91
88
. join ( "g" )
92
89
. attr ( "transform" , ky => `translate(0,${ fy ( ky ) } )` )
93
- . append ( ( _ , i ) => ( i === j ? axis1 : axis2 ) . render ( null , scales , null , fyDimensions ) ) ;
90
+ . append ( ( ky , i ) => ( i === j ? axis1 : axis2 ) . render (
91
+ fx && where ( fxDomain , kx => marksIndexByFacet . has ( [ kx , ky ] ) ) ,
92
+ scales ,
93
+ null ,
94
+ fyDimensions
95
+ ) ) ;
94
96
}
95
97
if ( fx && axes . x ) {
96
- const domain = fx . domain ( ) ;
97
98
const axis1 = axes . x , axis2 = nolabel ( axis1 ) ;
98
- const j = axis1 . labelAnchor === "right" ? domain . length - 1 : axis1 . labelAnchor === "center" ? domain . length >> 1 : 0 ;
99
+ const j = axis1 . labelAnchor === "right" ? fxDomain . length - 1 : axis1 . labelAnchor === "center" ? fxDomain . length >> 1 : 0 ;
99
100
const { marginLeft, marginRight} = dimensions ;
100
101
const fxDimensions = { ...dimensions , ...fxMargins , labelMarginLeft : marginLeft , labelMarginRight : marginRight } ;
101
102
g . selectAll ( )
102
- . data ( domain )
103
+ . data ( fxDomain )
103
104
. join ( "g" )
104
105
. attr ( "transform" , kx => `translate(${ fx ( kx ) } ,0)` )
105
- . append ( ( _ , i ) => ( i === j ? axis1 : axis2 ) . render ( null , scales , null , fxDimensions ) ) ;
106
+ . append ( ( kx , i ) => ( i === j ? axis1 : axis2 ) . render (
107
+ fy && where ( fyDomain , ky => marksIndexByFacet . has ( [ kx , ky ] ) ) ,
108
+ scales ,
109
+ null ,
110
+ fxDimensions
111
+ ) ) ;
106
112
}
107
113
} )
108
114
. call ( g => g . selectAll ( )
109
- . data ( facetKeys ( scales ) )
115
+ . data ( facetKeys ( scales ) . filter ( marksIndexByFacet . has , marksIndexByFacet ) )
110
116
. join ( "g" )
111
117
. attr ( "transform" , facetTranslate ( fx , fy ) )
112
118
. each ( function ( key ) {
113
- const marksFacetIndex = marksIndexByFacet . get ( key ) || marksIndex ;
119
+ const marksFacetIndex = marksIndexByFacet . get ( key ) ;
114
120
for ( let i = 0 ; i < marks . length ; ++ i ) {
115
121
const values = marksValues [ i ] ;
116
122
const index = filterStyles ( marksFacetIndex [ i ] , values ) ;
0 commit comments