Skip to content

Commit e69b7dd

Browse files
committed
mv find-main-subplot logic from supplyDefaults -> lsInner
- so that supplyDefaults and "easy" edits (e.g. axrange, modebar) can be faster. - mv "axrange" react test out of relayout switchboard describe keeping "real" graph div out the relayout switchboard describe
1 parent 73b354b commit e69b7dd

File tree

3 files changed

+72
-47
lines changed

3 files changed

+72
-47
lines changed

src/plot_api/subroutines.js

+47
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ exports.lsInner = function(gd) {
9595
ax._mainMirrorPosition = (ax.mirror && counterAx) ?
9696
getLinePosition(ax, counterAx,
9797
alignmentConstants.OPPOSITE_SIDE[ax.side]) : null;
98+
99+
// Figure out which subplot to draw ticks, labels, & axis lines on
100+
// do this as a separate loop so we already have all the
101+
// _mainAxis and _anchorAxis links set
102+
ax._mainSubplot = findMainSubplot(ax, fullLayout);
98103
}
99104

100105
fullLayout._paperdiv
@@ -324,6 +329,48 @@ exports.lsInner = function(gd) {
324329
return gd._promises.length && Promise.all(gd._promises);
325330
};
326331

332+
function findMainSubplot(ax, fullLayout) {
333+
var subplotList = fullLayout._subplots;
334+
var ids = subplotList.cartesian.concat(subplotList.gl2d || []);
335+
var mockGd = {_fullLayout: fullLayout};
336+
337+
var isX = ax._id.charAt(0) === 'x';
338+
var anchorAx = ax._mainAxis._anchorAxis;
339+
var mainSubplotID = '';
340+
var nextBestMainSubplotID = '';
341+
var anchorID = '';
342+
343+
// First try the main ID with the anchor
344+
if(anchorAx) {
345+
anchorID = anchorAx._mainAxis._id;
346+
mainSubplotID = isX ? (ax._id + anchorID) : (anchorID + ax._id);
347+
}
348+
349+
// Then look for a subplot with the counteraxis overlaying the anchor
350+
// If that fails just use the first subplot including this axis
351+
if(!mainSubplotID || !fullLayout._plots[mainSubplotID]) {
352+
mainSubplotID = '';
353+
354+
for(var j = 0; j < ids.length; j++) {
355+
var id = ids[j];
356+
var yIndex = id.indexOf('y');
357+
var idPart = isX ? id.substr(0, yIndex) : id.substr(yIndex);
358+
var counterPart = isX ? id.substr(yIndex) : id.substr(0, yIndex);
359+
360+
if(idPart === ax._id) {
361+
if(!nextBestMainSubplotID) nextBestMainSubplotID = id;
362+
var counterAx = Axes.getFromId(mockGd, counterPart);
363+
if(anchorID && counterAx.overlaying === anchorID) {
364+
mainSubplotID = id;
365+
break;
366+
}
367+
}
368+
}
369+
}
370+
371+
return mainSubplotID || nextBestMainSubplotID;
372+
}
373+
327374
function shouldShowLinesOrTicks(ax, subplot) {
328375
return (ax.ticks || ax.showline) &&
329376
(subplot === ax._mainSubplot || ax.mirror === 'all' || ax.mirror === 'allticks');

src/plots/plots.js

-37
Original file line numberDiff line numberDiff line change
@@ -801,43 +801,6 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
801801
null :
802802
axisIDs.getFromId(mockGd, ax.anchor);
803803
}
804-
805-
for(i = 0; i < axList.length; i++) {
806-
// Figure out which subplot to draw ticks, labels, & axis lines on
807-
// do this as a separate loop so we already have all the
808-
// _mainAxis and _anchorAxis links set
809-
ax = axList[i];
810-
var isX = ax._id.charAt(0) === 'x';
811-
var anchorAx = ax._mainAxis._anchorAxis;
812-
var mainSubplotID = '';
813-
var nextBestMainSubplotID = '';
814-
var anchorID = '';
815-
// First try the main ID with the anchor
816-
if(anchorAx) {
817-
anchorID = anchorAx._mainAxis._id;
818-
mainSubplotID = isX ? (ax._id + anchorID) : (anchorID + ax._id);
819-
}
820-
// Then look for a subplot with the counteraxis overlaying the anchor
821-
// If that fails just use the first subplot including this axis
822-
if(!mainSubplotID || !newSubplots[mainSubplotID]) {
823-
mainSubplotID = '';
824-
for(j = 0; j < ids.length; j++) {
825-
id = ids[j];
826-
var yIndex = id.indexOf('y');
827-
var idPart = isX ? id.substr(0, yIndex) : id.substr(yIndex);
828-
var counterPart = isX ? id.substr(yIndex) : id.substr(0, yIndex);
829-
if(idPart === ax._id) {
830-
if(!nextBestMainSubplotID) nextBestMainSubplotID = id;
831-
var counterAx = axisIDs.getFromId(mockGd, counterPart);
832-
if(anchorID && counterAx.overlaying === anchorID) {
833-
mainSubplotID = id;
834-
break;
835-
}
836-
}
837-
}
838-
}
839-
ax._mainSubplot = mainSubplotID || nextBestMainSubplotID;
840-
}
841804
};
842805

