Skip to content

Commit bbb8205

Browse files
committed
Merge pull request #351 from plotly/restyle-zoom
Don't override saved initial ranges on data-updating restyle calls [fixes #346]
2 parents 4ba05d8 + 5243590 commit bbb8205

File tree

3 files changed

+127
-85
lines changed

3 files changed

+127
-85
lines changed

src/plot_api/plot_api.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,17 @@ Plotly.plot = function(gd, data, layout, config) {
147147
}
148148
else if(graphWasEmpty) makePlotFramework(gd);
149149

150+
// save initial axis range once per graph
151+
if(graphWasEmpty) Plotly.Axes.saveRangeInitial(gd);
152+
150153
var fullLayout = gd._fullLayout;
151154

152155
// prepare the data and find the autorange
153156

154157
// generate calcdata, if we need to
155158
// to force redoing calcdata, just delete it before calling Plotly.plot
156-
var recalc = !gd.calcdata || gd.calcdata.length!==(gd.data||[]).length;
157-
if(recalc) {
158-
doCalcdata(gd);
159-
160-
if(gd._context.doubleClick!==false || gd._context.displayModeBar!==false) {
161-
Plotly.Axes.saveRangeInitial(gd);
162-
}
163-
}
159+
var recalc = !gd.calcdata || gd.calcdata.length !== (gd.data || []).length;
160+
if(recalc) doCalcdata(gd);
164161

165162
// in case it has changed, attach fullData traces to calcdata
166163
for(var i = 0; i < gd.calcdata.length; i++) {

test/jasmine/.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"env": {
44
"browser": true,
55
"jasmine": true
6+
},
7+
"globals": {
8+
"Promise": true
69
}
710
}

test/jasmine/tests/click_test.js

+119-77
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var mouseEvent = require('../assets/mouse_event');
88
var customMatchers = require('../assets/custom_matchers');
99

1010

11-
describe('click interactions', function() {
11+
describe('Test click interactions:', function() {
1212
var mock = require('@mocks/14.json'),
1313
gd;
1414

@@ -26,12 +26,15 @@ describe('click interactions', function() {
2626
mouseEvent('mouseup', x, y);
2727
}
2828

29-
function doubleClick(x, y, cb) {
30-
click(x, y);
31-
setTimeout(function() {
29+
function doubleClick(x, y) {
30+
return new Promise(function(resolve) {
3231
click(x, y);
33-
cb();
34-
}, DBLCLICKDELAY / 2);
32+
33+
setTimeout(function() {
34+
click(x, y);
35+
resolve();
36+
}, DBLCLICKDELAY / 2);
37+
});
3538
}
3639

3740
describe('click events', function() {
@@ -87,7 +90,7 @@ describe('click interactions', function() {
8790
});
8891

8992
it('should return null', function(done) {
90-
doubleClick(pointPos[0], pointPos[1], function() {
93+
doubleClick(pointPos[0], pointPos[1]).then(function() {
9194
expect(futureData).toBe(null);
9295
done();
9396
});
@@ -113,9 +116,11 @@ describe('click interactions', function() {
113116
'yaxis.range[1]': zoomRangeY[1]
114117
};
115118

116-
beforeEach(function() {
119+
beforeAll(function() {
117120
jasmine.addMatchers(customMatchers);
121+
});
118122

123+
beforeEach(function() {
119124
gd = createGraphDiv();
120125
mockCopy = Lib.extendDeep({}, mock);
121126
});
@@ -139,12 +144,12 @@ describe('click interactions', function() {
139144
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
140145
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
141146

142-
doubleClick(blankPos[0], blankPos[1], function() {
143-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
144-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
147+
return doubleClick(blankPos[0], blankPos[1]);
148+
}).then(function() {
149+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
150+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
145151

146-
done();
147-
});
152+
done();
148153
});
149154
});
150155
});
@@ -156,17 +161,17 @@ describe('click interactions', function() {
156161
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
157162
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
158163

159-
Plotly.relayout(gd, update).then(function() {
160-
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
161-
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
164+
return Plotly.relayout(gd, update);
165+
}).then(function() {
166+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
167+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
162168

163-
doubleClick(blankPos[0], blankPos[1], function() {
164-
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
165-
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
169+
return doubleClick(blankPos[0], blankPos[1]);
170+
}).then(function() {
171+
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
172+
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
166173

167-
done();
168-
});
169-
});
174+
done();
170175
});
171176
});
172177

@@ -177,17 +182,54 @@ describe('click interactions', function() {
177182
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
178183
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
179184

180-
doubleClick(blankPos[0], blankPos[1], function() {
181-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
182-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
185+
return doubleClick(blankPos[0], blankPos[1]);
186+
}).then(function() {
187+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
188+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
183189

184-
doubleClick(blankPos[0], blankPos[1], function() {
185-
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
186-
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
190+
return doubleClick(blankPos[0], blankPos[1]);
191+
}).then(function() {
192+
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
193+
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
187194

188-
done();
189-
});
190-
});
195+
done();
196+
});
197+
});
198+
199+
it('when set to \'reset+autorange\' (the default) should follow updated auto ranges', function(done) {
200+
var updateData = {
201+
x: [[1e-4, 0, 1e3]],
202+
y: [[30, 0, 30]]
203+
};
204+
205+
var newAutoRangeX = [-4.482371794871794, 3.4823717948717943],
206+
newAutoRangeY = [-0.8892256657741471, 1.6689872212461876];
207+
208+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
209+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
210+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
211+
212+
return Plotly.relayout(gd, update);
213+
}).then(function() {
214+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
215+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
216+
217+
return Plotly.restyle(gd, updateData);
218+
}).then(function() {
219+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
220+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
221+
222+
return doubleClick(blankPos[0], blankPos[1]);
223+
}).then(function() {
224+
expect(gd.layout.xaxis.range).toBeCloseToArray(newAutoRangeX);
225+
expect(gd.layout.yaxis.range).toBeCloseToArray(newAutoRangeY);
226+
227+
return doubleClick(blankPos[0], blankPos[1]);
228+
}).then(function() {
229+
expect(gd.layout.xaxis.range).toBeCloseToArray(newAutoRangeX);
230+
expect(gd.layout.yaxis.range).toBeCloseToArray(newAutoRangeY);
231+
232+
done();
191233
});
192234
});
193235

@@ -196,17 +238,17 @@ describe('click interactions', function() {
196238
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
197239
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
198240

199-
Plotly.relayout(gd, update).then(function() {
200-
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
201-
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
241+
return Plotly.relayout(gd, update);
242+
}).then(function() {
243+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
244+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
202245

203-
doubleClick(blankPos[0], blankPos[1], function() {
204-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
205-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
246+
return doubleClick(blankPos[0], blankPos[1]);
247+
}).then(function() {
248+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
249+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
206250

207-
done();
208-
});
209-
});
251+
done();
210252
});
211253
});
212254

@@ -217,17 +259,17 @@ describe('click interactions', function() {
217259
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
218260
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
219261

220-
Plotly.relayout(gd, update).then(function() {
221-
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
222-
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
262+
return Plotly.relayout(gd, update);
263+
}).then(function() {
264+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
265+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
223266

224-
doubleClick(blankPos[0], blankPos[1], function() {
225-
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
226-
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
267+
return doubleClick(blankPos[0], blankPos[1]);
268+
}).then(function() {
269+
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
270+
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
227271

228-
done();
229-
});
230-
});
272+
done();
231273
});
232274
});
233275

@@ -238,12 +280,12 @@ describe('click interactions', function() {
238280
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
239281
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
240282

241-
doubleClick(blankPos[0], blankPos[1], function() {
242-
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
243-
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
283+
return doubleClick(blankPos[0], blankPos[1]);
284+
}).then(function() {
285+
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
286+
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
244287

245-
done();
246-
});
288+
done();
247289
});
248290
});
249291

