@@ -9,11 +9,13 @@ import {
9
9
arrayify ,
10
10
isDomainSort ,
11
11
isScaleOptions ,
12
+ isTypedArray ,
12
13
keyword ,
13
14
map ,
14
15
maybeNamed ,
15
16
range ,
16
17
second ,
18
+ slice ,
17
19
where ,
18
20
yes
19
21
} from "./options.js" ;
@@ -686,9 +688,42 @@ export class Mark {
686
688
}
687
689
initialize ( facets , facetChannels ) {
688
690
let data = arrayify ( this . data ) ;
691
+ let channels = this . channels ;
689
692
if ( facets === undefined && data != null ) facets = [ range ( data ) ] ;
690
- if ( this . transform != null ) ( { facets, data} = this . transform ( data , facets ) ) , ( data = arrayify ( data ) ) ;
691
- const channels = Channels ( this . channels , data ) ;
693
+
694
+ if ( this . transform != null ) {
695
+ // If the mark has a transform, reindex facets that overlap
696
+ const overlap = new Set ( ) ;
697
+ const reindex = new Map ( ) ;
698
+ let j = data . length ;
699
+ for ( const facet of facets ) {
700
+ for ( let k = 0 ; k < facet . length ; ++ k ) {
701
+ const i = facet [ k ] ;
702
+ if ( overlap . has ( i ) ) {
703
+ facet [ k ] = j ;
704
+ reindex . set ( j , i ) ;
705
+ ++ j ;
706
+ }
707
+ overlap . add ( i ) ;
708
+ }
709
+ }
710
+ // If necessary, expand data and any channel defined as an array
711
+ if ( reindex . size > 0 ) {
712
+ data = expandArray ( data , data . length + reindex . size ) ;
713
+ for ( const [ j , i ] of reindex ) data [ j ] = data [ i ] ;
714
+ for ( const key in channels ) {
715
+ const A = channels [ key ] . value ;
716
+ if ( Array . isArray ( A ) || isTypedArray ( A ) ) {
717
+ channels [ key ] . value = ( _ , i ) => A [ reindex . has ( i ) ? reindex . get ( i ) : i ] ;
718
+ }
719
+ }
720
+ }
721
+
722
+ ( { facets, data} = this . transform ( data , facets ) ) ;
723
+ data = arrayify ( data ) ;
724
+ }
725
+
726
+ channels = Channels ( channels , data ) ;
692
727
if ( this . sort != null ) channelDomain ( channels , facetChannels , data , this . sort ) ;
693
728
return { data, facets, channels} ;
694
729
}
@@ -869,3 +904,13 @@ class FacetMap2 extends FacetMap {
869
904
return this ;
870
905
}
871
906
}
907
+
908
+ // expands an array or typed array to make room for n values
909
+ function expandArray ( values , n ) {
910
+ if ( isTypedArray ( values ) ) {
911
+ const d = new values . constructor ( n ) ;
912
+ d . set ( values ) ;
913
+ return d ;
914
+ }
915
+ return slice ( values ) ;
916
+ }
0 commit comments