-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathconstraints.js
67 lines (53 loc) · 2.19 KB
/
constraints.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var id2name = require('./axis_ids').id2name;
var ALMOST_EQUAL = 1 - 1e-6;
module.exports = function enforceAxisConstraints(gd) {
var fullLayout = gd._fullLayout;
var layout = gd.layout;
var constraintGroups = fullLayout._axisConstraintGroups;
var i, j, axisID, ax, normScale;
for(i = 0; i < constraintGroups.length; i++) {
var group = constraintGroups[i];
var axisIDs = Object.keys(group);
var minScale = Infinity;
var maxScale = 0;
var normScales = {};
var axes = {};
// find the (normalized) scale of each axis in the group
for(j = 0; j < axisIDs.length; j++) {
axisID = axisIDs[j];
axes[axisID] = ax = fullLayout[id2name(axisID)];
// set axis scale here so we can use _m rather than
// having to calculate it from length and range
ax.setScale();
// abs: inverted scales still satisfy the constraint
normScales[axisID] = normScale = Math.abs(ax._m) / group[axisID];
minScale = Math.min(minScale, normScale);
maxScale = Math.max(maxScale, normScale);
}
// Do we have a constraint mismatch? Give a small buffer for rounding errors
if(minScale > ALMOST_EQUAL * maxScale) continue;
// now increase any ranges we need to until all normalized scales are equal
for(j = 0; j < axisIDs.length; j++) {
axisID = axisIDs[j];
normScale = normScales[axisID];
if(normScale > minScale) {
ax = axes[axisID];
var rangeLinear = [ax.r2l(ax.range[0]), ax.r2l(ax.range[1])];
var center = (rangeLinear[0] + rangeLinear[1]) / 2;
var newHalfSpan = (center - rangeLinear[0]) * normScale / minScale;
ax.range = layout[id2name(axisID)].range = [
ax.l2r(center - newHalfSpan),
ax.l2r(center + newHalfSpan)
];
}
}
}
};