Skip to content

Commit c94ba44

Browse files
committed
Fix inconsistent axis title standoff when title is one text line vs. multiple lines.
Previously, the standoff was calculated in the same way for all axis sides, but since the renderer draws from top left to bottom right, the code must be different for top/left axes titles and bottom/right axes titles: For top/left axes all lines after the first of a multiline title require extra space before the ticklabels, whereas for bottom/right axes, the drawing baseline needs to be shifted 1 line after the ticklabels to prevent overlap.
1 parent 7beccfb commit c94ba44

File tree

2 files changed

+167
-4
lines changed

2 files changed

+167
-4
lines changed

src/plots/cartesian/axes.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,9 +4060,7 @@ function approxTitleDepth(ax) {
40604060
var fontSize = ax.title.font.size;
40614061
var extraLines = (ax.title.text.match(svgTextUtils.BR_TAG_ALL) || []).length;
40624062
if(ax.title.hasOwnProperty('standoff')) {
4063-
return extraLines ?
4064-
fontSize * (CAP_SHIFT + (extraLines * LINE_SPACING)) :
4065-
fontSize * CAP_SHIFT;
4063+
return fontSize * (CAP_SHIFT + (extraLines * LINE_SPACING));
40664064
} else {
40674065
return extraLines ?
40684066
fontSize * (extraLines + 1) * LINE_SPACING :
@@ -4093,9 +4091,19 @@ function drawTitle(gd, ax) {
40934091
var axLetter = axId.charAt(0);
40944092
var fontSize = ax.title.font.size;
40954093
var titleStandoff;
4094+
var extraLines = (ax.title.text.match(svgTextUtils.BR_TAG_ALL) || []).length;
40964095

40974096
if(ax.title.hasOwnProperty('standoff')) {
4098-
titleStandoff = ax._depth + ax.title.standoff + approxTitleDepth(ax);
4097+
// With ax._depth the initial drawing baseline is at the outer axis border (where the
4098+
// ticklabels are drawn). Since the title text will be drawn above the baseline,
4099+
// bottom/right axes must be shifted by 1 text line to draw below ticklabels instead of on
4100+
// top of them, whereas for top/left axes, the first line would already be correctly drawn
4101+
// before the ticklabels, but we need an offset for all subsequent lines.
4102+
if(ax.side === 'bottom' || ax.side === 'right') {
4103+
titleStandoff = ax._depth + ax.title.standoff + fontSize * CAP_SHIFT;
4104+
} else if(ax.side === 'top' || ax.side === 'left') {
4105+
titleStandoff = ax._depth + ax.title.standoff + fontSize * (extraLines * LINE_SPACING);
4106+
}
40994107
} else {
41004108
var isInside = insideTicklabelposition(ax);
41014109

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"data": [
3+
{
4+
"mode": "markers",
5+
"x": [
6+
100,
7+
200,
8+
300,
9+
400,
10+
500
11+
],
12+
"y": [
13+
200,
14+
300,
15+
400,
16+
500,
17+
600
18+
],
19+
"type": "scatter"
20+
},
21+
{
22+
"mode": "markers",
23+
"x": [
24+
100,
25+
200,
26+
300,
27+
400,
28+
500
29+
],
30+
"y": [
31+
200,
32+
300,
33+
400,
34+
500,
35+
600
36+
],
37+
"type": "scatter",
38+
"xaxis": "x2",
39+
"yaxis": "y2"
40+
},
41+
{
42+
"mode": "markers",
43+
"x": [
44+
100,
45+
200,
46+
300,
47+
400,
48+
500
49+
],
50+
"y": [
51+
200,
52+
300,
53+
400,
54+
500,
55+
600
56+
],
57+
"type": "scatter",
58+
"xaxis": "x3",
59+
"yaxis": "y3"
60+
},
61+
{
62+
"mode": "markers",
63+
"x": [
64+
100,
65+
200,
66+
300,
67+
400,
68+
500
69+
],
70+
"y": [
71+
200,
72+
300,
73+
400,
74+
500,
75+
600
76+
],
77+
"type": "scatter",
78+
"xaxis": "x4",
79+
"yaxis": "y4"
80+
}
81+
],
82+
"layout": {
83+
"width": 800,
84+
"legend": {
85+
"visible": false
86+
},
87+
"grid": {
88+
"rows": 2,
89+
"columns": 2,
90+
"pattern": "independent",
91+
"xgap": 0.4
92+
},
93+
"xaxis": {
94+
"title": {
95+
"text": "______xaxis______",
96+
"standoff": 0
97+
},
98+
"side": "top",
99+
"automargin": true
100+
},
101+
"xaxis3": {
102+
"title": {
103+
"text": "¯¯¯¯¯¯xaxis3¯¯¯¯¯¯",
104+
"standoff": 0
105+
},
106+
"automargin": true
107+
},
108+
"yaxis": {
109+
"title": {
110+
"text": "______yaxis______",
111+
"standoff": 0
112+
},
113+
"automargin": true
114+
},
115+
"yaxis3": {
116+
"title": {
117+
"text": "¯¯¯¯¯¯yaxis3¯¯¯¯¯¯",
118+
"standoff": 0
119+
},
120+
"side": "right",
121+
"automargin": true
122+
},
123+
"xaxis2": {
124+
"title": {
125+
"text": "xaxis2<br>some<br>more<br>______text______",
126+
"standoff": 0
127+
},
128+
"side": "top",
129+
"automargin": true
130+
},
131+
"xaxis4": {
132+
"title": {
133+
"text": "¯¯¯¯¯¯xaxis4¯¯¯¯¯¯<br>some<br>more<br>text",
134+
"standoff": 0
135+
},
136+
"side": "bottom",
137+
"automargin": true
138+
},
139+
"yaxis2": {
140+
"title": {
141+
"text": "¯¯¯¯¯¯yaxis2¯¯¯¯¯¯<br>some<br>more<br>text",
142+
"standoff": 0
143+
},
144+
"side": "right",
145+
"automargin": true
146+
},
147+
"yaxis4": {
148+
"title": {
149+
"text": "yaxis4<br>some<br>more<br>______text______",
150+
"standoff": 0
151+
},
152+
"automargin": true
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)