Skip to content

Commit b3896a2

Browse files
Simplification of dataToPixel code
Unfortunately the drag bug is back...
1 parent 89cbf11 commit b3896a2

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/components/shapes/draw.js

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ function setupDragElement(gd, shapePath, shapeOptions, index, shapeLayer, editHe
206206
var xRefType = Axes.getRefType(shapeOptions.xref);
207207
var ya = Axes.getFromId(gd, shapeOptions.yref);
208208
var yRefType = Axes.getRefType(shapeOptions.yref);
209-
var x2p = helpers.getDataToPixel(gd, xa, false, xRefType);
210-
var y2p = helpers.getDataToPixel(gd, ya, true, yRefType);
209+
var x2p = helpers.getDataToPixel(gd, xa, false, xRefType, helpers.keyIfDefined(xa,'type'));
210+
var y2p = helpers.getDataToPixel(gd, ya, true, yRefType, helpers.keyIfDefined(ya,'type'));
211211
var p2x = helpers.getPixelToData(gd, xa, false, xRefType);
212212
var p2y = helpers.getPixelToData(gd, ya, true, yRefType);
213213

@@ -392,7 +392,7 @@ function setupDragElement(gd, shapePath, shapeOptions, index, shapeLayer, editHe
392392
}
393393
if(ax && ax.type === 'category') {
394394
move = function(xy) {
395-
return p2xy(helpers.castNumericStringsToNumbers(xy2p)(xy) + dxy);
395+
return p2xy(xy2p(xy) + dxy);
396396
};
397397
}
398398
return move;
@@ -585,36 +585,16 @@ function getPathString(gd, options) {
585585
var gs = gd._fullLayout._size;
586586
var x2r, x2p, y2r, y2p;
587587
var x0, x1, y0, y1;
588+
var x2pDecoSelect, y2pDecoSelect;
588589

589-
if(xa) {
590-
if(xRefType === 'domain') {
591-
x2p = function(v) { return xa._offset + xa._length * v; };
592-
} else {
593-
x2r = helpers.shapePositionToRange(xa);
594-
x2p = function(v) { return xa._offset + xa.r2p(x2r(v, true)); };
595-
}
596-
} else {
597-
x2p = function(v) { return gs.l + gs.w * v; };
598-
}
599-
600-
if(ya) {
601-
if(yRefType === 'domain') {
602-
y2p = function(v) { return ya._offset + ya._length * (1 - v); };
603-
} else {
604-
y2r = helpers.shapePositionToRange(ya);
605-
y2p = function(v) { return ya._offset + ya.r2p(y2r(v, true)); };
606-
}
607-
} else {
608-
y2p = function(v) { return gs.t + gs.h * (1 - v); };
590+
if(type === 'path') {
591+
x2pDecoSelect = helpers.keyIfDefined(xa,'type');
592+
y2pDecoSelect = helpers.keyIfDefined(ya,'type');
609593
}
594+
x2p = helpers.getDataToPixel(gd,xa,false,xRefType,x2pDecoSelect);
595+
y2p = helpers.getDataToPixel(gd,ya,true,yRefType,y2pDecoSelect);
610596

611-
if(type === 'path') {
612-
if(xa && xa.type === 'date') x2p = helpers.decodeDate(x2p);
613-
if(ya && ya.type === 'date') y2p = helpers.decodeDate(y2p);
614-
// the SVG path is always a string, so for categories numeric strings
615-
// are always converted to numbers when seen in a SVG path
616-
if(xa && xa.type === 'category') x2p = helpers.castNumericStringsToNumbers(x2p);
617-
if(ya && ya.type === 'category') y2p = helpers.castNumericStringsToNumbers(y2p);
597+
if (type === 'path') {
618598
return convertPath(options, x2p, y2p);
619599
}
620600

src/components/shapes/helpers.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,18 @@ exports.extractPathCoords = function(path, paramsToUse) {
5757
return extractedCoordinates;
5858
};
5959

60-
exports.getDataToPixel = function(gd, axis, isVertical, refType) {
60+
exports.getDataToPixel = function(gd, axis, isVertical, refType, d2pDecoSelect) {
6161
var gs = gd._fullLayout._size;
6262
var dataToPixel;
63+
var d2pDecorator;
64+
var d2pDecos = {
65+
date: exports.decodeDate,
66+
category: exports.castNumericStringsToNumbers
67+
};
68+
d2pDecorator = d2pDecos[d2pDecoSelect];
69+
if (d2pDecorator === undefined) {
70+
d2pDecorator = function (f) { return f; }
71+
}
6372

6473
if(axis) {
6574
if(refType === 'domain') {
@@ -73,15 +82,14 @@ exports.getDataToPixel = function(gd, axis, isVertical, refType) {
7382
return axis._offset + axis.r2p(d2r(v, true));
7483
};
7584

76-
if(axis.type === 'date') dataToPixel = exports.decodeDate(dataToPixel);
7785
}
7886
} else if(isVertical) {
7987
dataToPixel = function(v) { return gs.t + gs.h * (1 - v); };
8088
} else {
8189
dataToPixel = function(v) { return gs.l + gs.w * v; };
8290
}
8391

84-
return dataToPixel;
92+
return d2pDecorator(dataToPixel);
8593
};
8694

8795
exports.getPixelToData = function(gd, axis, isVertical, opt) {
@@ -153,3 +161,8 @@ exports.makeOptionsAndPlotinfo = function(gd, index) {
153161
plotinfo: plotinfo
154162
};
155163
};
164+
165+
exports.keyIfDefined = function (o,k) {
166+
if (o !== undefined) { return o[k]; }
167+
return undefined;
168+
}

0 commit comments

Comments
 (0)