Skip to content

Commit 8b1f5bd

Browse files
authored
Merge pull request #7006 from my-tien/shift_axis_label
Add `ticklabelstandoff` and `ticklabelshift` to cartesian axes
2 parents 8c47c16 + 26315dc commit 8b1f5bd

File tree

14 files changed

+254
-7
lines changed

14 files changed

+254
-7
lines changed

draftlogs/7006_add.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add property `ticklabelstandoff` and `ticklabelshift` to cartesian axes to adjust positioning of tick labels [[#7006](https://github.com/plotly/plotly.js/pull/7006)]

src/components/colorbar/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
111111
var font = layout.font;
112112
var opts = {
113113
noAutotickangles: true,
114+
noTicklabelshift: true,
115+
noTicklabelstandoff: true,
114116
outerTicks: false,
115117
font: font
116118
};

src/plots/cartesian/axes.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,20 +2968,39 @@ axes.makeTransTickFn = function(ax) {
29682968

29692969
axes.makeTransTickLabelFn = function(ax) {
29702970
var uv = getTickLabelUV(ax);
2971+
var shift = ax.ticklabelshift || 0;
2972+
var standoff = ax.ticklabelstandoff || 0;
2973+
29712974
var u = uv[0];
29722975
var v = uv[1];
29732976

2977+
var isReversed = ax.range[0] > ax.range[1];
2978+
var labelsInside = ax.ticklabelposition && ax.ticklabelposition.indexOf('inside') !== -1;
2979+
var labelsOutside = !labelsInside;
2980+
2981+
if(shift) {
2982+
var shiftSign = isReversed ? -1 : 1;
2983+
shift = shift * shiftSign;
2984+
}
2985+
if(standoff) {
2986+
var side = ax.side;
2987+
var standoffSign = (
2988+
(labelsInside && (side === 'top' || side === 'left')) ||
2989+
(labelsOutside && (side === 'bottom' || side === 'right'))
2990+
) ? 1 : -1;
2991+
standoff = standoff * standoffSign;
2992+
}
29742993
return ax._id.charAt(0) === 'x' ?
29752994
function(d) {
29762995
return strTranslate(
2977-
u + ax._offset + ax.l2p(getPosX(d)),
2978-
v
2996+
u + ax._offset + ax.l2p(getPosX(d)) + shift,
2997+
v + standoff
29792998
);
29802999
} :
29813000
function(d) {
29823001
return strTranslate(
2983-
v,
2984-
u + ax._offset + ax.l2p(getPosX(d))
3002+
v + standoff,
3003+
u + ax._offset + ax.l2p(getPosX(d)) + shift
29853004
);
29863005
};
29873006
};

src/plots/cartesian/layout_attributes.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,29 @@ module.exports = {
698698
'In other cases the default is *hide past div*.'
699699
].join(' ')
700700
},
701+
ticklabelshift: {
702+
valType: 'integer',
703+
dflt: 0,
704+
editType: 'ticks',
705+
description: [
706+
'Shifts the tick labels by the specified number of pixels in parallel to the axis.',
707+
'Positive values move the labels in the positive direction of the axis.'
708+
].join(' ')
709+
},
710+
ticklabelstandoff: {
711+
valType: 'integer',
712+
dflt: 0,
713+
editType: 'ticks',
714+
description: [
715+
'Sets the standoff distance (in px) between the axis tick labels and their default position.',
716+
'A positive `ticklabelstandoff` moves the labels farther away from the plot area',
717+
'if `ticklabelposition` is *outside*, and deeper into the plot area if',
718+
'`ticklabelposition` is *inside*. A negative `ticklabelstandoff` works in the opposite',
719+
'direction, moving outside ticks towards the plot area and inside ticks towards',
720+
'the outside. If the negative value is large enough, inside ticks can even end up',
721+
'outside and vice versa.'
722+
].join(' ')
723+
},
701724
mirror: {
702725
valType: 'enumerated',
703726
values: [true, 'ticks', false, 'all', 'allticks'],

src/plots/cartesian/tick_label_defaults.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe
1616

1717
var showTickLabels = coerce('showticklabels');
1818
if(showTickLabels) {
19+
if(!options.noTicklabelshift) {
20+
coerce('ticklabelshift');
21+
}
22+
if(!options.noTicklabelstandoff) {
23+
coerce('ticklabelstandoff');
24+
}
1925
var font = options.font || {};
2026
var contColor = containerOut.color;
2127
var position = containerOut.ticklabelposition || '';

src/plots/gl3d/layout/axis_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) {
4444
noAutotickangles: true,
4545
noTickson: true,
4646
noTicklabelmode: true,
47+
noTicklabelshift: true,
48+
noTicklabelstandoff: true,
4749
noTicklabelstep: true,
4850
noTicklabelposition: true,
4951
noTicklabeloverflow: true,

src/plots/polar/layout_defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ function handleDefaults(contIn, contOut, coerce, opts) {
186186
size: dfltFontSize,
187187
family: dfltFontFamily
188188
},
189-
noAutotickangles: axName === 'angularaxis'
189+
noAutotickangles: axName === 'angularaxis',
190+
noTicklabelshift: true,
191+
noTicklabelstandoff: true
190192
});
191193

192194
handleTickMarkDefaults(axIn, axOut, coerceAxis, {outerTicks: true});

src/plots/smith/layout_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ function handleDefaults(contIn, contOut, coerce, opts) {
9090

9191
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type, {
9292
noAutotickangles: true,
93+
noTicklabelshift: true,
94+
noTicklabelstandoff: true,
9395
noTicklabelstep: true,
9496
noAng: !isRealAxis,
9597
noExp: true,

src/plots/ternary/layout_defaults.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ function handleAxisDefaults(containerIn, containerOut, options, ternaryLayoutOut
9191

9292
handleTickValueDefaults(containerIn, containerOut, coerce, 'linear');
9393
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, 'linear');
94-
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear', { noAutotickangles: true });
94+
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear', {
95+
noAutotickangles: true,
96+
noTicklabelshift: true,
97+
noTicklabelstandoff: true
98+
});
9599
handleTickMarkDefaults(containerIn, containerOut, coerce,
96100
{ outerTicks: true });
97101

src/traces/carpet/ab_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function mimickAxisDefaults(traceIn, traceOut, fullLayout, dfltColor) {
3131

3232
var defaultOptions = {
3333
noAutotickangles: true,
34+
noTicklabelshift: true,
35+
noTicklabelstandoff: true,
3436
noTicklabelstep: true,
3537
tickfont: 'x',
3638
id: axLetter + 'axis',

0 commit comments

Comments
 (0)