Skip to content

Commit e2dab8e

Browse files
committed
sort subplots
1 parent ed0e12d commit e2dab8e

File tree

1 file changed

+134
-70
lines changed

1 file changed

+134
-70
lines changed

src/plots/cartesian/index.js

+134-70
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ exports.finalizeSubplots = function(layoutIn, layoutOut) {
126126
*/
127127
exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
128128
var fullLayout = gd._fullLayout;
129-
var subplots = fullLayout._subplots.cartesian;
130129
var calcdata = gd.calcdata;
131130
var i;
132131

@@ -141,55 +140,75 @@ exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
141140
for(i = 0; i < calcdata.length; i++) traces.push(i);
142141
}
143142

144-
// For each subplot
145-
for(i = 0; i < subplots.length; i++) {
146-
var subplot = subplots[i];
147-
var subplotInfo = fullLayout._plots[subplot];
148-
149-
// Get all calcdata (traces) for this subplot:
150-
var cdSubplot = [];
151-
var pcd;
152-
153-
// For each trace
154-
for(var j = 0; j < calcdata.length; j++) {
155-
var cd = calcdata[j];
156-
var trace = cd[0].trace;
157-
158-
// Skip trace if whitelist provided and it's not whitelisted:
159-
// if (Array.isArray(traces) && traces.indexOf(i) === -1) continue;
160-
if(trace.xaxis + trace.yaxis === subplot) {
161-
// XXX: Should trace carpet dependencies. Only replot all carpet plots if the carpet
162-
// axis has actually changed:
163-
//
164-
// If this trace is specifically requested, add it to the list:
165-
if(traces.indexOf(trace.index) !== -1 || trace.carpet) {
166-
// Okay, so example: traces 0, 1, and 2 have fill = tonext. You animate
167-
// traces 0 and 2. Trace 1 also needs to be updated, otherwise its fill
168-
// is outdated. So this retroactively adds the previous trace if the
169-
// traces are interdependent.
170-
if(
171-
pcd &&
172-
pcd[0].trace.xaxis + pcd[0].trace.yaxis === subplot &&
173-
['tonextx', 'tonexty', 'tonext'].indexOf(trace.fill) !== -1 &&
174-
cdSubplot.indexOf(pcd) === -1
175-
) {
176-
cdSubplot.push(pcd);
143+
var trace;
144+
var subplot;
145+
var subplotZindexGroups = {};
146+
for(var t = 0; t < calcdata.length; t++) {
147+
trace = calcdata[t][0].trace;
148+
var zi = trace.zindex || 0;
149+
subplot = trace.xaxis + trace.yaxis;
150+
if(!subplotZindexGroups[zi]) subplotZindexGroups[zi] = {};
151+
if(!subplotZindexGroups[zi][subplot]) subplotZindexGroups[zi][subplot] = [];
152+
subplotZindexGroups[zi][subplot].push(calcdata[t]);
153+
}
154+
var zindices = Object.keys(subplotZindexGroups)
155+
.map(Number)
156+
.sort(Lib.sorterAsc);
157+
158+
var subplots;
159+
var zindex;
160+
var subplotLayerData = {};
161+
for(i = 0; i < zindices.length; i++) {
162+
zindex = zindices[i];
163+
subplots = Object.keys(subplotZindexGroups[zindex]);
164+
165+
// For each subplot
166+
for(var j = 0; j < subplots.length; j++) {
167+
subplot = subplots[j];
168+
var subplotInfo = fullLayout._plots[subplot];
169+
170+
// Get all calcdata (traces) for this subplot:
171+
var cdSubplot = [];
172+
var pcd;
173+
// For each trace
174+
for(var k = 0; k < subplotZindexGroups[zindex][subplot].length; k++) {
175+
var cd = subplotZindexGroups[zindex][subplot][k];
176+
trace = cd[0].trace;
177+
// Skip trace if whitelist provided and it's not whitelisted:
178+
// if (Array.isArray(traces) && traces.indexOf(i) === -1) continue;
179+
if(trace.xaxis + trace.yaxis === subplot) {
180+
// XXX: Should trace carpet dependencies. Only replot all carpet plots if the carpet
181+
// axis has actually changed:
182+
//
183+
// If this trace is specifically requested, add it to the list:
184+
if(traces.indexOf(trace.index) !== -1 || trace.carpet) {
185+
// Okay, so example: traces 0, 1, and 2 have fill = tonext. You animate
186+
// traces 0 and 2. Trace 1 also needs to be updated, otherwise its fill
187+
// is outdated. So this retroactively adds the previous trace if the
188+
// traces are interdependent.
189+
if(
190+
pcd &&
191+
pcd[0].trace.xaxis + pcd[0].trace.yaxis === subplot &&
192+
['tonextx', 'tonexty', 'tonext'].indexOf(trace.fill) !== -1 &&
193+
cdSubplot.indexOf(pcd) === -1
194+
) {
195+
cdSubplot.push(pcd);
196+
}
197+
cdSubplot.push(cd);
177198
}
178199

179-
cdSubplot.push(cd);
200+
// Track the previous trace on this subplot for the retroactive-add step
201+
// above:
202+
pcd = cd;
180203
}
181-
182-
// Track the previous trace on this subplot for the retroactive-add step
183-
// above:
184-
pcd = cd;
185204
}
205+
if(!subplotLayerData[subplot]) subplotLayerData[subplot] = [];
206+
subplotLayerData[subplot] = plotOne(gd, subplotInfo, cdSubplot, transitionOpts, makeOnCompleteCallback, subplotLayerData[subplot]);
186207
}
187-
// Plot the traces for this subplot
188-
plotOne(gd, subplotInfo, cdSubplot, transitionOpts, makeOnCompleteCallback);
189208
}
190209
};
191210

192-
function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback) {
211+
function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback, layerData) {
193212
var traceLayerClasses = constants.traceLayerClasses;
194213
var fullLayout = gd._fullLayout;
195214
var modules = fullLayout._modules;
@@ -205,7 +224,6 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
205224
traceZindexGroups[zi].push(cdSubplot[t]);
206225
}
207226

208-
var layerData = [];
209227
var zoomScaleQueryParts = [];
210228

211229
// Plot each zindex group in ascending order
@@ -221,7 +239,7 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
221239
var categories = Registry.modules[name].categories;
222240

223241
if(categories.svg) {
224-
var className = (_module.layerName || name + 'layer') + (zindex ? '-' + z : '');
242+
var className = (_module.layerName || name + 'layer') + '_' + zindex;
225243
var plotMethod = _module.plot;
226244

227245
// plot all visible traces of this type on this subplot at once
@@ -234,7 +252,7 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
234252
if(cdModule.length) {
235253
layerData.push({
236254
i: traceLayerClasses.indexOf(className),
237-
zindex: z,
255+
zindex: zindex,
238256
className: className,
239257
plotMethod: plotMethod,
240258
cdModule: cdModule
@@ -301,6 +319,7 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
301319
plotinfo.zoomScaleTxt = traces.selectAll('.textpoint');
302320
}
303321
}
322+
return layerData;
304323
}
305324

306325
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
@@ -372,15 +391,16 @@ exports.drawFramework = function(gd) {
372391

373392
var subplotLayers = fullLayout._cartesianlayer.selectAll('.subplot')
374393
.data(subplotData, String);
375-
394+
376395
subplotLayers.enter().append('g')
377396
.attr('class', function(d) { return 'subplot ' + d[0]; });
378397

379-
subplotLayers.order();
380-
381-
subplotLayers.exit()
382-
.call(purgeSubplotLayers, fullLayout);
398+
//subplotLayers.order();
383399

400+
//subplotLayers.exit()
401+
// .call(purgeSubplotLayers, fullLayout);
402+
console.log("Subplotlayers")
403+
console.log(subplotLayers)
384404
subplotLayers.each(function(d) {
385405
var id = d[0];
386406
var plotinfo = fullLayout._plots[id];
@@ -411,29 +431,67 @@ function makeSubplotData(gd) {
411431
var regulars = [];
412432
var overlays = [];
413433

414-
for(i = 0; i < len; i++) {
415-
id = ids[i];
416-
plotinfo = fullLayout._plots[id];
417-
xa = plotinfo.xaxis;
418-
ya = plotinfo.yaxis;
419-
420-
var xa2 = xa._mainAxis;
421-
var ya2 = ya._mainAxis;
422-
var mainplot = xa2._id + ya2._id;
423-
var mainplotinfo = fullLayout._plots[mainplot];
424-
plotinfo.overlays = [];
434+
var calcdata = gd.calcdata;
425435

426-
if(mainplot !== id && mainplotinfo) {
427-
plotinfo.mainplot = mainplot;
428-
plotinfo.mainplotinfo = mainplotinfo;
429-
overlays.push(id);
430-
} else {
431-
plotinfo.mainplot = undefined;
432-
plotinfo.mainplotinfo = undefined;
433-
regulars.push(id);
436+
var trace;
437+
var subplot;
438+
var subplotZindexGroups = {};
439+
for(var t = 0; t < calcdata.length; t++) {
440+
trace = calcdata[t][0].trace;
441+
var zi = trace.zindex || 0;
442+
subplot = trace.xaxis + trace.yaxis;
443+
if(!subplotZindexGroups[zi]) subplotZindexGroups[zi] = {};
444+
if(!subplotZindexGroups[zi][subplot]) subplotZindexGroups[zi][subplot] = [];
445+
subplotZindexGroups[zi][subplot].push(calcdata[t]);
446+
}
447+
var zindices = Object.keys(subplotZindexGroups)
448+
.map(Number)
449+
.sort(Lib.sorterAsc);
450+
451+
console.log(subplotZindexGroups)
452+
453+
for(i = 0; i < zindices.length; i++) {
454+
console.log(i)
455+
var zindex = subplotZindexGroups[zindices[i]];
456+
console.log(zindex)
457+
console.log()
458+
var ids = Object.keys(zindex);
459+
for(var j=0; j<ids.length; j++) {
460+
var id = ids[j];
461+
plotinfo = fullLayout._plots[id];
462+
//xa = plotinfo.xaxis;
463+
//ya = plotinfo.yaxis;
464+
465+
//var xa2 = xa._mainAxis;
466+
//var ya2 = ya._mainAxis;
467+
var mainplot = mainplot ? mainplot : id;//xa2._id + ya2._id;
468+
var mainplotinfo = fullLayout._plots[mainplot];
469+
plotinfo.overlays = [];
470+
471+
if(i!==0) {//if(mainplot !== id && mainplotinfo) {
472+
console.log("hererere")
473+
plotinfo.mainplot = mainplot;
474+
plotinfo.mainplotinfo = mainplotinfo;
475+
overlays.push(id);
476+
} else {
477+
plotinfo.mainplot = undefined;
478+
plotinfo.mainplotinfo = undefined;
479+
regulars.push(id);
480+
}
481+
console.log(".....")
434482
}
483+
484+
}
485+
console.log(plotinfo.mainplotinfo)
486+
console.log("----")
487+
console.log(regulars, overlays)
488+
function onlyUnique(value, index, array) {
489+
return array.indexOf(value) === index;
435490
}
436491

492+
regulars = regulars.filter(onlyUnique);
493+
overlays = overlays.filter(onlyUnique);
494+
console.log(regulars, overlays)
437495
// fill in list of overlaying subplots in 'main plot'
438496
for(i = 0; i < overlays.length; i++) {
439497
id = overlays[i];
@@ -448,6 +506,7 @@ function makeSubplotData(gd) {
448506
for(i = 0; i < len; i++) {
449507
id = subplotIds[i];
450508
plotinfo = fullLayout._plots[id];
509+
console.log(id, plotinfo)
451510
xa = plotinfo.xaxis;
452511
ya = plotinfo.yaxis;
453512

@@ -528,8 +587,12 @@ function makeSubplotLayer(gd, plotinfo) {
528587
plotinfo.minorGridlayer = mainplotinfo.minorGridlayer;
529588
plotinfo.gridlayer = mainplotinfo.gridlayer;
530589
plotinfo.zerolinelayer = mainplotinfo.zerolinelayer;
531-
590+
console.log(xId)
591+
console.log(mainplotinfo)
592+
console.log(mainplotinfo.overlinesBelow)
532593
ensureSingle(mainplotinfo.overlinesBelow, 'path', xId);
594+
console.log(yId)
595+
533596
ensureSingle(mainplotinfo.overlinesBelow, 'path', yId);
534597
ensureSingle(mainplotinfo.overaxesBelow, 'g', xId);
535598
ensureSingle(mainplotinfo.overaxesBelow, 'g', yId);
@@ -571,6 +634,7 @@ function makeSubplotLayer(gd, plotinfo) {
571634
plotinfo.ylines
572635
.style('fill', 'none')
573636
.classed('crisp', true);
637+
console.log("ñññññ")
574638
}
575639

576640
function purgeSubplotLayers(layers, fullLayout) {

0 commit comments

Comments
 (0)