Skip to content

Commit 84d95a5

Browse files
committed
dynamic axis constraints
1 parent 98900a2 commit 84d95a5

File tree

2 files changed

+302
-74
lines changed

2 files changed

+302
-74
lines changed

src/plots/cartesian/constraints.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ module.exports = function enforceAxisConstraints(gd) {
2727

2828
var minScale = Infinity;
2929
var maxScale = 0;
30+
// mostly matchScale will be the same as minScale
31+
// ie we expand axis ranges to encompass *everything*
32+
// that's currently in any of their ranges, but during
33+
// autorange of a subset of axes we will ignore other
34+
// axes for this purpose.
35+
var matchScale = Infinity;
3036
var normScales = {};
3137
var axes = {};
3238

@@ -42,6 +48,13 @@ module.exports = function enforceAxisConstraints(gd) {
4248
// abs: inverted scales still satisfy the constraint
4349
normScales[axisID] = normScale = Math.abs(ax._m) / group[axisID];
4450
minScale = Math.min(minScale, normScale);
51+
if(ax._constraintShrinkable) {
52+
// this has served its purpose, so remove it
53+
delete ax._constraintShrinkable;
54+
}
55+
else {
56+
matchScale = Math.min(matchScale, normScale);
57+
}
4558
maxScale = Math.max(maxScale, normScale);
4659
}
4760

@@ -53,7 +66,19 @@ module.exports = function enforceAxisConstraints(gd) {
5366
axisID = axisIDs[j];
5467
normScale = normScales[axisID];
5568

56-
if(normScale > minScale) scaleZoom(axes[axisID], normScale / minScale);
69+
if(normScale !== matchScale) {
70+
ax = axes[axisID];
71+
// if range matches _rangeInitial before the constraint is applied,
72+
// change _rangeInitial to the new range - otherwise a doubleclick
73+
// will never autorange because we're not starting at the reset point.
74+
var wasAtInitial = (ax._rangeInitial &&
75+
ax.range[0] === ax._rangeInitial[0] &&
76+
ax.range[1] === ax._rangeInitial[1]);
77+
78+
scaleZoom(ax, normScale / matchScale);
79+
80+
if(wasAtInitial) ax._rangeInitial = ax.range.slice();
81+
}
5782
}
5883
}
5984
};

0 commit comments

Comments
 (0)