@@ -130,21 +130,27 @@ exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
130130 var calcdata = gd . calcdata ;
131131 var i ;
132132
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+
133137 if ( ! Array . isArray ( traces ) ) {
134138 // If traces is not provided, then it's a complete replot and missing
135139 // traces are removed
136140 traces = [ ] ;
137141 for ( i = 0 ; i < calcdata . length ; i ++ ) traces . push ( i ) ;
138142 }
139143
144+ // For each subplot
140145 for ( i = 0 ; i < subplots . length ; i ++ ) {
141146 var subplot = subplots [ i ] ;
142147 var subplotInfo = fullLayout . _plots [ subplot ] ;
143148
144- // Get all calcdata for this subplot:
149+ // Get all calcdata (traces) for this subplot:
145150 var cdSubplot = [ ] ;
146151 var pcd ;
147152
153+ // For each trace
148154 for ( var j = 0 ; j < calcdata . length ; j ++ ) {
149155 var cd = calcdata [ j ] ;
150156 var trace = cd [ 0 ] . trace ;
@@ -178,7 +184,7 @@ exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
178184 pcd = cd ;
179185 }
180186 }
181-
187+ // Plot the traces for this subplot
182188 plotOne ( gd , subplotInfo , cdSubplot , transitionOpts , makeOnCompleteCallback ) ;
183189 }
184190} ;
@@ -189,41 +195,60 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
189195 var modules = fullLayout . _modules ;
190196 var _module , cdModuleAndOthers , cdModule ;
191197
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+
192208 var layerData = [ ] ;
193209 var zoomScaleQueryParts = [ ] ;
194210
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+ }
219243
220- if ( categories . zoomScale ) {
221- zoomScaleQueryParts . push ( '.' + className ) ;
244+ if ( categories . zoomScale ) {
245+ zoomScaleQueryParts . push ( '.' + className ) ;
246+ }
222247 }
223248 }
224249 }
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 ; } ) ;
227252
228253 var layers = plotinfo . plot . selectAll ( 'g.mlayer' )
229254 . data ( layerData , function ( d ) { return d . className ; } ) ;
@@ -434,7 +459,6 @@ function makeSubplotData(gd) {
434459 }
435460 subplotData [ i ] = d ;
436461 }
437-
438462 return subplotData ;
439463}
440464
0 commit comments