Skip to content

Commit

Permalink
Fix backwards-compatibility
Browse files Browse the repository at this point in the history
Previously, although I used [30, 90] as default autotickangles, this did not reproduce old images, since the condition for choosing between the two was also different.
  • Loading branch information
my-tien committed Nov 24, 2023
1 parent 7ff8d92 commit 9473029
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 76 deletions.
34 changes: 20 additions & 14 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3479,7 +3479,8 @@ axes.drawLabels = function(gd, ax, opts) {

var labelFns = opts.labelFns;
var tickAngle = opts.secondary ? 0 : ax.tickangle;
var autoTickAngles = ax.autotickangles || [30, 90];

var autoTickAngles = ax.autotickangles;
var prevAngle = (ax._prevTickAngles || {})[cls];

var tickLabels = opts.layer.selectAll('g.' + cls)
Expand Down Expand Up @@ -3782,19 +3783,24 @@ axes.drawLabels = function(gd, ax, opts) {
var pad = !isAligned ? 0 :
(ax.tickwidth || 0) + 2 * TEXTPAD;

var adjacent = tickSpacing;
var opposite = maxFontSize * 1.25 * maxLines;
var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
// sin(angle) = opposite / hypotenuse
var minAngle = Math.asin(opposite / hypotenuse) * (180 / Math.PI /* to degrees */);

var angle = autoTickAngles.find(function(angle) { return Math.abs(angle) >= minAngle; });
if(angle === undefined) {
// no angle larger than minAngle, just pick the largest angle
angle = autoTickAngles.reduce(
function(currentMax, nextAngle) { return Math.abs(currentMax) < Math.abs(nextAngle) ? nextAngle : currentMax; }
, autoTickAngles[0]
);
// old behavior for backwards-compatibility
var angle = ((tickSpacing < maxFontSize * 2.5) || ax.type === 'multicategory' || ax._name === 'realaxis') ? 90 : 30;
// autotickangles
if(autoTickAngles !== undefined) {
var adjacent = tickSpacing;
var opposite = maxFontSize * 1.25 * maxLines;
var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
// sin(angle) = opposite / hypotenuse
var minAngle = Math.asin(opposite / hypotenuse) * (180 / Math.PI /* to degrees */);

angle = autoTickAngles.find(function(angle) { return Math.abs(angle) >= minAngle; });
if(angle === undefined) {
// no angle larger than minAngle, just pick the largest angle
angle = autoTickAngles.reduce(
function(currentMax, nextAngle) { return Math.abs(currentMax) < Math.abs(nextAngle) ? nextAngle : currentMax; }
, autoTickAngles[0]
);
}
}
if(prevAngle !== undefined) {
angle = Math.abs(angle) > Math.abs(prevAngle) ? angle : prevAngle;
Expand Down
9 changes: 6 additions & 3 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,17 +806,20 @@ module.exports = {
description: [
'Sets the angle of the tick labels with respect to the horizontal.',
'For example, a `tickangle` of -90 draws the tick labels',
'vertically.'
'vertically. If `tickangle` is auto, the angle will be chosen from',
'autotickangles if provided. If `autotickangles` is not provided,',
'the angle will be 30 or 90 degrees.'
].join(' ')
},
autotickangles: {
valType: 'data_array',
dflt: [30, 90],
dflt: null,
editType: 'ticks',
description: [
'When `tickangle` is set to *auto*, it will be set to the first',
'angle in this array that is large enough to prevent label',
'overlap.'
'overlap. If undefined, tickangle *auto* will instead choose',
'between 30 and 90 degrees.'
].join(' ')
},
tickprefix: {
Expand Down
Loading

0 comments on commit 9473029

Please sign in to comment.