Skip to content

Commit 7678185

Browse files
committed
first cut title.standoff for cartesian axes
1 parent 5045067 commit 7678185

File tree

5 files changed

+96
-20
lines changed

5 files changed

+96
-20
lines changed

src/plots/cartesian/axes.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -1937,9 +1937,11 @@ axes.drawOne = function(gd, ax, opts) {
19371937

19381938
if(ax.title.text !== fullLayout._dfltTitle[axLetter]) {
19391939
var extraLines = (ax.title.text.match(svgTextUtils.BR_TAG_ALL) || []).length;
1940-
push[s] += extraLines ?
1940+
var titlePush = extraLines ?
19411941
ax.title.font.size * (extraLines + 1) * LINE_SPACING :
19421942
ax.title.font.size;
1943+
1944+
push[s] = Math.max(push[s] + titlePush, (ax.title.standoff || 0));
19431945
}
19441946

19451947
if(ax.mirror) {
@@ -2706,35 +2708,37 @@ function drawTitle(gd, ax) {
27062708
var fontSize = ax.title.font.size;
27072709

27082710
var titleStandoff;
2709-
if(ax.type === 'multicategory') {
2710-
titleStandoff = ax._depth;
2711+
2712+
if(ax.title.hasOwnProperty('standoff')) {
2713+
titleStandoff = ax.title.standoff;
27112714
} else {
2712-
var offsetBase = 1.5;
2713-
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
2715+
if(ax.type === 'multicategory') {
2716+
titleStandoff = ax._depth;
2717+
} else {
2718+
var offsetBase = 1.5;
2719+
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
2720+
}
2721+
2722+
if(axLetter === 'x') {
2723+
titleStandoff += ax.side === 'top' ?
2724+
fontSize * (ax.showticklabels ? 1 : 0) :
2725+
fontSize * (ax.showticklabels ? 1.5 : 0.5);
2726+
} else {
2727+
titleStandoff += ax.side === 'right' ?
2728+
fontSize * (ax.showticklabels ? 1 : 0.5) :
2729+
fontSize * (ax.showticklabels ? 0.5 : 0);
2730+
}
27142731
}
27152732

27162733
var pos = axes.getPxPosition(gd, ax);
27172734
var transform, x, y;
27182735

27192736
if(axLetter === 'x') {
27202737
x = ax._offset + ax._length / 2;
2721-
2722-
if(ax.side === 'top') {
2723-
y = -titleStandoff - fontSize * (ax.showticklabels ? 1 : 0);
2724-
} else {
2725-
y = titleStandoff + fontSize * (ax.showticklabels ? 1.5 : 0.5);
2726-
}
2727-
y += pos;
2738+
y = (ax.side === 'top') ? pos - titleStandoff : pos + titleStandoff;
27282739
} else {
27292740
y = ax._offset + ax._length / 2;
2730-
2731-
if(ax.side === 'right') {
2732-
x = titleStandoff + fontSize * (ax.showticklabels ? 1 : 0.5);
2733-
} else {
2734-
x = -titleStandoff - fontSize * (ax.showticklabels ? 0.5 : 0);
2735-
}
2736-
x += pos;
2737-
2741+
x = (ax.side === 'right') ? pos + titleStandoff : pos - titleStandoff;
27382742
transform = {rotate: '-90', offset: 0};
27392743
}
27402744

src/plots/cartesian/layout_attributes.js

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ module.exports = {
6262
'by the now deprecated `titlefont` attribute.'
6363
].join(' ')
6464
}),
65+
standoff: {
66+
valType: 'number',
67+
role: 'info',
68+
min: 0,
69+
editType: 'ticks',
70+
description: [
71+
'Sets the standoff distance (in px) between the axis line and the title text',
72+
'The default value is a function of the axis tick labels, the title `font.size`',
73+
'and the axis `linewidth`.',
74+
'Note that the axis title position is always constrained within the margins,',
75+
'so the actual standoff distance is always less than the set or default value.',
76+
'By setting `standoff` and turning on `automargin`, plotly.js will push the',
77+
'margins to fit the axis title at given standoff distance.'
78+
].join(' ')
79+
},
6580
editType: 'ticks'
6681
},
6782
type: {

src/plots/cartesian/layout_defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
235235
grid: layoutOut.grid
236236
});
237237

238+
coerce('title.standoff');
239+
238240
axLayoutOut._input = axLayoutIn;
239241
}
240242

Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"data": [
3+
{"y": [1]},
4+
{"y": [1], "xaxis": "x2", "yaxis": "y2"}
5+
],
6+
"layout": {
7+
"width": 600,
8+
"height": 300,
9+
"margin": {"l": 0, "r": 0, "t": 0, "b": 0},
10+
"showlegend": false,
11+
"grid": {"rows": 1, "columns": 2, "pattern": "independent"},
12+
"xaxis": {
13+
"title": {
14+
"text": "X Axis",
15+
"standoff": 60,
16+
"font": {"size": 25}
17+
},
18+
"automargin": true,
19+
"showline": true,
20+
"mirror": true
21+
},
22+
"yaxis": {
23+
"title": {
24+
"text": "Y<br>Axis",
25+
"standoff": 100,
26+
"font": {"size": 20}
27+
},
28+
"automargin": true,
29+
"showline": true,
30+
"mirror": true
31+
},
32+
"xaxis2": {
33+
"anchor": "y2",
34+
"side": "top",
35+
"title": {
36+
"text": "X<br>Axis<br>2",
37+
"standoff": 80
38+
},
39+
"automargin": true,
40+
"showline": true,
41+
"mirror": true
42+
},
43+
"yaxis2": {
44+
"anchor": "x2",
45+
"side": "right",
46+
"title": {
47+
"text": "Y Axis 2",
48+
"standoff": 30
49+
},
50+
"automargin": true,
51+
"showline": true,
52+
"mirror": true
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)