Skip to content

Commit 943f7bf

Browse files
committed
Retain click-selected points in box or lasso select mode [1852]
- If the Shift key is down, data points previously selected by a click are now retained when user adds to a selection through box or lasso select mode.
1 parent fda7141 commit 943f7bf

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/plots/cartesian/select.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
7171
(!e.shiftKey && !e.altKey) ||
7272
((e.shiftKey || e.altKey) && !plotinfo.selection)
7373
) {
74-
// create new polygons, if shift mode or selecting across different subplots
74+
// create new polygons, if not shift mode or selecting across different subplots
7575
plotinfo.selection = {};
7676
plotinfo.selection.polygons = dragOptions.polygons = [];
7777
plotinfo.selection.mergedPolygons = dragOptions.mergedPolygons = [];
@@ -283,7 +283,7 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
283283
for(i = 0; i < searchTraces.length; i++) {
284284
searchInfo = searchTraces[i];
285285

286-
traceSelection = searchInfo._module.selectPoints(searchInfo, testPoly);
286+
traceSelection = searchInfo._module.selectPoints(searchInfo, testPoly, shouldRetainSelection(e));
287287
traceSelections.push(traceSelection);
288288

289289
thisSelection = fillSelectionItem(traceSelection, searchInfo);
@@ -310,8 +310,6 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
310310
throttle.done(throttleID).then(function() {
311311
throttle.clear(throttleID);
312312

313-
// clear visual boundaries of selection area if displayed
314-
outlines.remove();
315313
if(numClicks === 2) {
316314
for(i = 0; i < searchTraces.length; i++) {
317315
searchInfo = searchTraces[i];
@@ -320,6 +318,9 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
320318

321319
updateSelectedState(gd, searchTraces);
322320
gd.emit('plotly_deselect', null);
321+
322+
// clear visual boundaries of selection area if displayed
323+
outlines.remove();
323324
}
324325
else {
325326
// TODO What to do with the code below because we now have behavior for a single click
@@ -358,7 +359,6 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
358359
// ----------------
359360
// TODO handle clearing selection when no point is clicked (based on hoverData)
360361
// TODO do we have to consider multiple traces?
361-
// TODO implement selection retention (Shift key) for lasso and box
362362
function selectOnClick(gd, numClicks, evt) {
363363
var calcData = gd.calcdata[0];
364364

@@ -374,7 +374,7 @@ function selectOnClick(gd, numClicks, evt) {
374374
module = trace._module;
375375

376376
// Execute selection by delegating to respective module
377-
var retainSelection = evt.shiftKey,
377+
var retainSelection = shouldRetainSelection(evt),
378378
pointSelected = isPointSelected(trace, hoverDatum.pointNumber),
379379
onePointSelectedOnly = isOnePointSelectedOnly(trace);
380380

@@ -395,6 +395,10 @@ function selectOnClick(gd, numClicks, evt) {
395395
}
396396
}
397397

398+
function shouldRetainSelection(evt) {
399+
return evt.shiftKey;
400+
}
401+
398402
function isPointSelected(trace, pointNumber) {
399403
if(!trace.selectedpoints && !Array.isArray(trace.selectedpoints)) return false;
400404
return trace.selectedpoints.indexOf(pointNumber) > -1;

src/traces/scatter/select.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
var subtypes = require('./subtypes');
1313

14-
function selectPoints(searchInfo, polygon) {
14+
function selectPoints(searchInfo, polygon, retainOtherSelectModesState) {
1515
var cd = searchInfo.cd,
1616
xa = searchInfo.xaxis,
1717
ya = searchInfo.yaxis,
@@ -37,8 +37,13 @@ function selectPoints(searchInfo, polygon) {
3737
if(polygon.contains([x, y])) {
3838
selection.push(_newSelectionItem(i, xa.c2d(di.x), ya.c2d(di.y)));
3939
di.selected = 1;
40+
di.selectedByPolygon = true;
4041
} else {
42+
if(retainOtherSelectModesState && !di.selectedByPolygon && di.selected === 1) {
43+
continue;
44+
}
4145
di.selected = 0;
46+
delete di.selectedByPolygon;
4247
}
4348
}
4449
}

0 commit comments

Comments
 (0)