Skip to content

Commit dab3a1f

Browse files
committed
fix angular multi-line tick labels positioning
... by extracting getHeight axis label fn from Axes.drawLabels, and defining a custom fn for angular tick labels.
1 parent 7e55b51 commit dab3a1f

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

src/plots/cartesian/axes.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,7 @@ axes.makeTickPath = function(ax, shift, sgn, len) {
21742174
* - {fn} xFn
21752175
* - {fn} yFn
21762176
* - {fn} anchorFn
2177+
* - {fn} heightFn
21772178
* - {number} labelStandoff (gap parallel to ticks)
21782179
* - {number} labelShift (gap perpendicular to ticks)
21792180
*/
@@ -2218,6 +2219,11 @@ axes.makeLabelFns = function(ax, shift, angle) {
22182219
}
22192220
return (a * flipIt < 0) ? 'end' : 'start';
22202221
};
2222+
out.heightFn = function(d, a, h) {
2223+
return (a < -60 || a > 60) ? -0.5 * h :
2224+
ax.side === 'top' ? -h :
2225+
0;
2226+
};
22212227
} else if(axLetter === 'y') {
22222228
flipIt = ax.side === 'right' ? 1 : -1;
22232229
x0 = labelStandoff;
@@ -2232,6 +2238,12 @@ axes.makeLabelFns = function(ax, shift, angle) {
22322238
}
22332239
return ax.side === 'right' ? 'start' : 'end';
22342240
};
2241+
out.heightFn = function(d, a, h) {
2242+
a *= ax.side === 'left' ? 1 : -1;
2243+
return a < -30 ? -h :
2244+
a < 30 ? -0.5 * h :
2245+
0;
2246+
};
22352247
}
22362248

22372249
return out;
@@ -2466,26 +2478,6 @@ axes.drawLabels = function(gd, ax, opts) {
24662478
});
24672479
}
24682480

