Skip to content

Commit 3262220

Browse files
committed
Refactor calcArrayRefAutorange as pure function
1 parent b2a246f commit 3262220

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

src/components/shapes/calc_autorange.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ module.exports = function calcAutorange(gd) {
2222
var yRefType = Axes.getRefType(shape.yref);
2323

2424
if(xRefType === 'array') {
25-
calcArrayRefAutorange(gd, shape, 'x');
25+
const extremesForRefArray = calcArrayRefAutorange(gd, shape, 'x');
26+
Object.entries(extremesForRefArray).forEach(([axID, axExtremes]) => {
27+
ax = Axes.getFromId(gd, axID);
28+
shape._extremes[ax._id] = Axes.findExtremes(ax, axExtremes, calcXPaddingOptions(shape));
29+
});
2630
} else if(shape.xref !== 'paper' && xRefType !== 'domain') {
2731
// paper and axis domain referenced shapes don't affect autorange
2832
ax = Axes.getFromId(gd, shape.xref);
@@ -33,7 +37,11 @@ module.exports = function calcAutorange(gd) {
3337
}
3438

3539
if(yRefType === 'array') {
36-
calcArrayRefAutorange(gd, shape, 'y');
40+
const extremesForRefArray = calcArrayRefAutorange(gd, shape, 'y');
41+
Object.entries(extremesForRefArray).forEach(([axID, axExtremes]) => {
42+
ax = Axes.getFromId(gd, axID);
43+
shape._extremes[ax._id] = Axes.findExtremes(ax, axExtremes, calcYPaddingOptions(shape));
44+
});
3745
} else if(shape.yref !== 'paper' && yRefType !== 'domain') {
3846
ax = Axes.getFromId(gd, shape.yref);
3947
bounds = shapeBounds(ax, shape, constants.paramIsY);
@@ -44,10 +52,9 @@ module.exports = function calcAutorange(gd) {
4452
}
4553
};
4654

47-
function calcArrayRefAutorange(gd, shape, dim) {
48-
var refs = shape[dim + 'ref'];
49-
var paramsToUse = dim === 'x' ? constants.paramIsX : constants.paramIsY;
50-
var paddingOpts = dim === 'x' ? calcXPaddingOptions(shape) : calcYPaddingOptions(shape);
55+
function calcArrayRefAutorange(gd, shape, axLetter) {
56+
var refs = shape[axLetter + 'ref'];
57+
var paramsToUse = axLetter === 'x' ? constants.paramIsX : constants.paramIsY;
5158

5259
function addToAxisGroup(ref, val) {
5360
if(ref === 'paper' || Axes.getRefType(ref) === 'domain') return;
@@ -74,18 +81,21 @@ function calcArrayRefAutorange(gd, shape, dim) {
7481
}
7582
}
7683
} else {
77-
addToAxisGroup(refs[0], shape[dim + '0']);
78-
addToAxisGroup(refs[1], shape[dim + '1']);
84+
addToAxisGroup(refs[0], shape[axLetter + '0']);
85+
addToAxisGroup(refs[1], shape[axLetter + '1']);
7986
}
8087

81-
// For each axis, convert coordinates to data values then calculate extremes
88+
// Convert coordinates to data values
89+
var convertedGroups = {};
8290
for(var axId in axisGroups) {
8391
var ax = Axes.getFromId(gd, axId);
8492
if(!ax) continue;
8593
var convertVal = (ax.type === 'category' || ax.type === 'multicategory') ? ax.r2c : ax.d2c;
8694
if(ax.type === 'date') convertVal = helpers.decodeDate(convertVal);
87-
shape._extremes[ax._id] = Axes.findExtremes(ax, axisGroups[axId].map(convertVal), paddingOpts);
95+
convertedGroups[ax._id] = axisGroups[axId].map(convertVal);
8896
}
97+
98+
return convertedGroups;
8999
}
90100

91101
function calcXPaddingOptions(shape) {

0 commit comments

Comments
 (0)