Skip to content

Commit c288f57

Browse files
authored
Merge pull request #5534 from plotly/scattergl-fix-select
scattergl: make selection's eventData the same as in scatter traces
2 parents ff0c00a + bfd95d8 commit c288f57

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

src/traces/scattergl/select.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var styleTextSelection = require('./edit_style').styleTextSelection;
55

66
module.exports = function select(searchInfo, selectionTester) {
77
var cd = searchInfo.cd;
8+
var xa = searchInfo.xaxis;
9+
var ya = searchInfo.yaxis;
810
var selection = [];
911
var trace = cd[0].trace;
1012
var stash = cd[0].t;
@@ -33,8 +35,8 @@ module.exports = function select(searchInfo, selectionTester) {
3335
els.push(i);
3436
selection.push({
3537
pointNumber: i,
36-
x: x[i],
37-
y: y[i]
38+
x: xa.c2d(x[i]),
39+
y: ya.c2d(y[i])
3840
});
3941
} else {
4042
unels.push(i);

test/jasmine/tests/scattergl_select_test.js

+47
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,53 @@ describe('Test gl2d lasso/select:', function() {
349349
})
350350
.then(done, done.fail);
351351
});
352+
353+
['x', 'y'].forEach(function(ax) {
354+
[
355+
['linear', [1, 2, 3]],
356+
['log', [1, 2, 3]],
357+
['category', ['A', 'B', 'C']],
358+
['date', ['1900-01-01', '2000-01-01', '2100-01-01']]
359+
].forEach(function(test) {
360+
var axType = test[0];
361+
362+
it('@gl should return the same eventData as scatter on ' + axType + ' ' + ax + ' axis', function(done) {
363+
var _mock = {
364+
data: [{type: 'scatter', x: [1, 2, 3], y: [6, 5, 4]}],
365+
layout: {dragmode: 'select', width: 400, height: 400, xaxis: {}, yaxis: {}}
366+
};
367+
_mock.data[0][ax] = test[1];
368+
_mock.layout[ax + 'axis'].type = axType;
369+
gd = createGraphDiv();
370+
var scatterEventData = {};
371+
var selectPath = [[150, 150], [250, 250]];
372+
373+
Plotly.newPlot(gd, _mock)
374+
.then(delay(20))
375+
.then(function() {
376+
expect(gd._fullLayout[ax + 'axis'].type).toEqual(test[0]);
377+
return select(gd, selectPath);
378+
})
379+
.then(delay(20))
380+
.then(function(eventData) {
381+
scatterEventData = eventData;
382+
// Make sure we selected a point
383+
expect(eventData.points.length).toBe(1);
384+
return Plotly.restyle(gd, 'type', 'scattergl');
385+
})
386+
.then(delay(20))
387+
.then(function() {
388+
expect(gd._fullLayout[ax + 'axis'].type).toEqual(test[0]);
389+
return select(gd, selectPath);
390+
})
391+
.then(delay(20))
392+
.then(function(eventData) {
393+
assertEventData(eventData, scatterEventData);
394+
})
395+
.then(done, done.fail);
396+
});
397+
});
398+
});
352399
});
353400

354401
describe('Test displayed selections:', function() {

test/jasmine/tests/scatterpolargl_test.js

+83
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@ var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
33
var ScatterPolarGl = require('@src/traces/scatterpolargl');
44

5+
var d3Select = require('../../strict-d3').select;
56
var d3SelectAll = require('../../strict-d3').selectAll;
67
var createGraphDiv = require('../assets/create_graph_div');
78
var destroyGraphDiv = require('../assets/destroy_graph_div');
89

10+
var delay = require('../assets/delay');
911
var mouseEvent = require('../assets/mouse_event');
1012
var readPixel = require('../assets/read_pixel');
1113

1214
var customAssertions = require('../assets/custom_assertions');
1315
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
1416
var checkTextTemplate = require('../assets/check_texttemplate');
1517

18+
function drag(gd, path) {
19+
var len = path.length;
20+
var el = d3Select(gd).select('rect.nsewdrag').node();
21+
var opts = {element: el};
22+
23+
Lib.clearThrottle();
24+
mouseEvent('mousemove', path[0][0], path[0][1], opts);
25+
mouseEvent('mousedown', path[0][0], path[0][1], opts);
26+
27+
path.slice(1, len).forEach(function(pt) {
28+
Lib.clearThrottle();
29+
mouseEvent('mousemove', pt[0], pt[1], opts);
30+
});
31+
32+
mouseEvent('mouseup', path[len - 1][0], path[len - 1][1], opts);
33+
}
34+
35+
function select(gd, path) {
36+
return new Promise(function(resolve, reject) {
37+
gd.once('plotly_selected', resolve);
38+
setTimeout(function() { reject('did not trigger *plotly_selected*');}, 200);
39+
drag(gd, path);
40+
});
41+
}
42+
1643
describe('Test scatterpolargl hover:', function() {
1744
var gd;
1845

@@ -132,6 +159,18 @@ describe('Test scatterpolargl interactions:', function() {
132159
.reduce(function(acc, v) { return acc + v; }, 0);
133160
}
134161

162+
function assertEventData(actual, expected) {
163+
expect(actual.points.length).toBe(expected.points.length);
164+
165+
expected.points.forEach(function(e, i) {
166+
var a = actual.points[i];
167+
if(a) {
168+
expect(a.r).toBe(e.r, 'r');
169+
expect(a.theta).toBe(e.theta, 'theta');
170+
}
171+
});
172+
}
173+
135174
it('@gl should be able to toggle from svg to gl', function(done) {
136175
gd = createGraphDiv();
137176

@@ -258,6 +297,50 @@ describe('Test scatterpolargl interactions:', function() {
258297
})
259298
.then(done, done.fail);
260299
});
300+
301+
['r', 'theta'].forEach(function(ax) {
302+
[
303+
['linear', [0, 180]],
304+
['category', ['A', 'B']],
305+
].forEach(function(test) {
306+
var axType = test[0];
307+
var axNames = {'r': 'radialaxis', 'theta': 'angularaxis'};
308+
it('@gl should return the same eventData as scatter on ' + axType + ' ' + ax + ' axis', function(done) {
309+
var _mock = {
310+
data: [{type: 'scatterpolar', r: [5, 10], theta: [0, 180]}],
311+
layout: {dragmode: 'select', width: 400, height: 400}
312+
};
313+
_mock.data[0][ax] = test[1];
314+
gd = createGraphDiv();
315+
var scatterpolarEventData = {};
316+
var selectPath = [[185, 150], [400, 250]];
317+
318+
Plotly.newPlot(gd, _mock)
319+
.then(delay(20))
320+
.then(function() {
321+
expect(gd._fullLayout.polar[axNames[ax]].type).toEqual(test[0]);
322+
return select(gd, selectPath);
323+
})
324+
.then(delay(20))
325+
.then(function(eventData) {
326+
scatterpolarEventData = eventData;
327+
// Make sure we selected a point
328+
expect(eventData.points.length).toBe(1);
329+
return Plotly.restyle(gd, 'type', 'scatterpolargl');
330+
})
331+
.then(delay(20))
332+
.then(function() {
333+
expect(gd._fullLayout.polar[axNames[ax]].type).toEqual(test[0]);
334+
return select(gd, selectPath);
335+
})
336+
.then(delay(20))
337+
.then(function(eventData) {
338+
assertEventData(eventData, scatterpolarEventData);
339+
})
340+
.then(done, done.fail);
341+
});
342+
});
343+
});
261344
});
262345

263346
describe('Test scatterpolargl autorange:', function() {

0 commit comments

Comments
 (0)