@@ -252,17 +294,17 @@ describe('click interactions', function() {
252294
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
253295
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
254296

255-
Plotly.relayout(gd, update).then(function() {
256-
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
257-
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
297+
return Plotly.relayout(gd, update);
298+
}).then(function() {
299+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
300+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
258301

259-
doubleClick(blankPos[0], blankPos[1], function() {
260-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
261-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
302+
return doubleClick(blankPos[0], blankPos[1]);
303+
}).then(function() {
304+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
305+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
262306

263-
done();
264-
});
265-
});
307+
done();
266308
});
267309
});
268310

@@ -273,17 +315,17 @@ describe('click interactions', function() {
273315
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
274316
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
275317

276-
Plotly.relayout(gd, update).then(function() {
277-
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
278-
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
318+
return Plotly.relayout(gd, update);
319+
}).then(function() {
320+
expect(gd.layout.xaxis.range).toBeCloseToArray(zoomRangeX);
321+
expect(gd.layout.yaxis.range).toBeCloseToArray(zoomRangeY);
279322

280-
doubleClick(blankPos[0], blankPos[1], function() {
281-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
282-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
323+
return doubleClick(blankPos[0], blankPos[1]);
324+
}).then(function() {
325+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
326+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
283327

284-
done();
285-
});
286-
});
328+
done();
287329
});
288330
});
289331

@@ -294,12 +336,12 @@ describe('click interactions', function() {
294336
expect(gd.layout.xaxis.range).toBeCloseToArray(setRangeX);
295337
expect(gd.layout.yaxis.range).toBeCloseToArray(setRangeY);
296338

297-
doubleClick(blankPos[0], blankPos[1], function() {
298-
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
299-
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
339+
return doubleClick(blankPos[0], blankPos[1]);
340+
}).then(function() {
341+
expect(gd.layout.xaxis.range).toBeCloseToArray(autoRangeX);
342+
expect(gd.layout.yaxis.range).toBeCloseToArray(autoRangeY);
300343

301-
done();
302-
});
344+
done();
303345
});
304346
});
305347

0 commit comments

Comments
 (0)