2469-
// How much to shift a multi-line label to center it vertically.
2470-
function getAnchorHeight(lineCount, lineHeight, angle) {
2471-
var h = (lineCount - 1) * lineHeight;
2472-
if(axLetter === 'x') {
2473-
if(angle < -60 || 60 < angle) {
2474-
return -0.5 * h;
2475-
} else if(ax.side === 'top') {
2476-
return -h;
2477-
}
2478-
} else {
2479-
angle *= ax.side === 'left' ? 1 : -1;
2480-
if(angle < -30) {
2481-
return -h;
2482-
} else if(angle < 30) {
2483-
return -0.5 * h;
2484-
}
2485-
}
2486-
return 0;
2487-
}
2488-
24892481
function positionLabels(s, angle) {
24902482
s.each(function(d) {
24912483
var thisLabel = d3.select(this);
@@ -2498,11 +2490,10 @@ axes.drawLabels = function(gd, ax, opts) {
24982490
(labelFns.yFn(d) - d.fontSize / 2) + ')') :
24992491
'');
25002492

2501-
var anchorHeight = getAnchorHeight(
2502-
svgTextUtils.lineCount(thisLabel),
2503-
LINE_SPACING * d.fontSize,
2504-
isNumeric(angle) ? +angle : 0
2505-
);
2493+
// how much to shift a multi-line label to center it vertically.
2494+
var nLines = svgTextUtils.lineCount(thisLabel);
2495+
var lineHeight = LINE_SPACING * d.fontSize;
2496+
var anchorHeight = labelFns.heightFn(d, isNumeric(angle) ? +angle : 0, (nLines - 1) * lineHeight);
25062497

25072498
if(anchorHeight) {
25082499
transform += ' translate(0, ' + anchorHeight + ')';

src/plots/polar/polar.js

+5
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
575575
(cos > 0 ? 'start' : 'end');
576576
};
577577

578+
labelFns.heightFn = function(d, a, h) {
579+
var rad = t2g(d);
580+
return -0.5 * (1 + Math.sin(rad)) * h;
581+
};
582+
578583
var newTickLayout = strTickLayout(angularLayout);
579584
if(_this.angularTickLayout !== newTickLayout) {
580585
layers['angular-axis'].selectAll('.' + ax._id + 'tick').remove();
Loading

test/image/mocks/polar_long-category-angular-labels.json

+45-5
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,73 @@
99
"width": 4
1010
},
1111
"type": "scatterpolar",
12-
"name": "Backfile"
12+
"name": "Backfile",
13+
"legendgroup": "Backfile"
1314
},
1415
{
1516
"r": [0.98, 0.98, 0.83, 0.89, 1, 1, 0.99, 0.96, 1, 0, 0, 0.98],
1617
"mode": "lines",
17-
"theta": ["Abstracts", "Affiliations", "Award Numbers", "Funders", "Licenses", "Open References", "Orcids", "References", "Resource Links", "Similarity Checking", "Update Policies", "Abstracts" ],
18+
"theta": ["Abstracts", "Affiliations", "Award Numbers", "Funders", "Licenses", "Open References", "Orcids", "References", "Resource Links", "Similarity Checking", "Update Policies", "Abstracts"],
19+
"line": {
20+
"color": "blue",
21+
"width": 2
22+
},
23+
"type": "scatterpolar",
24+
"name": "Current",
25+
"legendgroup": "Current"
26+
},
27+
{
28+
"r": [0.97, 0.97, 0.75, 0.83, 1, 1, 0.45, 0.97, 1, 0, 0.01, 0.97 ],
29+
"mode": "lines",
30+
"theta": ["Abstracts<br>Abstracts", "Affiliations<br>Affiliations", "Award<br>Numbers", "Funders<br>Funders", "Licenses<br>Licenses", "Open<br>References", "Orcids<br>Orcids<br>Orcids", "References<br>References", "Resource<br>Links", "Similarity<br>Checking", "Update<br>Policies", "Abstracts<br>Abstracts"],
31+
"line": {
32+
"color": "orangered",
33+
"width": 4
34+
},
35+
"type": "scatterpolar",
36+
"name": "Backfile",
37+
"legendgroup": "Backfile",
38+
"showlegend": false,
39+
"subplot": "polar2"
40+
},
41+
{
42+
"r": [0.98, 0.98, 0.83, 0.89, 1, 1, 0.99, 0.96, 1, 0, 0, 0.98],
43+
"mode": "lines",
44+
"theta": ["Abstracts<br>Abstracts", "Affiliations<br>Affiliations", "Award<br>Numbers", "Funders<br>Funders", "Licenses<br>Licenses", "Open<br>References", "Orcids<br>Orcids<br>Orcids", "References<br>References", "Resource<br>Links", "Similarity<br>Checking", "Update<br>Policies", "Abstracts<br>Abstracts"],
1845
"line": {
1946
"color": "blue",
2047
"width": 2
2148
},
2249
"type": "scatterpolar",
23-
"name": "Current"
50+
"name": "Current",
51+
"legendgroup": "Current",
52+
"showlegend": false,
53+
"subplot": "polar2"
2454
}
2555
],
2656
"layout": {
57+
"grid": {"rows": 2, "columns": 1, "ygap": 0.15},
58+
"height": 800,
2759
"polar": {
60+
"domain": {"row": 0, "column": 0},
61+
"angularaxis": {
62+
"rotation": 90,
63+
"direction": "clockwise"
64+
}
65+
},
66+
"polar2": {
67+
"domain": {"row": 1, "column": 0},
2868
"angularaxis": {
2969
"rotation": 90,
3070
"direction": "clockwise"
3171
}
3272
},
33-
"showlegend": true,
3473
"legend": {
3574
"y": 0,
3675
"yanchor": "top",
3776
"x": 0,
38-
"xanchor": "left"
77+
"xanchor": "left",
78+
"tracegroupgap": 0
3979
},
4080
"title": {
4181
"text": "eLife Sciences Publications<br>Ltd: journal-article (8.62 - 7.95 = 0.67)"

0 commit comments

Comments
 (0)