8000 Add multiselect by dy · Pull Request #2140 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Add multiselect #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add subtraction mode
  • Loading branch information
dy committed Nov 3, 2017
commit e8c18f03ed8c1e5a3d0ac61be97f5b2f131ef36a
10 changes: 8 additions & 2 deletions src/lib/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ polygon.multitester = function multitester(list) {

for(var i = 0; i < list.length; i++) {
var tester = polygon.tester(list[i]);
tester.subtract = list[i].subtract;
testers.push(tester);
xmin = Math.min(xmin, tester.xmin);
xmax = Math.max(xmax, tester.xmax);
Expand All @@ -194,10 +195,15 @@ polygon.multitester = function multitester(list) {
}

function contains(pt, arg) {
var yes = false;
for(var i = 0; i < testers.length; i++) {
if(testers[i].contains(pt, arg)) return true;
if(testers[i].contains(pt, arg)) {
// if contained by subtract polygon - exclude the point
yes = testers[i].subtract === false;
}
}
return false;

return yes;
}

return {
Expand Down
6 changes: 3 additions & 3 deletions src/plots/cartesian/dragbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
dragOptions.yaxes = ya;

// take over selection polygons from prev mode, if any
if(e.shiftKey && plotinfo.selection.polygons && !dragOptions.polygons) {
if((e.shiftKey || e.altKey) && plotinfo.selection.polygons && !dragOptions.polygons) {
dragOptions.polygons = plotinfo.selection.polygons;
dragOptions.mergedPolygons = plotinfo.selection.mergedPolygons;
}
// create new polygons, if shift mode
else if(!e.shiftKey || (e.shiftKey && !plotinfo.selection.polygons)) {
else if((!e.shiftKey && !e.altKey) || ((e.shiftKey || e.altKey) && !plotinfo.selection.polygons)) {
plotinfo.selection = {};
plotinfo.selection.polygons = dragOptions.polygons = [];
dragOptions.mergedPolygons = plotinfo.selection.mergedPolygons = [];
plotinfo.selection.mergedPolygons = dragOptions.mergedPolygons = [];
}

prepSelect(e, startX, startY, dragOptions, dragModeNow);
Expand Down
25 changes: 21 additions & 4 deletions src/plots/cartesian/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
xAxisIds = dragOptions.xaxes.map(getAxId),
yAxisIds = dragOptions.yaxes.map(getAxId),
allAxes = dragOptions.xaxes.concat(dragOptions.yaxes),
filterPoly, testPoly, mergedPolygons, currentPolygon;
filterPoly, testPoly, mergedPolygons, currentPolygon,
subtract = e.altKey;

if(mode === 'lasso') {
filterPoly = filteredPolygon([[x0, y0]], constants.BENDPX);
Expand Down Expand Up @@ -193,7 +194,8 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {

// create outline & tester
if(dragOptions.polygons && dragOptions.polygons.length) {
mergedPolygons = joinPolygons(dragOptions.mergedPolygons, currentPolygon);
mergedPolygons = mergePolygons(dragOptions.mergedPolygons, currentPolygon, subtract);
currentPolygon.subtract = subtract;
testPoly = multipolygonTester(dragOptions.polygons.concat([currentPolygon]));
}
else {
Expand Down Expand Up @@ -262,6 +264,7 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {

if(currentPolygon && dragOptions.polygons) {
// save last polygons
currentPolygon.subtract = subtract;
dragOptions.polygons.push(currentPolygon);

// we have to keep reference to arrays container
Expand All @@ -272,8 +275,22 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
};
};

function joinPolygons(list, poly) {
var res = polybool.union({
function mergePolygons(list, poly, subtract) {
var res;

if(subtract) {
res = polybool.difference({
regions: list,
inverted: false
}, {
regions: [poly],
inverted: false
});

return res.regions;
}

res = polybool.union({
regions: list,
inverted: false
}, {
Expand Down
0