diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index d13d3f6104d..9c266949bc8 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -1946,29 +1946,29 @@ axes.drawOne = function(gd, ax, opts) { push = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0}; var bbox = ax._boundingBox; - var counterAx = mainPlotinfo[counterLetter + 'axis']; + var titleOffset = getTitleOffset(gd, ax); var anchorAxDomainIndex; var offset; switch(axLetter + s) { case 'xb': anchorAxDomainIndex = 0; - offset = bbox.top - counterAx._length - counterAx._offset; + offset = bbox.top - titleOffset; push[s] = bbox.height; break; case 'xt': anchorAxDomainIndex = 1; - offset = counterAx._offset - bbox.bottom; + offset = titleOffset - bbox.bottom; push[s] = bbox.height; break; case 'yl': anchorAxDomainIndex = 0; - offset = counterAx._offset - bbox.right; + offset = titleOffset - bbox.right; push[s] = bbox.width; break; case 'yr': anchorAxDomainIndex = 1; - offset = bbox.left - counterAx._length - counterAx._offset; + offset = bbox.left - titleOffset; push[s] = bbox.width; break; } @@ -1980,6 +1980,7 @@ axes.drawOne = function(gd, ax, opts) { if(push[s] > 0) { push[s] += offset; } + if(ax.title.text !== fullLayout._dfltTitle[axLetter]) { push[s] += ax.title.font.size; } @@ -2647,11 +2648,37 @@ function drawDividers(gd, ax, opts) { .attr('d', opts.path); } +function getTitleOffset(gd, ax) { + var gs = gd._fullLayout._size; + var axLetter = ax._id.charAt(0); + var side = ax.side; + var anchorAxis; + + if(ax.anchor !== 'free') { + anchorAxis = axisIds.getFromId(gd, ax.anchor); + } else if(axLetter === 'x') { + anchorAxis = { + _offset: gs.t + (1 - (ax.position || 0)) * gs.h, + _length: 0 + }; + } else if(axLetter === 'y') { + anchorAxis = { + _offset: gs.l + (ax.position || 0) * gs.w, + _length: 0 + }; + } + + if(side === 'top' || side === 'left') { + return anchorAxis._offset; + } else if(side === 'bottom' || side === 'right') { + return anchorAxis._offset + anchorAxis._length; + } +} + function drawTitle(gd, ax) { var fullLayout = gd._fullLayout; var axId = ax._id; var axLetter = axId.charAt(0); - var gs = fullLayout._size; var fontSize = ax.title.font.size; var titleStandoff; @@ -2662,36 +2689,28 @@ function drawTitle(gd, ax) { titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0); } - var transform, counterAxis, x, y; + var titleOffset = getTitleOffset(gd, ax); - if(axLetter === 'x') { - counterAxis = (ax.anchor === 'free') ? - {_offset: gs.t + (1 - (ax.position || 0)) * gs.h, _length: 0} : - axisIds.getFromId(gd, ax.anchor); + var transform, x, y; + if(axLetter === 'x') { x = ax._offset + ax._length / 2; if(ax.side === 'top') { y = -titleStandoff - fontSize * (ax.showticklabels ? 1 : 0); } else { - y = counterAxis._length + titleStandoff + - fontSize * (ax.showticklabels ? 1.5 : 0.5); + y = titleStandoff + fontSize * (ax.showticklabels ? 1.5 : 0.5); } - y += counterAxis._offset; + y += titleOffset; } else { - counterAxis = (ax.anchor === 'free') ? - {_offset: gs.l + (ax.position || 0) * gs.w, _length: 0} : - axisIds.getFromId(gd, ax.anchor); - y = ax._offset + ax._length / 2; if(ax.side === 'right') { - x = counterAxis._length + titleStandoff + - fontSize * (ax.showticklabels ? 1 : 0.5); + x = titleStandoff + fontSize * (ax.showticklabels ? 1 : 0.5); } else { x = -titleStandoff - fontSize * (ax.showticklabels ? 0.5 : 0); } - x += counterAxis._offset; + x += titleOffset; transform = {rotate: '-90', offset: 0}; } diff --git a/test/image/baselines/automargin-superimposed-axes.png b/test/image/baselines/automargin-superimposed-axes.png new file mode 100644 index 00000000000..06fc2d2ee23 Binary files /dev/null and b/test/image/baselines/automargin-superimposed-axes.png differ diff --git a/test/image/mocks/automargin-superimposed-axes.json b/test/image/mocks/automargin-superimposed-axes.json new file mode 100644 index 00000000000..156fa0b2e15 --- /dev/null +++ b/test/image/mocks/automargin-superimposed-axes.json @@ -0,0 +1,45 @@ +{ + "data": [ + { + "x": [ 1, 2 ], + "y": [ 1, 2 ] + }, + { + "x": [ 1, 2 ], + "y": [ 1, 2 ], + "xaxis": "x2", + "yaxis": "y2" + } + ], + "layout": { + "showlegend": false, + "margin": {"b": 0}, + "title": { + "text": "automargin:true on superimposed x axes" + }, + "xaxis": { + "title": { + "text": "x" + }, + "automargin": true + }, + "xaxis2": { + "title": { + "text": "           x2" + }, + "automargin": true + }, + "yaxis": { + "title": { + "text": "hi" + }, + "domain": [0, 0.5] + }, + "yaxis2": { + "title": { + "text": "hi" + }, + "domain": [0.5, 1] + } + } +}