Skip to content

Commit 0e0de2f

Browse files
committed
Apply autotickangles also on axes with tickson="boundaries" and axes with showdividers=true
1 parent f673ce6 commit 0e0de2f

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/plots/cartesian/axes.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,27 +3748,54 @@ axes.drawLabels = function(gd, ax, opts) {
37483748
});
37493749
});
37503750

3751-
if((ax.tickson === 'boundaries' || ax.showdividers) && !opts.secondary) {
3751+
// autotickangles
3752+
// if there are dividers or ticks on boundaries, the labels will be in between and
3753+
// we need to prevent overlap with the next divider/tick. Else the labels will be on
3754+
// the ticks and we need to prevent overlap with the next label.
3755+
3756+
// TODO should secondary labels also fall into this fix-overlap regime?
3757+
var preventOverlapWithTick = (ax.tickson === 'boundaries' || ax.showdividers) && !opts.secondary;
3758+
3759+
var vLen = vals.length;
3760+
var tickSpacing = Math.abs((vals[vLen - 1].x - vals[0].x) * ax._m) / (vLen - 1);
3761+
3762+
var adjacent = preventOverlapWithTick ? tickSpacing / 2 : tickSpacing;
3763+
var opposite = preventOverlapWithTick ? ax.ticklen : maxFontSize * 1.25 * maxLines;
3764+
var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
3765+
var maxCos = adjacent / hypotenuse;
3766+
var autoTickAnglesRadians = ax.autotickangles.map(
3767+
function(degrees) { return degrees * Math.PI / 180; }
3768+
);
3769+
var angleRadians = autoTickAnglesRadians.find(
3770+
function(angle) { return Math.abs(Math.cos(angle)) <= maxCos; }
3771+
);
3772+
if(angleRadians === undefined) {
3773+
// no angle with smaller cosine than maxCos, just pick the angle with smallest cosine
3774+
angleRadians = autoTickAnglesRadians.reduce(
3775+
function(currentMax, nextAngle) {
3776+
return Math.abs(Math.cos(currentMax)) < Math.abs(Math.cos(nextAngle)) ? currentMax : nextAngle;
3777+
}
3778+
, autoTickAnglesRadians[0]
3779+
);
3780+
}
3781+
var newAngle = angleRadians * (180 / Math.PI /* to degrees */);
3782+
3783+
if(preventOverlapWithTick) {
37523784
var gap = 2;
37533785
if(ax.ticks) gap += ax.tickwidth / 2;
37543786

3755-
// TODO should secondary labels also fall into this fix-overlap regime?
3756-
37573787
for(i = 0; i < lbbArray.length; i++) {
37583788
var xbnd = vals[i].xbnd;
37593789
var lbb = lbbArray[i];
37603790
if(
37613791
(xbnd[0] !== null && (lbb.left - ax.l2p(xbnd[0])) < gap) ||
37623792
(xbnd[1] !== null && (ax.l2p(xbnd[1]) - lbb.right) < gap)
37633793
) {
3764-
autoangle = 90;
3794+
autoangle = newAngle;
37653795
break;
37663796
}
37673797
}
37683798
} else {
3769-
var vLen = vals.length;
3770-
var tickSpacing = Math.abs((vals[vLen - 1].x - vals[0].x) * ax._m) / (vLen - 1);
3771-
37723799
var ticklabelposition = ax.ticklabelposition || '';
37733800
var has = function(str) {
37743801
return ticklabelposition.indexOf(str) !== -1;
@@ -3779,29 +3806,7 @@ axes.drawLabels = function(gd, ax, opts) {
37793806
var isBottom = has('bottom');
37803807
var isAligned = isBottom || isLeft || isTop || isRight;
37813808
var pad = !isAligned ? 0 :
3782-
(ax.tickwidth || 0) + 2 * TEXTPAD;
3783-
3784-
// autotickangles
3785-
var adjacent = tickSpacing;
3786-
var opposite = maxFontSize * 1.25 * maxLines;
3787-
var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
3788-
var maxCos = adjacent / hypotenuse;
3789-
var autoTickAnglesRadians = ax.autotickangles.map(
3790-
function(degrees) { return degrees * Math.PI / 180; }
3791-
);
3792-
var angleRadians = autoTickAnglesRadians.find(
3793-
function(angle) { return Math.abs(Math.cos(angle)) <= maxCos; }
3794-
);
3795-
if(angleRadians === undefined) {
3796-
// no angle with smaller cosine than maxCos, just pick the angle with smallest cosine
3797-
angleRadians = autoTickAnglesRadians.reduce(
3798-
function(currentMax, nextAngle) {
3799-
return Math.abs(Math.cos(currentMax)) < Math.abs(Math.cos(nextAngle)) ? currentMax : nextAngle;
3800-
}
3801-
, autoTickAnglesRadians[0]
3802-
);
3803-
}
3804-
var newAngle = angleRadians * (180 / Math.PI /* to degrees */);
3809+
(ax.tickwidth || 0) + 2 * TEXTPAD;
38053810

38063811
for(i = 0; i < lbbArray.length - 1; i++) {
38073812
if(Lib.bBoxIntersect(lbbArray[i], lbbArray[i + 1], pad)) {

0 commit comments

Comments
 (0)