@@ -130,21 +130,27 @@ exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
130
130
var calcdata = gd . calcdata ;
131
131
var i ;
132
132
133
+ // Traces is a list of trace indices to (re)plot. If it's not provided,
134
+ // then it's a complete replot so we create a new list and add all trace indices
135
+ // which are in calcdata.
136
+
133
137
if ( ! Array . isArray ( traces ) ) {
134
138
// If traces is not provided, then it's a complete replot and missing
135
139
// traces are removed
136
140
traces = [ ] ;
137
141
for ( i = 0 ; i < calcdata . length ; i ++ ) traces . push ( i ) ;
138
142
}
139
143
144
+ // For each subplot
140
145
for ( i = 0 ; i < subplots . length ; i ++ ) {
141
146
var subplot = subplots [ i ] ;
142
147
var subplotInfo = fullLayout . _plots [ subplot ] ;
143
148
144
- // Get all calcdata for this subplot:
149
+ // Get all calcdata (traces) for this subplot:
145
150
var cdSubplot = [ ] ;
146
151
var pcd ;
147
152
153
+ // For each trace
148
154
for ( var j = 0 ; j < calcdata . length ; j ++ ) {
149
155
var cd = calcdata [ j ] ;
150
156
var trace = cd [ 0 ] . trace ;
@@ -178,7 +184,7 @@ exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
178
184
pcd = cd ;
179
185
}
180
186
}
181
-
187
+ // Plot the traces for this subplot
182
188
plotOne ( gd , subplotInfo , cdSubplot , transitionOpts , makeOnCompleteCallback ) ;
183
189
}
184
190
} ;
@@ -189,41 +195,60 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
189
195
var modules = fullLayout . _modules ;
190
196
var _module , cdModuleAndOthers , cdModule ;
191
197
198
+ // Separate traces by zorder and plot each zorder group separately
199
+ // TODO: Performance
200
+ var traceZorderGroups = { } ;
201
+ for ( var t = 0 ; t < cdSubplot . length ; t ++ ) {
202
+ var trace = cdSubplot [ t ] [ 0 ] . trace ;
203
+ var zi = trace . zorder || 0 ;
204
+ if ( ! traceZorderGroups [ zi ] ) traceZorderGroups [ zi ] = [ ] ;
205
+ traceZorderGroups [ zi ] . push ( cdSubplot [ t ] ) ;
206
+ }
207
+
192
208
var layerData = [ ] ;
193
209
var zoomScaleQueryParts = [ ] ;
194
210
195
- for ( var i = 0 ; i < modules . length ; i ++ ) {
196
- _module = modules [ i ] ;
197
- var name = _module . name ;
198
- var categories = Registry . modules [ name ] . categories ;
199
-
200
- if ( categories . svg ) {
201
- var className = ( _module . layerName || name + 'layer' ) ;
202
- var plotMethod = _module . plot ;
203
-
204
- // plot all visible traces of this type on this subplot at once
205
- cdModuleAndOthers = getModuleCalcData ( cdSubplot , plotMethod ) ;
206
- cdModule = cdModuleAndOthers [ 0 ] ;
207
- // don't need to search the found traces again - in fact we need to NOT
208
- // so that if two modules share the same plotter we don't double-plot
209
- cdSubplot = cdModuleAndOthers [ 1 ] ;
210
-
211
- if ( cdModule . length ) {
212
- layerData . push ( {
213
- i : traceLayerClasses . indexOf ( className ) ,
214
- className : className ,
215
- plotMethod : plotMethod ,
216
- cdModule : cdModule
217
- } ) ;
218
- }
211
+ // Plot each zorder group in ascending order
212
+ var zindices = Object . keys ( traceZorderGroups )
213
+ . map ( Number )
214
+ . sort ( Lib . sorterAsc ) ;
215
+ for ( var z = 0 ; z < zindices . length ; z ++ ) {
216
+ var zorder = zindices [ z ] ;
217
+ // For each "module" (trace type)
218
+ for ( var i = 0 ; i < modules . length ; i ++ ) {
219
+ _module = modules [ i ] ;
220
+ var name = _module . name ;
221
+ var categories = Registry . modules [ name ] . categories ;
222
+
223
+ if ( categories . svg ) {
224
+ var className = ( _module . layerName || name + 'layer' ) + ( z ? Number ( z ) + 1 : '' ) ;
225
+ var plotMethod = _module . plot ;
226
+
227
+ // plot all visible traces of this type on this subplot at once
228
+ cdModuleAndOthers = getModuleCalcData ( cdSubplot , plotMethod , zorder ) ;
229
+ cdModule = cdModuleAndOthers [ 0 ] ;
230
+ // don't need to search the found traces again - in fact we need to NOT
231
+ // so that if two modules share the same plotter we don't double-plot
232
+ cdSubplot = cdModuleAndOthers [ 1 ] ;
233
+
234
+ if ( cdModule . length ) {
235
+ layerData . push ( {
236
+ i : traceLayerClasses . indexOf ( className ) ,
237
+ zorder : z ,
238
+ className : className ,
239
+ plotMethod : plotMethod ,
240
+ cdModule : cdModule
241
+ } ) ;
242
+ }
219
243
220
- if ( categories . zoomScale ) {
221
- zoomScaleQueryParts . push ( '.' + className ) ;
244
+ if ( categories . zoomScale ) {
245
+ zoomScaleQueryParts . push ( '.' + className ) ;
246
+ }
222
247
}
223
248
}
224
249
}
225
-
226
- layerData . sort ( function ( a , b ) { return a . i - b . i ; } ) ;
250
+ // Sort the layers primarily by z, then by i
251
+ layerData . sort ( function ( a , b ) { return ( a . zorder || 0 ) - ( b . zorder || 0 ) || a . i - b . i ; } ) ;
227
252
228
253
var layers = plotinfo . plot . selectAll ( 'g.mlayer' )
229
254
. data ( layerData , function ( d ) { return d . className ; } ) ;
@@ -434,7 +459,6 @@ function makeSubplotData(gd) {
434
459
}
435
460
subplotData [ i ] = d ;
436
461
}
437
-
438
462
return subplotData ;
439
463
}
440
464
0 commit comments