Skip to content

Commit b2a246f

Browse files
committed
Count defining coordinates by axis
1 parent 8489819 commit b2a246f

2 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/components/shapes/defaults.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
8383

8484
if(Array.isArray(inputRef) && inputRef.length > 0) {
8585
// Array case: use coerceRefArray for validation
86-
var expectedLen = helpers.countDefiningCoords(shapeType, path);
86+
var expectedLen = helpers.countDefiningCoords(shapeType, path, axLetter);
8787
axRef = Axes.coerceRefArray(shapeIn, shapeOut, gdMock, axLetter, undefined, 'paper', expectedLen);
8888
shapeOut['_' + axLetter + 'refArray'] = true;
8989

@@ -102,25 +102,24 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
102102
}
103103

104104
if(Array.isArray(axRef)) {
105-
var dflts = [0.25, 0.75];
106-
var pixelDflts = [0, 10];
107-
108-
// For each coordinate, coerce the position with their respective axis ref
109-
[0, 1].forEach(function(i) {
110-
var ref = axRef[i];
111-
var refType = Axes.getRefType(ref);
112-
if(refType === 'range') {
113-
ax = Axes.getFromId(gdMock, ref);
114-
pos2r = helpers.shapePositionToRange(ax);
115-
r2pos = helpers.rangeToShapePosition(ax);
116-
if(ax.type === 'category' || ax.type === 'multicategory') {
117-
coerce(axLetter + i + 'shift');
105+
if(noPath) {
106+
var dflts = [0.25, 0.75];
107+
var pixelDflts = [0, 10];
108+
109+
[0, 1].forEach(function(i) {
110+
var ref = axRef[i];
111+
var refType = Axes.getRefType(ref);
112+
if(refType === 'range') {
113+
ax = Axes.getFromId(gdMock, ref);
114+
pos2r = helpers.shapePositionToRange(ax);
115+
r2pos = helpers.rangeToShapePosition(ax);
116+
if(ax.type === 'category' || ax.type === 'multicategory') {
117+
coerce(axLetter + i + 'shift');
118+
}
119+
} else {
120+
pos2r = r2pos = Lib.identity;
118121
}
119-
} else {
120-
pos2r = r2pos = Lib.identity;
121-
}
122122

123-
if(noPath) {
124123
var attr = axLetter + i;
125124
var inValue = shapeIn[attr];
126125
shapeIn[attr] = pos2r(shapeIn[attr], true);
@@ -133,16 +132,16 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
133132

134133
shapeOut[attr] = r2pos(shapeOut[attr]);
135134
shapeIn[attr] = inValue;
136-
}
137135

138-
if(i === 0 && sizeMode === 'pixel') {
139-
var inAnchor = shapeIn[attrAnchor];
140-
shapeIn[attrAnchor] = pos2r(shapeIn[attrAnchor], true);
141-
Axes.coercePosition(shapeOut, gdMock, coerce, ref, attrAnchor, 0.25);
142-
shapeOut[attrAnchor] = r2pos(shapeOut[attrAnchor]);
143-
shapeIn[attrAnchor] = inAnchor;
144-
}
145-
});
136+
if(i === 0 && sizeMode === 'pixel') {
137+
var inAnchor = shapeIn[attrAnchor];
138+
shapeIn[attrAnchor] = pos2r(shapeIn[attrAnchor], true);
139+
Axes.coercePosition(shapeOut, gdMock, coerce, ref, attrAnchor, 0.25);
140+
shapeOut[attrAnchor] = r2pos(shapeOut[attrAnchor]);
141+
shapeIn[attrAnchor] = inAnchor;
142+
}
143+
});
144+
}
146145
} else {
147146
var axRefType = Axes.getRefType(axRef);
148147

@@ -151,7 +150,7 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
151150
ax._shapeIndices.push(shapeOut._index);
152151
r2pos = helpers.rangeToShapePosition(ax);
153152
pos2r = helpers.shapePositionToRange(ax);
154-
if(ax.type === 'category' || ax.type === 'multicategory') {
153+
if(noPath && (ax.type === 'category' || ax.type === 'multicategory')) {
155154
coerce(axLetter + '0shift');
156155
coerce(axLetter + '1shift');
157156
}

src/components/shapes/helpers.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ exports.extractPathCoords = function(path, paramsToUse, isRaw) {
5353
return extractedCoordinates;
5454
};
5555

56-
exports.countDefiningCoords = function(shapeType, path) {
56+
exports.countDefiningCoords = function(shapeType, path, axLetter) {
5757
// non-path shapes always have 2 defining coordinates
5858
if(shapeType !== 'path') return 2;
5959
if(!path) return 0;
6060

6161
var segments = path.match(constants.segmentRE);
6262
if(!segments) return 0;
6363

64+
var paramIsAxis = axLetter === 'x' ? constants.paramIsX : constants.paramIsY;
65+
6466
return segments.reduce((coordCount, segment) => {
65-
// for each path command, check if there is a drawn coordinate
67+
// for each path command, check if there is a drawn coordinate for this axis
6668
var segmentType = segment.charAt(0);
67-
var hasDrawnX = constants.paramIsX[segmentType].drawn !== undefined;
68-
var hasDrawnY = constants.paramIsY[segmentType].drawn !== undefined;
69-
return coordCount + (hasDrawnX || hasDrawnY ? 1 : 0);
69+
var hasDrawn = paramIsAxis[segmentType].drawn !== undefined;
70+
return coordCount + (hasDrawn ? 1 : 0);
7071
}, 0);
7172
};
7273

0 commit comments

Comments
 (0)