Skip to content

Commit 085c2b7

Browse files
committed
Adjust API to have both autoshift and shift params
1 parent 6064e5a commit 085c2b7

File tree

10 files changed

+85
-44
lines changed

10 files changed

+85
-44
lines changed

src/plot_api/subroutines.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ function lsInner(gd) {
240240
}
241241

242242
function yLinePathFree(x) {
243-
if(typeof(ya.shift) === 'number') {
244-
x += ya.shift;
245-
} else if(ya._shift !== undefined) {
243+
if(ya._shift !== undefined) {
246244
x += ya._shift;
247245
}
248246
return 'M' + x + ',' + ya._offset + 'v' + ya._length;

src/plots/cartesian/axes.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ axes.draw = function(gd, arg, opts) {
22512251
var fullAxList = axes.list(gd);
22522252
// Get the list of the overlaying axis for all 'shift' axes
22532253
var overlayingShiftedAx = fullAxList.filter(function(ax) {
2254-
return ax.shift === true;
2254+
return ax.autoshift === true;
22552255
}).map(function(ax) {
22562256
return ax.overlaying;
22572257
});
@@ -2272,7 +2272,7 @@ axes.draw = function(gd, arg, opts) {
22722272
var axDone = axes.drawOne(gd, ax, opts);
22732273

22742274
if(ax._shiftPusher) {
2275-
incrementShift(ax, ax._fullDepth, axShifts);
2275+
incrementShift(ax, ax._fullDepth, axShifts, true);
22762276
}
22772277
ax._r = ax.range.slice();
22782278
ax._rl = Lib.simpleMap(ax._r, ax.r2l);
@@ -2328,16 +2328,18 @@ axes.drawOne = function(gd, ax, opts) {
23282328
// this happens when updating matched group with 'missing' axes
23292329
if(!mainPlotinfo) return;
23302330

2331-
ax._shiftPusher = ax.shift === true ||
2331+
ax._shiftPusher = ax.autoshift === true ||
23322332
overlayingShiftedAx.indexOf(ax._id) !== -1 ||
23332333
overlayingShiftedAx.indexOf(ax.overlaying) !== -1;
23342334
// An axis is also shifted by 1/2 of its own linewidth and inside tick length if applicable
2335+
// as well as its manually specified `shift` val if we're in the context of `autoshift`
23352336
if(ax._shiftPusher & ax.anchor === 'free') {
23362337
var selfPush = (ax.linewidth / 2 || 0);
23372338
if(ax.ticks === 'inside') {
23382339
selfPush += ax.ticklen;
23392340
}
2340-
incrementShift(ax, selfPush, axShifts);
2341+
incrementShift(ax, selfPush, axShifts, true);
2342+
incrementShift(ax, (ax.shift || 0), axShifts, false);
23412343
}
23422344

23432345
// Somewhat inelegant way of making sure that the shift value is only updated when the
@@ -2608,7 +2610,6 @@ axes.drawOne = function(gd, ax, opts) {
26082610

26092611
var axDepth = 0;
26102612
var titleDepth = 0;
2611-
var multAxisPad = 3; // TODO: Expose as a param to allow user to specify padding between axes
26122613
if(ax._shiftPusher) {
26132614
axDepth = Math.max(
26142615
outsideTickLen,
@@ -2621,7 +2622,7 @@ axes.drawOne = function(gd, ax, opts) {
26212622
}
26222623
}
26232624

2624-
ax._fullDepth = Math.max(axDepth, titleDepth) + multAxisPad;
2625+
ax._fullDepth = Math.max(axDepth, titleDepth);
26252626
}
26262627

26272628
if(ax.automargin) {
@@ -4274,10 +4275,15 @@ function hideCounterAxisInsideTickLabels(ax, opts) {
42744275
}
42754276
}
42764277

4277-
function incrementShift(ax, shiftVal, axShifts) {
4278+
function incrementShift(ax, shiftVal, axShifts, normalize) {
42784279
// Need to set 'overlay' for anchored axis
42794280
var overlay = ((ax.anchor !== 'free') && ((ax.overlaying === undefined) || (ax.overlaying === false))) ? ax._id : ax.overlaying;
4280-
var shiftValAdj = ax.side === 'right' ? shiftVal : -shiftVal;
4281+
var shiftValAdj;
4282+
if(normalize) {
4283+
shiftValAdj = ax.side === 'right' ? shiftVal : -shiftVal;
4284+
} else {
4285+
shiftValAdj = shiftVal;
4286+
}
42814287
if(!(overlay in axShifts)) {
42824288
axShifts[overlay] = {};
42834289
}
@@ -4288,7 +4294,7 @@ function incrementShift(ax, shiftVal, axShifts) {
42884294
}
42894295

42904296
function setShiftVal(ax, axShifts) {
4291-
return ax.shift === true ?
4297+
return ax.autoshift === true ?
42924298
axShifts[ax.overlaying][ax.side] :
42934299
(ax.shift || 0);
42944300
}

src/plots/cartesian/layout_attributes.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,15 +1003,25 @@ module.exports = {
10031003
'Only has an effect if `anchor` is set to *free*.'
10041004
].join(' ')
10051005
},
1006-
shift: {
1007-
valType: 'any',
1006+
autoshift: {
1007+
valType: 'boolean',
10081008
dflt: false,
10091009
editType: 'plot',
10101010
description: [
1011-
'Shift the positioning of this axis in pixels relative to where it',
1012-
'would have been given the default `position`.',
1013-
'If set to `true` then the axis will be automatically shifted so as not',
1014-
'to overlap with other axes with the same `overlaying` value.',
1011+
'Automatically reposition the axis to avoid',
1012+
'overlap with other axes with the same `overlaying` value.',
1013+
'This repositioning will account for any `shift` amount applied to other',
1014+
'axes on the same side with `autoshift=true`.',
1015+
'Only has an effect if `anchor` is set to *free*.',
1016+
].join(' ')
1017+
},
1018+
shift: {
1019+
valType: 'number',
1020+
editType: 'plot',
1021+
description: [
1022+
'Moves the axis a given number of pixels from where it would have been otherwise.',
1023+
'If `shiftauto=true`, then this defaults to a padding of `-3` if `side=left`',
1024+
'and `+3` if `side=right`. Defaults to `0` if `shiftauto=false`.',
10151025
'Only has an effect if `anchor` is set to *free*.'
10161026
].join(' ')
10171027
},

src/plots/cartesian/position_defaults.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function handlePositionDefaults(containerIn, containerOut, coer
1111
var letter = options.letter;
1212
var grid = options.grid;
1313
var overlayingDomain = options.overlayingDomain;
14-
var dfltAnchor, dfltDomain, dfltSide, dfltPosition, dfltShift, dfltAutomargin;
14+
var dfltAnchor, dfltDomain, dfltSide, dfltPosition, dfltAutoshift, dfltShift, dfltAutomargin;
1515

1616
if(grid) {
1717
dfltDomain = grid._domains[letter][grid._axisMap[containerOut._id]];
@@ -27,7 +27,8 @@ module.exports = function handlePositionDefaults(containerIn, containerOut, coer
2727
dfltAnchor = dfltAnchor || (isNumeric(containerIn.position) ? 'free' : (counterAxes[0] || 'free'));
2828
dfltSide = dfltSide || (letter === 'x' ? 'bottom' : 'left');
2929
dfltPosition = dfltPosition || 0;
30-
dfltShift = dfltShift || false;
30+
dfltAutoshift = dfltAutoshift || false;
31+
dfltShift = dfltShift || 0;
3132
dfltAutomargin = dfltAutomargin || false;
3233

3334
var anchor = Lib.coerce(containerIn, containerOut, {
@@ -47,14 +48,14 @@ module.exports = function handlePositionDefaults(containerIn, containerOut, coer
4748
}, 'side');
4849

4950
if(anchor === 'free') {
50-
var shift = coerce('shift', dfltShift);
51-
if(shift !== false && shift !== undefined) {
51+
var autoshift = coerce('autoshift', dfltAutoshift);
52+
if(autoshift === true) {
5253
dfltPosition = side === 'left' ? overlayingDomain[0] : overlayingDomain[1];
53-
}
54-
if(shift === true) {
5554
dfltAutomargin = containerOut.automargin ? containerOut.automargin : true;
55+
dfltShift = side === 'left' ? -3 : 3;
5656
}
5757
coerce('position', dfltPosition);
58+
coerce('shift', dfltShift);
5859
}
5960
coerce('automargin', dfltAutomargin);
6061

src/plots/plots.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,10 +1965,10 @@ function needsRedrawForShift(gd) {
19651965
if(isShift === true) {
19661966
return isShift;
19671967
}
1968-
isShift = axList[ax].shift;
1968+
isShift = (axList[ax].autoshift === true || axList[ax].shift !== undefined) ? true : false;
19691969
}
19701970
return isShift;
1971-
};
1971+
}
19721972

19731973
plots.doAutoMargin = function(gd) {
19741974
var fullLayout = gd._fullLayout;

test/image/mocks/zz-mult-yaxes-manual-shift.json

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@
5858
"name": "yaxis4 data",
5959
"yaxis": "y4",
6060
"type": "scatter"
61+
},
62+
{
63+
"x": [
64+
3,
65+
4,
66+
5
67+
],
68+
"y": [
69+
400,
70+
500,
71+
600
72+
],
73+
"name": "yaxis5 data",
74+
"yaxis": "y5",
75+
"type": "scatter"
6176
}
6277
],
6378
"layout": {
@@ -78,16 +93,24 @@
7893
"anchor": "free",
7994
"overlaying": "y",
8095
"showline": true,
81-
"shift": -100,
82-
"position": 0.2
96+
"autoshift": true,
97+
"shift": -25
8398
},
8499
"yaxis4": {
85100
"title": {"text": "yaxis4 title"},
86101
"anchor": "free",
87102
"overlaying": "y",
88103
"showline": true,
104+
"autoshift": true
105+
},
106+
"yaxis5": {
107+
"title": {"text": "yaxis5 title"},
108+
"anchor": "free",
109+
"overlaying": "y",
110+
"position": 0.8,
89111
"shift": 100,
90-
"side": "right"
112+
"showline": true,
113+
"side": "left"
91114
}
92115
}
93116
}

test/image/mocks/zz-mult-yaxes-redraw.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@
8080
"anchor": "free",
8181
"overlaying": "y",
8282
"showline": true,
83-
"shift": true,
83+
"autoshift": true,
8484
"side": "right",
8585
"title": {"text": "axis 3"}
8686
},
8787
"yaxis4": {
8888
"anchor": "free",
8989
"overlaying": "y",
9090
"showline": true,
91-
"shift": true,
91+
"autoshift": true,
9292
"side": "left",
9393
"title": {"text": "axis 4"}
9494
}

test/image/mocks/zz-mult-yaxes-simple.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"anchor": "free",
123123
"overlaying": "y",
124124
"showline": true,
125-
"shift": true,
125+
"autoshift": true,
126126
"linewidth": 20,
127127
"ticklen": 30,
128128
"side": "right",
@@ -138,7 +138,7 @@
138138
"anchor": "free",
139139
"overlaying": "y",
140140
"showline": true,
141-
"shift": true,
141+
"autoshift": true,
142142
"side": "right",
143143
"linewidth": 0,
144144
"ticklen": 20,
@@ -149,7 +149,7 @@
149149
"anchor": "free",
150150
"overlaying": "y",
151151
"showline": true,
152-
"shift": true,
152+
"autoshift": true,
153153
"linewidth": 0,
154154
"tickformat" : ".2f",
155155
"title": {"text": "yaxis 5"}
@@ -158,7 +158,7 @@
158158
"anchor": "free",
159159
"overlaying": "y",
160160
"showline": true,
161-
"shift": true,
161+
"autoshift": true,
162162
"linewidth": 0,
163163
"side": "right",
164164
"tickfont": {"size": 24},

test/image/mocks/zz-mult-yaxes-subplots-stacked.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@
9393
"anchor": "free",
9494
"overlaying": "y",
9595
"showline": true,
96-
"shift": true
96+
"autoshift": true
9797
},
9898
"yaxis4": {
9999
"title": {"text": "yaxis4 title"},
100100
"anchor": "free",
101101
"overlaying": "y2",
102102
"showline": true,
103-
"shift": true
103+
"autoshift": true
104104
},
105105
"yaxis5": {
106106
"title": {"text": "yaxis5 title"},
107107
"anchor": "free",
108108
"overlaying": "y",
109109
"showline": true,
110-
"shift": true
110+
"autoshift": true
111111
}
112112
}
113113
}

test/jasmine/tests/axes_test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,27 +1433,30 @@ describe('Test axes', function() {
14331433
expect(layoutOut.xaxis11.rangebreaks[0].enabled).toBe(false, 'reject true');
14341434
});
14351435

1436-
it('should coerce shift only if anchor is *free*', function() {
1436+
it('should coerce autoshift and shift only if anchor is *free*', function() {
14371437
layoutIn = {
14381438
xaxis: {},
14391439
yaxis: {anchor: 'free'}
14401440
};
14411441
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
1442-
expect(layoutOut.yaxis.shift).toBe(false);
1442+
expect(layoutOut.yaxis.autoshift).toBe(false);
1443+
expect(layoutOut.yaxis.shift).toEqual(0);
14431444

1444-
layoutIn.yaxis.shift = true;
1445+
layoutIn.yaxis.autoshift = true;
14451446
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
1446-
expect(layoutOut.yaxis.shift).toBe(true);
1447+
expect(layoutOut.yaxis.autoshift).toBe(true);
1448+
expect(layoutOut.yaxis.shift).toEqual(-3);
14471449

14481450
layoutIn.yaxis.anchor = 'x';
14491451
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
1452+
expect(layoutOut.yaxis.autoshift).toBeUndefined();
14501453
expect(layoutOut.yaxis.shift).toBeUndefined();
14511454
});
14521455

14531456
it('should set automargin to *true* when shift is *true*', function() {
14541457
layoutIn = {
14551458
xaxis: {},
1456-
yaxis: {shift: true, anchor: 'free'}
1459+
yaxis: {autoshift: true, anchor: 'free'}
14571460
};
14581461
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
14591462
expect(layoutOut.yaxis.automargin).toBe(true);
@@ -1472,7 +1475,7 @@ describe('Test axes', function() {
14721475
layoutIn = {
14731476
xaxis: {domain: [0.2, 0.5]},
14741477
yaxis: {},
1475-
yaxis2: {shift: true, anchor: 'free', overlaying: 'y'}
1478+
yaxis2: {autoshift: true, anchor: 'free', overlaying: 'y'}
14761479
};
14771480

14781481
layoutOut._subplots.cartesian.push('xy2');

0 commit comments

Comments
 (0)