Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function return a default value if both conditions were false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should never happen.

}

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;
Expand All @@ -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};
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions test/image/mocks/automargin-superimposed-axes.json
Original file line number Diff line number Diff line change
@@ -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]
}
}
}