843806
// This function clears any trace attributes with valType: color and

test/jasmine/tests/plot_api_test.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -643,30 +643,27 @@ describe('Test plot api', function() {
643643
});
644644
}
645645

646-
var trace = {y: [1, 2, 1]};
647-
648646
var specs = [
649647
['relayout', ['xaxis.range[0]', 0]],
650648
['relayout', ['xaxis.range[1]', 3]],
651649
['relayout', ['xaxis.range', [-1, 5]]],
652-
['update', [{}, {'xaxis.range': [-1, 10]}]],
653-
['react', [[trace], {xaxis: {range: [0, 1]}}]]
650+
['update', [{}, {'xaxis.range': [-1, 10]}]]
654651
];
655652

656653
specs.forEach(function(s) {
657-
// create 'real' div for Plotly.react to work
658-
gd = createGraphDiv();
659-
Plotly.plot(gd, [trace], {xaxis: {range: [1, 2]}});
660-
mock(gd);
654+
gd = mock({
655+
data: [{y: [1, 2, 1]}],
656+
layout: {
657+
xaxis: {range: [1, 2]}
658+
}
659+
});
661660

662661
Plotly[s[0]](gd, s[1][0], s[1][1]);
663662

664663
_assert([
665664
'Plotly.', s[0],
666665
'(gd, ', JSON.stringify(s[1][0]), ', ', JSON.stringify(s[1][1]), ')'
667666
].join(''));
668-
669-
destroyGraphDiv();
670667
});
671668
});
672669

@@ -2773,6 +2770,24 @@ describe('Test plot api', function() {
27732770
.then(done);
27742771
});
27752772

2773+
it('picks up minimal sequence for cartesian axis range updates', function(done) {
2774+
var data = [{y: [1, 2, 1]}];
2775+
var layout = {xaxis: {range: [1, 2]}};
2776+
var layout2 = {xaxis: {range: [0, 1]}};
2777+
2778+
Plotly.newPlot(gd, data, layout)
2779+
.then(countPlots)
2780+
.then(function() {
2781+
return Plotly.react(gd, data, layout2);
2782+
})
2783+
.then(function() {
2784+
expect(subroutines.doTicksRelayout).toHaveBeenCalledTimes(1);
2785+
expect(subroutines.layoutStyles).not.toHaveBeenCalled();
2786+
})
2787+
.catch(failTest)
2788+
.then(done);
2789+
});
2790+
27762791
it('redraws annotations one at a time', function(done) {
27772792
var data = [{y: [1, 2, 3], mode: 'markers'}];
27782793
var layout = {};

0 commit comments

Comments
 (0)