Skip to content

Commit 6af4695

Browse files
authored
Merge pull request #2506 from ECraneWorldwide/configurable-select-direction
Configurable select direction
2 parents 58e8fc4 + 9dd166c commit 6af4695

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

src/components/fx/layout_attributes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,17 @@ module.exports = {
101101
].join(' ')
102102
},
103103
editType: 'none'
104+
},
105+
selectdirection: {
106+
valType: 'enumerated',
107+
role: 'info',
108+
values: ['h', 'v', 'd', 'any'],
109+
dflt: 'any',
110+
description: [
111+
'When "dragmode" is set to "select", this limits the selection of the drag to',
112+
'horizontal, vertical or diagonal. "h" only allows horizontal selection,',
113+
'"v" only vertical, "d" only diagonal and "any" sets no limit.'
114+
].join(' '),
115+
editType: 'none'
104116
}
105117
};

src/components/fx/layout_defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
1616
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
1717
}
1818

19-
coerce('dragmode');
19+
var dragMode = coerce('dragmode');
20+
if(dragMode === 'select') coerce('selectdirection');
2021

2122
var hovermodeDflt;
2223
if(layoutOut._has('cartesian')) {

src/plots/cartesian/select.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,18 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
200200
dy = Math.abs(y1 - y0);
201201

202202
if(mode === 'select') {
203-
if(dy < Math.min(dx * 0.6, MINSELECT)) {
203+
var direction = fullLayout.selectdirection;
204+
205+
if(fullLayout.selectdirection === 'any') {
206+
if(dy < Math.min(dx * 0.6, MINSELECT)) direction = 'h';
207+
else if(dx < Math.min(dy * 0.6, MINSELECT)) direction = 'v';
208+
else direction = 'd';
209+
}
210+
else {
211+
direction = fullLayout.selectdirection;
212+
}
213+
214+
if(direction === 'h') {
204215
// horizontal motion: make a vertical box
205216
currentPolygon = [[x0, 0], [x0, ph], [x1, ph], [x1, 0]];
206217
currentPolygon.xmin = Math.min(x0, x1);
@@ -214,7 +225,7 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
214225
'h4v' + (2 * MINSELECT) + 'h-4Z');
215226

216227
}
217-
else if(dx < Math.min(dy * 0.6, MINSELECT)) {
228+
else if(direction === 'v') {
218229
// vertical motion: make a horizontal box
219230
currentPolygon = [[0, y0], [0, y1], [pw, y1], [pw, y0]];
220231
currentPolygon.xmin = Math.min(0, pw);
@@ -226,7 +237,7 @@ function prepSelect(e, startX, startY, dragOptions, mode) {
226237
'M' + (x0 - MINSELECT) + ',' + (currentPolygon.ymax - 1) +
227238
'v4h' + (2 * MINSELECT) + 'v-4Z');
228239
}
229-
else {
240+
else if(direction === 'd') {
230241
// diagonal motion
231242
currentPolygon = [[x0, y0], [x0, y1], [x1, y1], [x1, y0]];
232243
currentPolygon.xmin = Math.min(x0, x1);

test/jasmine/tests/select_test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,69 @@ describe('@flaky Test select box and lasso in general:', function() {
552552
.catch(failTest)
553553
.then(done);
554554
});
555+
556+
it('should select the right data with the corresponding select direction', function(done) {
557+
558+
var gd = createGraphDiv();
559+
560+
// drag around just the center point, but if we have a selectdirection we may
561+
// get either the ones to the left and right or above and below
562+
var selectPath = [[175, 175], [225, 225]];
563+
564+
function selectDrag() {
565+
resetEvents(gd);
566+
drag(selectPath);
567+
return selectedPromise;
568+
}
569+
570+
function assertSelectedPointNumbers(pointNumbers) {
571+
var pts = selectedData.points;
572+
expect(pts.length).toBe(pointNumbers.length);
573+
pointNumbers.forEach(function(pointNumber, i) {
574+
expect(pts[i].pointNumber).toBe(pointNumber);
575+
});
576+
}
577+
578+
Plotly.newPlot(gd, [{
579+
x: [1, 1, 1, 2, 2, 2, 3, 3, 3],
580+
y: [1, 2, 3, 1, 2, 3, 1, 2, 3],
581+
mode: 'markers'
582+
}], {
583+
width: 400,
584+
height: 400,
585+
dragmode: 'select',
586+
margin: {l: 100, r: 100, t: 100, b: 100},
587+
xaxis: {range: [0, 4]},
588+
yaxis: {range: [0, 4]}
589+
})
590+
.then(selectDrag)
591+
.then(function() {
592+
expect(gd._fullLayout.selectdirection).toBe('any');
593+
assertSelectedPointNumbers([4]);
594+
595+
return Plotly.relayout(gd, {selectdirection: 'h'});
596+
})
597+
.then(selectDrag)
598+
.then(function() {
599+
assertSelectedPointNumbers([3, 4, 5]);
600+
601+
return Plotly.relayout(gd, {selectdirection: 'v'});
602+
})
603+
.then(selectDrag)
604+
.then(function() {
605+
assertSelectedPointNumbers([1, 4, 7]);
606+
607+
return Plotly.relayout(gd, {selectdirection: 'd'});
608+
})
609+
.then(selectDrag)
610+
.then(function() {
611+
assertSelectedPointNumbers([4]);
612+
})
613+
.catch(failTest)
614+
.then(done);
615+
616+
});
617+
555618
});
556619

557620
describe('@flaky Test select box and lasso per trace:', function() {

test/jasmine/tests/shapes_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ describe('A fixed size shape', function() {
10261026

10271027
var shapeAndResizeTypes = combinations(shapeTypes, resizeTypes);
10281028
shapeAndResizeTypes.forEach(function(testCase) {
1029-
describe('of type ' + testCase.type + ' can be ' + testCase.resizeDisplayName, function() {
1029+
describe('@flaky of type ' + testCase.type + ' can be ' + testCase.resizeDisplayName, function() {
10301030
resizeDirections.forEach(function(direction) {
10311031
it('over direction ' + direction, function(done) {
10321032
layout.shapes[0].type = testCase.type;

0 commit comments

Comments
 (0)