Skip to content

Commit f1faa28

Browse files
authored
Merge pull request #2081 from plotly/invisible-select
Return empty set for invisible bar trace
2 parents c3f0a18 + a5cf1b7 commit f1faa28

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

src/traces/bar/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
1717
var node3 = cd[0].node3;
1818
var i;
1919

20-
if(trace.visible !== true) return;
20+
if(trace.visible !== true) return [];
2121

2222
if(polygon === false) {
2323
// clear selection

src/traces/scatter/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
2626

2727
// TODO: include lines? that would require per-segment line properties
2828
var hasOnlyLines = (!subtypes.hasMarkers(trace) && !subtypes.hasText(trace));
29-
if(trace.visible !== true || hasOnlyLines) return;
29+
if(trace.visible !== true || hasOnlyLines) return [];
3030

3131
var opacity = Array.isArray(marker.opacity) ? 1 : marker.opacity;
3232

src/traces/scattergeo/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
2222
var di, lonlat, x, y, i;
2323

2424
var hasOnlyLines = (!subtypes.hasMarkers(trace) && !subtypes.hasText(trace));
25-
if(trace.visible !== true || hasOnlyLines) return;
25+
if(trace.visible !== true || hasOnlyLines) return [];
2626

2727
var marker = trace.marker;
2828
var opacity = Array.isArray(marker.opacity) ? 1 : marker.opacity;

src/traces/scattergl/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
2626
var scene = glTrace.scene;
2727

2828
var hasOnlyLines = (!subtypes.hasMarkers(trace) && !subtypes.hasText(trace));
29-
if(trace.visible !== true || hasOnlyLines) return;
29+
if(trace.visible !== true || hasOnlyLines) return [];
3030

3131
// filter out points by visible scatter ones
3232
if(polygon === false) {

src/traces/scattermapbox/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function selectPoints(searchInfo, polygon) {
2424
// to not insert data-driven 'circle-opacity' when we don't need to
2525
trace._hasDimmedPts = false;
2626

27-
if(trace.visible !== true || !subtypes.hasMarkers(trace)) return;
27+
if(trace.visible !== true || !subtypes.hasMarkers(trace)) return [];
2828

2929
if(polygon === false) {
3030
for(i = 0; i < cd.length; i++) {

test/jasmine/tests/select_test.js

+58-14
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ describe('Test select box and lasso in general:', function() {
142142
.map(function(v) { return 'id-' + v; });
143143
mockCopy.data[0].customdata = mockCopy.data[0].y
144144
.map(function(v) { return 'customdata-' + v; });
145+
addInvisible(mockCopy);
145146

146147
var gd;
147148
beforeEach(function(done) {
@@ -213,6 +214,7 @@ describe('Test select box and lasso in general:', function() {
213214
describe('lasso events', function() {
214215
var mockCopy = Lib.extendDeep({}, mock);
215216
mockCopy.layout.dragmode = 'lasso';
217+
addInvisible(mockCopy);
216218

217219
var gd;
218220
beforeEach(function(done) {
@@ -293,6 +295,9 @@ describe('Test select box and lasso in general:', function() {
293295
});
294296

295297
it('should skip over non-visible traces', function(done) {
298+
// note: this tests a mock with one or several invisible traces
299+
// the invisible traces in the other tests test for multiple
300+
// traces, with some visible and some not.
296301
var mockCopy = Lib.extendDeep({}, mock);
297302
mockCopy.layout.dragmode = 'select';
298303

@@ -348,6 +353,21 @@ describe('Test select box and lasso in general:', function() {
348353
.then(resetAndLasso)
349354
.then(function() {
350355
checkPointCount(1, '(back to lasso case 0)');
356+
357+
mockCopy = Lib.extendDeep({}, mock);
358+
mockCopy.layout.dragmode = 'select';
359+
mockCopy.data[0].visible = false;
360+
addInvisible(mockCopy);
361+
return Plotly.newPlot(gd, mockCopy);
362+
})
363+
.then(resetAndSelect)
364+
.then(function() {
365+
checkPointCount(0, '(multiple invisible traces select)');
366+
return Plotly.relayout(gd, 'dragmode', 'lasso');
367+
})
368+
.then(resetAndLasso)
369+
.then(function() {
370+
checkPointCount(0, '(multiple invisible traces lasso)');
351371
})
352372
.catch(fail)
353373
.then(done);
@@ -502,6 +522,7 @@ describe('Test select box and lasso per trace:', function() {
502522
var fig = Lib.extendDeep({}, require('@mocks/ternary_simple'));
503523
fig.layout.width = 800;
504524
fig.layout.dragmode = 'select';
525+
addInvisible(fig);
505526

506527
Plotly.plot(gd, fig).then(function() {
507528
return _run(
@@ -545,6 +566,7 @@ describe('Test select box and lasso per trace:', function() {
545566

546567
var fig = Lib.extendDeep({}, require('@mocks/scattercarpet'));
547568
fig.layout.dragmode = 'select';
569+
addInvisible(fig);
548570

549571
Plotly.plot(gd, fig).then(function() {
550572
return _run(
@@ -577,6 +599,7 @@ describe('Test select box and lasso per trace:', function() {
577599
fig.config = {
578600
mapboxAccessToken: require('@build/credentials.json').MAPBOX_ACCESS_TOKEN
579601
};
602+
addInvisible(fig);
580603

581604
Plotly.plot(gd, fig).then(function() {
582605
return _run(
@@ -620,21 +643,26 @@ describe('Test select box and lasso per trace:', function() {
620643
var assertPoints = makeAssertPoints(['lon', 'lat']);
621644
var assertRanges = makeAssertRanges('geo');
622645
var assertLassoPoints = makeAssertLassoPoints('geo');
646+
var fig = {
647+
data: [{
648+
type: 'scattergeo',
649+
lon: [10, 20, 30],
650+
lat: [10, 20, 30]
651+
}, {
652+
type: 'scattergeo',
653+
lon: [-10, -20, -30],
654+
lat: [10, 20, 30]
655+
}],
656+
layout: {
657+
showlegend: false,
658+
dragmode: 'select',
659+
width: 800,
660+
height: 600
661+
}
662+
};
663+
addInvisible(fig);
623664

624-
Plotly.plot(gd, [{
625-
type: 'scattergeo',
626-
lon: [10, 20, 30],
627-
lat: [10, 20, 30]
628-
}, {
629-
type: 'scattergeo',
630-
lon: [-10, -20, -30],
631-
lat: [10, 20, 30]
632-
}], {
633-
showlegend: false,
634-
dragmode: 'select',
635-
width: 800,
636-
height: 600
637-
})
665+
Plotly.plot(gd, fig)
638666
.then(function() {
639667
return _run(
640668
[[350, 200], [450, 400]],
@@ -686,6 +714,7 @@ describe('Test select box and lasso per trace:', function() {
686714
fig.layout.height = 450;
687715
fig.layout.dragmode = 'select';
688716
fig.layout.geo.scope = 'europe';
717+
addInvisible(fig, false);
689718

690719
Plotly.plot(gd, fig)
691720
.then(function() {
@@ -735,6 +764,7 @@ describe('Test select box and lasso per trace:', function() {
735764

736765
var fig = Lib.extendDeep({}, require('@mocks/0'));
737766
fig.layout.dragmode = 'lasso';
767+
addInvisible(fig);
738768

739769
Plotly.plot(gd, fig)
740770
.then(function() {
@@ -788,6 +818,7 @@ describe('Test select box and lasso per trace:', function() {
788818
fig.layout.dragmode = 'lasso';
789819
fig.layout.width = 600;
790820
fig.layout.height = 500;
821+
addInvisible(fig);
791822

792823
Plotly.plot(gd, fig)
793824
.then(function() {
@@ -824,3 +855,16 @@ describe('Test select box and lasso per trace:', function() {
824855
.then(done);
825856
});
826857
});
858+
859+
// to make sure none of the above tests fail with extraneous invisible traces,
860+
// add a bunch of them here
861+
function addInvisible(fig, canHaveLegend) {
862+
var data = fig.data;
863+
var inputData = Lib.extendDeep([], data);
864+
for(var i = 0; i < inputData.length; i++) {
865+
data.push(Lib.extendDeep({}, inputData[i], {visible: false}));
866+
if(canHaveLegend !== false) data.push(Lib.extendDeep({}, inputData[i], {visible: 'legendonly'}));
867+
}
868+
869+
if(inputData.length === 1 && fig.layout.showlegend !== true) fig.layout.showlegend = false;
870+
}

0 commit comments

Comments
 (0)