Skip to content

Commit 86e0d5a

Browse files
committed
test dynamic axis ranging with and without constraints
plus a couple small bugfixes encountered in the process
1 parent 25c2cfb commit 86e0d5a

12 files changed

+491
-196
lines changed

src/plot_api/plot_api.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ Plotly.plot = function(gd, data, layout, config) {
153153
makePlotFramework(gd);
154154
}
155155

156-
// save initial axis range once per graph
157-
if(graphWasEmpty) Plotly.Axes.saveRangeInitial(gd);
158-
159-
160156
// prepare the data and find the autorange
161157

162158
// generate calcdata, if we need to
@@ -272,6 +268,10 @@ Plotly.plot = function(gd, data, layout, config) {
272268
}
273269

274270
enforceAxisConstraints(gd);
271+
272+
// store initial ranges *after* enforcing constraints, otherwise
273+
// we will never look like we're at the initial ranges
274+
if(graphWasEmpty) Plotly.Axes.saveRangeInitial(gd);
275275
}
276276

277277
// draw ticks, titles, and calculate axis scaling (._b, ._m)

src/plots/cartesian/constraints.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,7 @@ module.exports = function enforceAxisConstraints(gd) {
6767
normScale = normScales[axisID];
6868

6969
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();
70+
scaleZoom(axes[axisID], normScale / matchScale);
8171
}
8272
}
8373
}

src/plots/cartesian/dragbox.js

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
151151
if(dragModeNow === 'zoom') {
152152
dragOptions.moveFn = zoomMove;
153153
dragOptions.doneFn = zoomDone;
154+
155+
// zoomMove takes care of the threshold, but we need to
156+
// minimize this so that constrained zoom boxes will flip
157+
// orientation at the right place
158+
dragOptions.minDrag = 1;
159+
154160
zoomPrep(e, startX, startY);
155161
}
156162
else if(dragModeNow === 'pan') {

test/image/mocks/axes_scaleanchor.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"xaxis": {"nticks": 10, "domain": [0, 0.45], "title": "shared X axis"},
1313
"yaxis": {"scaleanchor": "x", "domain": [0, 0.45], "title": "1:1"},
1414
"yaxis2": {"scaleanchor": "x", "scaleratio": 0.2, "domain": [0.55,1], "title": "1:5"},
15-
"xaxis2": {"type": "log", "domain": [0.55, 1], "title": "unconstrained log X"},
15+
"xaxis2": {"type": "log", "domain": [0.55, 1], "anchor": "y3", "title": "unconstrained log X"},
1616
"yaxis3": {"domain": [0, 0.45], "anchor": "x2", "title": "Scale matches ->"},
1717
"yaxis4": {"scaleanchor": "y3", "domain": [0.55, 1], "anchor": "x2", "title": "Scale matches <-"},
1818
"showlegend": false

test/jasmine/assets/double_click.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
var click = require('./click');
2+
var getNodeCoords = require('./get_node_coords');
23
var DBLCLICKDELAY = require('@src/constants/interactions').DBLCLICKDELAY;
34

5+
/*
6+
* double click on a point.
7+
* you can either specify x,y as pixels, or
8+
* you can specify node and optionally an edge ('n', 'se', 'w' etc)
9+
* to grab it by an edge or corner (otherwise the middle is used)
10+
*/
411
module.exports = function doubleClick(x, y) {
12+
if(typeof x === 'object') {
13+
var coords = getNodeCoords(x, y);
14+
x = coords.x;
15+
y = coords.y;
16+
}
517
return new Promise(function(resolve) {
618
click(x, y);
719

test/jasmine/assets/drag.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var mouseEvent = require('../assets/mouse_event');
1+
var mouseEvent = require('./mouse_event');
2+
var getNodeCoords = require('./get_node_coords');
23

34
/*
45
* drag: grab a node and drag it (dx, dy) pixels
@@ -7,21 +8,12 @@ var mouseEvent = require('../assets/mouse_event');
78
*/
89
module.exports = function(node, dx, dy, edge) {
910

10-
edge = edge || '';
11-
var bbox = node.getBoundingClientRect(),
12-
fromX, fromY;
11+
var coords = getNodeCoords(node, edge);
12+
var fromX = coords.x;
13+
var fromY = coords.y;
1314

14-
if(edge.indexOf('n') !== -1) fromY = bbox.top;
15-
else if(edge.indexOf('s') !== -1) fromY = bbox.bottom;
16-
else fromY = (bbox.bottom + bbox.top) / 2;
17-
18-
if(edge.indexOf('w') !== -1) fromX = bbox.left;
19-
else if(edge.indexOf('e') !== -1) fromX = bbox.right;
20-
else fromX = (bbox.left + bbox.right) / 2;
21-
22-
23-
var toX = fromX + dx,
24-
toY = fromY + dy;
15+
var toX = fromX + dx;
16+
var toY = fromY + dy;
2517

2618
mouseEvent('mousemove', fromX, fromY, {element: node});
2719
mouseEvent('mousedown', fromX, fromY, {element: node});
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* get the pixel coordinates of a node on screen
3+
* optionally specify an edge ('n', 'se', 'w' etc)
4+
* to return an edge or corner (otherwise the middle is used)
5+
*/
6+
module.exports = function(node, edge) {
7+
edge = edge || '';
8+
var bbox = node.getBoundingClientRect(),
9+
x, y;
10+
11+
if(edge.indexOf('n') !== -1) y = bbox.top;
12+
else if(edge.indexOf('s') !== -1) y = bbox.bottom;
13+
else y = (bbox.bottom + bbox.top) / 2;
14+
15+
if(edge.indexOf('w') !== -1) x = bbox.left;
16+
else if(edge.indexOf('e') !== -1) x = bbox.right;
17+
else x = (bbox.left + bbox.right) / 2;
18+
19+
return {x: x, y: y};
20+
};

test/jasmine/assets/mouse_event.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function(type, x, y, opts) {
1414
ev;
1515

1616
if(type === 'scroll') {
17-
ev = new window.WheelEvent('wheel', opts);
17+
ev = new window.WheelEvent('wheel', Object.assign({}, fullOpts, opts));
1818
} else {
1919
ev = new window.MouseEvent(type, fullOpts);
2020
}

0 commit comments

Comments
 (0)