Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab935ac

Browse filesBrowse files
committed
Move calculation of shape shift into a dedicated function
1 parent 4bbb669 commit ab935ac
Copy full SHA for ab935ac

File tree

Expand file treeCollapse file tree

1 file changed

+25
-27
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-27
lines changed

‎src/components/shapes/helpers.js

Copy file name to clipboardExpand all lines: src/components/shapes/helpers.js
+25-27Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ exports.extractPathCoords = function(path, paramsToUse, isRaw) {
5656
exports.getDataToPixel = function(gd, axis, shift, isVertical, refType) {
5757
var gs = gd._fullLayout._size;
5858
var dataToPixel;
59-
shift = shift || 0;
6059

6160
if(axis) {
6261
if(refType === 'domain') {
@@ -67,14 +66,7 @@ exports.getDataToPixel = function(gd, axis, shift, isVertical, refType) {
6766
var d2r = exports.shapePositionToRange(axis);
6867

6968
dataToPixel = function(v) {
70-
var shiftPixels = 0;
71-
if(axis.type === 'category' || axis.type === 'multicategory') {
72-
if(isVertical) {
73-
shiftPixels = ((axis.r2p(1) - axis.r2p(0)) * shift);
74-
} else {
75-
shiftPixels = axis.r2p(0.5) * shift;
76-
}
77-
}
69+
var shiftPixels = getPixelShift(axis, shift);
7870
return axis._offset + axis.r2p(d2r(v, true)) + shiftPixels;
7971
};
8072

@@ -188,10 +180,10 @@ exports.getPathString = function(gd, options) {
188180
var ya = Axes.getFromId(gd, options.yref);
189181
var gs = gd._fullLayout._size;
190182
var x2r, x2p, y2r, y2p;
191-
var xShiftStart = 0;
192-
var xShiftEnd = 0;
193-
var yShiftStart = 0;
194-
var yShiftEnd = 0;
183+
var xShiftStart = getPixelShift(xa, options.x0shift);
184+
var xShiftEnd = getPixelShift(xa, options.x1shift);
185+
var yShiftStart = getPixelShift(ya, options.y0shift);
186+
var yShiftEnd = getPixelShift(ya, options.y1shift);
195187
var x0, x1, y0, y1;
196188

197189
if(xa) {
@@ -200,11 +192,6 @@ exports.getPathString = function(gd, options) {
200192
} else {
201193
x2r = exports.shapePositionToRange(xa);
202194
x2p = function(v) { return xa._offset + xa.r2p(x2r(v, true)); };
203-
if(xa.type === 'category' || xa.type === 'multicategory') {
204-
var shiftUnitX = xa.r2p(0.5);
205-
xShiftStart = shiftUnitX * options.x0shift;
206-
xShiftEnd = shiftUnitX * options.x1shift;
207-
}
208195
}
209196
} else {
210197
x2p = function(v) { return gs.l + gs.w * v; };
@@ -216,11 +203,6 @@ exports.getPathString = function(gd, options) {
216203
} else {
217204
y2r = exports.shapePositionToRange(ya);
218205
y2p = function(v) { return ya._offset + ya.r2p(y2r(v, true)); };
219-
if(ya.type === 'category' || ya.type === 'multicategory') {
220-
var shiftUnitY = ya.r2p(0) - ya.r2p(1);
221-
yShiftStart = shiftUnitY * options.y0shift;
222-
yShiftEnd = shiftUnitY * options.y1shift;
223-
}
224206
}
225207
} else {
226208
y2p = function(v) { return gs.t + gs.h * (1 - v); };
@@ -242,11 +224,11 @@ exports.getPathString = function(gd, options) {
242224

243225
if(options.ysizemode === 'pixel') {
244226
var yAnchorPos = y2p(options.yanchor);
245-
y0 = yAnchorPos - options.y0 - yShiftStart;
246-
y1 = yAnchorPos - options.y1 - yShiftEnd;
227+
y0 = yAnchorPos - options.y0 + yShiftStart;
228+
y1 = yAnchorPos - options.y1 + yShiftEnd;
247229
} else {
248-
y0 = y2p(options.y0) - yShiftStart;
249-
y1 = y2p(options.y1) - yShiftEnd;
230+
y0 = y2p(options.y0) + yShiftStart;
231+
y1 = y2p(options.y1) + yShiftEnd;
250232
}
251233

252234
if(type === 'line') return 'M' + x0 + ',' + y0 + 'L' + x1 + ',' + y1;
@@ -301,3 +283,19 @@ function convertPath(options, x2p, y2p) {
301283
return segmentType + paramString;
302284
});
303285
}
286+
287+
function getPixelShift(axis, shift) {
288+
shift = shift || 0;
289+
var shiftPixels = 0;
290+
if(axis) {
291+
var isVertical = axis._id.charAt(0) === 'y';
292+
if(axis.type === 'category' || axis.type === 'multicategory') {
293+
if(isVertical) {
294+
shiftPixels = ((axis.r2p(1) - axis.r2p(0)) * shift);
295+
} else {
296+
shiftPixels = axis.r2p(0.5) * shift;
297+
}
298+
}
299+
}
300+
return shiftPixels;
301+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.