Skip to content

Commit 274526a

Browse files
committed
stash _mainAxis and _anchorAxis in fullLayout axis objects
We could probably use these in other places too, to reduce getFromId calls
1 parent b31e60d commit 274526a

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

src/plot_api/subroutines.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ exports.lsInner = function(gd) {
172172
.call(Drawing.setTranslate, xa._offset, ya._offset)
173173
.call(Drawing.setClipUrl, plotinfo.clipId);
174174

175-
var xIsFree = xa.anchor === 'free';
175+
var xIsFree = !xa._anchorAxis;
176176
var showFreeX = xIsFree && !freeFinished[xa._id];
177177
var showBottom = shouldShowLine(xa, ya, 'bottom');
178178
var showTop = shouldShowLine(xa, ya, 'top');
179179

180-
var yIsFree = ya.anchor === 'free';
180+
var yIsFree = !ya._anchorAxis;
181181
var showFreeY = yIsFree && !freeFinished[ya._id];
182182
var showLeft = shouldShowLine(ya, xa, 'left');
183183
var showRight = shouldShowLine(ya, xa, 'right');
@@ -249,7 +249,7 @@ exports.lsInner = function(gd) {
249249
showTop ? xLinesYTop : undefined,
250250
showFreeX ? xLinesYFree : undefined
251251
];
252-
if(xa.anchor === ya._id) {
252+
if(xa._anchorAxis === ya) {
253253
xa._linepositions[subplot][3] = xa.side === 'top' ?
254254
xLinesYTop : xLinesYBottom;
255255
}
@@ -262,7 +262,7 @@ exports.lsInner = function(gd) {
262262
showRight ? yLinesXRight : undefined,
263263
showFreeY ? yLinesXFree : undefined
264264
];
265-
if(ya.anchor === xa._id) {
265+
if(ya._anchorAxis === xa) {
266266
ya._linepositions[subplot][3] = ya.side === 'right' ?
267267
yLinesXRight : yLinesXLeft;
268268
}
@@ -329,28 +329,24 @@ exports.lsInner = function(gd) {
329329
};
330330

331331
function shouldShowLine(ax, counterAx, side) {
332-
return (ax.anchor === counterAx._id && (ax.mirror || ax.side === side)) ||
332+
return (ax._anchorAxis === counterAx && (ax.mirror || ax.side === side)) ||
333333
ax.mirror === 'all' || ax.mirror === 'allticks' ||
334334
(ax.mirrors && ax.mirrors[counterAx._id + side]);
335335
}
336336

337-
function findMainAxis(gd, ax) {
338-
return ax.overlaying ? Plotly.Axes.getFromId(gd, ax.overlaying) : ax;
339-
}
340-
341337
function findCounterAxes(gd, ax, axList) {
342338
var counterAxes = [];
343-
var anchorAx = Plotly.Axes.getFromId(gd, ax.anchor);
339+
var anchorAx = ax._anchorAxis;
344340
if(anchorAx) {
345-
var counterMain = findMainAxis(gd, anchorAx);
341+
var counterMain = anchorAx._mainAxis;
346342
if(counterAxes.indexOf(counterMain) === -1) {
347343
counterAxes.push(counterMain);
348-
}
349-
for(var i = 0; i < axList.length; i++) {
350-
if(axList[i].overlaying === counterMain._id &&
351-
counterAxes.indexOf(axList[i]) === -1
352-
) {
353-
counterAxes.push(axList[i]);
344+
for(var i = 0; i < axList.length; i++) {
345+
if(axList[i].overlaying === counterMain._id &&
346+
counterAxes.indexOf(axList[i]) === -1
347+
) {
348+
counterAxes.push(axList[i]);
349+
}
354350
}
355351
}
356352
}
@@ -360,7 +356,8 @@ function findCounterAxes(gd, ax, axList) {
360356
function findLineWidth(gd, axes, side) {
361357
for(var i = 0; i < axes.length; i++) {
362358
var ax = axes[i];
363-
if(ax.anchor !== 'free' && shouldShowLine(ax, {_id: ax.anchor}, side)) {
359+
var anchorAx = ax._anchorAxis;
360+
if(anchorAx && shouldShowLine(ax, anchorAx, side)) {
364361
return Drawing.crispRound(gd, ax.linewidth);
365362
}
366363
}
@@ -374,7 +371,7 @@ function findCounterAxisLineWidth(gd, ax, subplotCounterLineWidth,
374371

375372
// find all counteraxes for this one, then of these, find the
376373
// first one that has a visible line on this side
377-
var mainAxis = findMainAxis(gd, ax);
374+
var mainAxis = ax._mainAxis;
378375
var counterAxes = findCounterAxes(gd, mainAxis, axList);
379376

380377
var lineWidth = findLineWidth(gd, counterAxes, side);

src/plots/cartesian/index.js

+4-28
Original file line numberDiff line numberDiff line change
@@ -242,40 +242,16 @@ function makeSubplotData(gd) {
242242
var subplot = subplots[i],
243243
plotinfo = fullLayout._plots[subplot];
244244

245-
var xa = plotinfo.xaxis,
246-
ya = plotinfo.yaxis;
247-
248-
// is this subplot overlaid on another?
249-
// ax.overlaying is the id of another axis of the same
250-
// dimension that this one overlays to be an overlaid subplot,
251-
// the main plot must exist make sure we're not trying to
252-
// overlay on an axis that's already overlaying another
253-
var xa2 = axisIds.getFromId(gd, xa.overlaying) || xa;
254-
if(xa2 !== xa && xa2.overlaying) {
255-
xa2 = xa;
256-
xa.overlaying = false;
257-
}
258-
259-
var ya2 = axisIds.getFromId(gd, ya.overlaying) || ya;
260-
if(ya2 !== ya && ya2.overlaying) {
261-
ya2 = ya;
262-
ya.overlaying = false;
263-
}
245+
var xa = plotinfo.xaxis;
246+
var ya = plotinfo.yaxis;
247+
var xa2 = xa._mainAxis;
248+
var ya2 = ya._mainAxis;
264249

265250
var mainplot = xa2._id + ya2._id;
266251
if(mainplot !== subplot && subplots.indexOf(mainplot) !== -1) {
267252
plotinfo.mainplot = mainplot;
268253
plotinfo.mainplotinfo = fullLayout._plots[mainplot];
269254
overlays.push(subplot);
270-
271-
// for now force overlays to overlay completely... so they
272-
// can drag together correctly and share backgrounds.
273-
// Later perhaps we make separate axis domain and
274-
// tick/line domain or something, so they can still share
275-
// the (possibly larger) dragger and background but don't
276-
// have to both be drawn over that whole domain
277-
xa.domain = xa2.domain.slice();
278-
ya.domain = ya2.domain.slice();
279255
}
280256
else {
281257
subplotData.push(subplot);

src/plots/plots.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,9 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
641641

642642
var ids = Plotly.Axes.getSubplots(mockGd);
643643

644-
for(var i = 0; i < ids.length; i++) {
644+
var i;
645+
646+
for(i = 0; i < ids.length; i++) {
645647
var id = ids[i],
646648
oldSubplot = oldSubplots[id],
647649
plotinfo;
@@ -661,6 +663,39 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
661663
plotinfo.xaxis = Plotly.Axes.getFromId(mockGd, id, 'x');
662664
plotinfo.yaxis = Plotly.Axes.getFromId(mockGd, id, 'y');
663665
}
666+
667+
// while we're at it, link overlaying axes to their main axes and
668+
// anchored axes to the axes they're anchored to
669+
var axList = Plotly.Axes.list(mockGd, null, true);
670+
for(i = 0; i < axList.length; i++) {
671+
var ax = axList[i];
672+
var mainAx = null;
673+
674+
if(ax.overlaying) {
675+
mainAx = Plotly.Axes.getFromId(mockGd, ax.overlaying);
676+
677+
// you cannot overlay an axis that's already overlaying another
678+
if(mainAx && mainAx.overlaying) {
679+
ax.overlaying = false;
680+
mainAx = null;
681+
}
682+
}
683+
ax._mainAxis = mainAx || ax;
684+
685+
/*
686+
* For now force overlays to overlay completely... so they
687+
* can drag together correctly and share backgrounds.
688+
* Later perhaps we make separate axis domain and
689+
* tick/line domain or something, so they can still share
690+
* the (possibly larger) dragger and background but don't
691+
* have to both be drawn over that whole domain
692+
*/
693+
if(mainAx) ax.domain = mainAx.domain.slice();
694+
695+
ax._anchorAxis = ax.anchor === 'free' ?
696+
null :
697+
Plotly.Axes.getFromId(mockGd, ax.anchor);
698+
}
664699
};
665700

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

0 commit comments

Comments
 (0)