-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathselect.js
473 lines (400 loc) · 15.4 KB
/
select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var polybool = require('polybooljs');
var Registry = require('../../registry');
var Color = require('../../components/color');
var Fx = require('../../components/fx');
var polygon = require('../../lib/polygon');
var throttle = require('../../lib/throttle');
var makeEventData = require('../../components/fx/helpers').makeEventData;
var getFromId = require('./axis_ids').getFromId;
var sortModules = require('../sort_modules').sortModules;
var constants = require('./constants');
var MINSELECT = constants.MINSELECT;
var filteredPolygon = polygon.filter;
var polygonTester = polygon.tester;
var multipolygonTester = polygon.multitester;
function getAxId(ax) { return ax._id; }
function prepSelect(e, startX, startY, dragOptions, mode) {
var gd = dragOptions.gd;
var fullLayout = gd._fullLayout;
var zoomLayer = fullLayout._zoomlayer;
var dragBBox = dragOptions.element.getBoundingClientRect();
var plotinfo = dragOptions.plotinfo;
var xs = plotinfo.xaxis._offset;
var ys = plotinfo.yaxis._offset;
var x0 = startX - dragBBox.left;
var y0 = startY - dragBBox.top;
var x1 = x0;
var y1 = y0;
var path0 = 'M' + x0 + ',' + y0;
var pw = dragOptions.xaxes[0]._length;
var ph = dragOptions.yaxes[0]._length;
var xAxisIds = dragOptions.xaxes.map(getAxId);
var yAxisIds = dragOptions.yaxes.map(getAxId);
var allAxes = dragOptions.xaxes.concat(dragOptions.yaxes);
var subtract = e.altKey;
var filterPoly, testPoly, mergedPolygons, currentPolygon;
var i, cd, trace, searchInfo, eventData;
var selectingOnSameSubplot = (
fullLayout._lastSelectedSubplot &&
fullLayout._lastSelectedSubplot === plotinfo.id
);
if(
selectingOnSameSubplot &&
(e.shiftKey || e.altKey) &&
(plotinfo.selection && plotinfo.selection.polygons) &&
!dragOptions.polygons
) {
// take over selection polygons from prev mode, if any
dragOptions.polygons = plotinfo.selection.polygons;
dragOptions.mergedPolygons = plotinfo.selection.mergedPolygons;
} else if(
(!e.shiftKey && !e.altKey) ||
((e.shiftKey || e.altKey) && !plotinfo.selection)
) {
// create new polygons, if shift mode or selecting across different subplots
plotinfo.selection = {};
plotinfo.selection.polygons = dragOptions.polygons = [];
plotinfo.selection.mergedPolygons = dragOptions.mergedPolygons = [];
}
// clear selection outline when selecting a different subplot
if(!selectingOnSameSubplot) {
clearSelect(zoomLayer);
fullLayout._lastSelectedSubplot = plotinfo.id;
}
if(mode === 'lasso') {
filterPoly = filteredPolygon([[x0, y0]], constants.BENDPX);
}
var outlines = zoomLayer.selectAll('path.select-outline-' + plotinfo.id).data([1, 2]);
outlines.enter()
.append('path')
.attr('class', function(d) { return 'select-outline select-outline-' + d + ' select-outline-' + plotinfo.id; })
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', path0 + 'Z');
var corners = zoomLayer.append('path')
.attr('class', 'zoombox-corners')
.style({
fill: Color.background,
stroke: Color.defaultLine,
'stroke-width': 1
})
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', 'M0,0Z');
// find the traces to search for selection points
var searchTraces = [];
var throttleID = fullLayout._uid + constants.SELECTID;
var selection = [];
for(i = 0; i < gd.calcdata.length; i++) {
cd = gd.calcdata[i];
trace = cd[0].trace;
if(trace.visible !== true || !trace._module || !trace._module.selectPoints) continue;
if(dragOptions.subplot) {
if(
trace.subplot === dragOptions.subplot ||
trace.geo === dragOptions.subplot
) {
searchTraces.push({
_module: trace._module,
cd: cd,
xaxis: dragOptions.xaxes[0],
yaxis: dragOptions.yaxes[0]
});
}
} else if(
trace.type === 'splom' &&
// FIXME: make sure we don't have more than single axis for splom
trace._xaxes[xAxisIds[0]] && trace._yaxes[yAxisIds[0]]
) {
searchTraces.push({
_module: trace._module,
cd: cd,
xaxis: dragOptions.xaxes[0],
yaxis: dragOptions.yaxes[0]
});
} else {
if(xAxisIds.indexOf(trace.xaxis) === -1) continue;
if(yAxisIds.indexOf(trace.yaxis) === -1) continue;
searchTraces.push({
_module: trace._module,
cd: cd,
xaxis: getFromId(gd, trace.xaxis),
yaxis: getFromId(gd, trace.yaxis)
});
}
}
function axValue(ax) {
var index = (ax._id.charAt(0) === 'y') ? 1 : 0;
return function(v) { return ax.p2d(v[index]); };
}
function ascending(a, b) { return a - b; }
// allow subplots to override fillRangeItems routine
var fillRangeItems;
if(plotinfo.fillRangeItems) {
fillRangeItems = plotinfo.fillRangeItems;
} else {
if(mode === 'select') {
fillRangeItems = function(eventData, poly) {
var ranges = eventData.range = {};
for(i = 0; i < allAxes.length; i++) {
var ax = allAxes[i];
var axLetter = ax._id.charAt(0);
ranges[ax._id] = [
ax.p2d(poly[axLetter + 'min']),
ax.p2d(poly[axLetter + 'max'])
].sort(ascending);
}
};
} else {
fillRangeItems = function(eventData, poly, filterPoly) {
var dataPts = eventData.lassoPoints = {};
for(i = 0; i < allAxes.length; i++) {
var ax = allAxes[i];
dataPts[ax._id] = filterPoly.filtered.map(axValue(ax));
}
};
}
}
dragOptions.moveFn = function(dx0, dy0) {
x1 = Math.max(0, Math.min(pw, dx0 + x0));
y1 = Math.max(0, Math.min(ph, dy0 + y0));
var dx = Math.abs(x1 - x0),
dy = Math.abs(y1 - y0);
if(mode === 'select') {
var direction = fullLayout.selectdirection;
if(fullLayout.selectdirection === 'any') {
if(dy < Math.min(dx * 0.6, MINSELECT)) direction = 'h';
else if(dx < Math.min(dy * 0.6, MINSELECT)) direction = 'v';
else direction = 'd';
}
else {
direction = fullLayout.selectdirection;
}
if(direction === 'h') {
// horizontal motion: make a vertical box
currentPolygon = [[x0, 0], [x0, ph], [x1, ph], [x1, 0]];
currentPolygon.xmin = Math.min(x0, x1);
currentPolygon.xmax = Math.max(x0, x1);
currentPolygon.ymin = Math.min(0, ph);
currentPolygon.ymax = Math.max(0, ph);
// extras to guide users in keeping a straight selection
corners.attr('d', 'M' + currentPolygon.xmin + ',' + (y0 - MINSELECT) +
'h-4v' + (2 * MINSELECT) + 'h4Z' +
'M' + (currentPolygon.xmax - 1) + ',' + (y0 - MINSELECT) +
'h4v' + (2 * MINSELECT) + 'h-4Z');
}
else if(direction === 'v') {
// vertical motion: make a horizontal box
currentPolygon = [[0, y0], [0, y1], [pw, y1], [pw, y0]];
currentPolygon.xmin = Math.min(0, pw);
currentPolygon.xmax = Math.max(0, pw);
currentPolygon.ymin = Math.min(y0, y1);
currentPolygon.ymax = Math.max(y0, y1);
corners.attr('d', 'M' + (x0 - MINSELECT) + ',' + currentPolygon.ymin +
'v-4h' + (2 * MINSELECT) + 'v4Z' +
'M' + (x0 - MINSELECT) + ',' + (currentPolygon.ymax - 1) +
'v4h' + (2 * MINSELECT) + 'v-4Z');
}
else if(direction === 'd') {
// diagonal motion
currentPolygon = [[x0, y0], [x0, y1], [x1, y1], [x1, y0]];
currentPolygon.xmin = Math.min(x0, x1);
currentPolygon.xmax = Math.max(x0, x1);
currentPolygon.ymin = Math.min(y0, y1);
currentPolygon.ymax = Math.max(y0, y1);
corners.attr('d', 'M0,0Z');
}
}
else if(mode === 'lasso') {
filterPoly.addPt([x1, y1]);
currentPolygon = filterPoly.filtered;
}
// create outline & tester
if(dragOptions.polygons && dragOptions.polygons.length) {
mergedPolygons = mergePolygons(dragOptions.mergedPolygons, currentPolygon, subtract);
currentPolygon.subtract = subtract;
testPoly = multipolygonTester(dragOptions.polygons.concat([currentPolygon]));
}
else {
mergedPolygons = [currentPolygon];
testPoly = polygonTester(currentPolygon);
}
// draw selection
var paths = [];
for(i = 0; i < mergedPolygons.length; i++) {
var ppts = mergedPolygons[i];
paths.push(ppts.join('L') + 'L' + ppts[0]);
}
outlines
.attr('d', 'M' + paths.join('M') + 'Z');
throttle.throttle(
throttleID,
constants.SELECTDELAY,
function() {
selection = [];
var thisSelection, traceSelections = [], traceSelection;
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
traceSelection = searchInfo._module.selectPoints(searchInfo, testPoly);
traceSelections.push(traceSelection);
thisSelection = fillSelectionItem(traceSelection, searchInfo);
if(selection.length) {
for(var j = 0; j < thisSelection.length; j++) {
selection.push(thisSelection[j]);
}
}
else selection = thisSelection;
}
eventData = {points: selection};
updateSelectedState(gd, searchTraces, eventData);
fillRangeItems(eventData, currentPolygon, filterPoly);
dragOptions.gd.emit('plotly_selecting', eventData);
}
);
};
dragOptions.clickFn = function(numClicks, evt) {
corners.remove();
throttle.done(throttleID).then(function() {
throttle.clear(throttleID);
if(numClicks === 2) {
// clear selection on doubleclick
outlines.remove();
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo._module.selectPoints(searchInfo, false);
}
updateSelectedState(gd, searchTraces);
gd.emit('plotly_deselect', null);
}
else {
// TODO: remove in v2 - this was probably never intended to work as it does,
// but in case anyone depends on it we don't want to break it now.
gd.emit('plotly_selected', undefined);
}
Fx.click(gd, evt);
});
};
dragOptions.doneFn = function() {
corners.remove();
throttle.done(throttleID).then(function() {
throttle.clear(throttleID);
dragOptions.gd.emit('plotly_selected', eventData);
if(currentPolygon && dragOptions.polygons) {
// save last polygons
currentPolygon.subtract = subtract;
dragOptions.polygons.push(currentPolygon);
// we have to keep reference to arrays container
dragOptions.mergedPolygons.length = 0;
[].push.apply(dragOptions.mergedPolygons, mergedPolygons);
}
});
};
}
function updateSelectedState(gd, searchTraces, eventData) {
var i, j, searchInfo, trace;
if(eventData) {
var pts = eventData.points || [];
for(i = 0; i < searchTraces.length; i++) {
trace = searchTraces[i].cd[0].trace;
trace.selectedpoints = [];
trace._input.selectedpoints = [];
}
for(i = 0; i < pts.length; i++) {
var pt = pts[i];
var data = pt.data;
var fullData = pt.fullData;
if(pt.pointIndices) {
[].push.apply(data.selectedpoints, pt.pointIndices);
[].push.apply(fullData.selectedpoints, pt.pointIndices);
} else {
data.selectedpoints.push(pt.pointIndex);
fullData.selectedpoints.push(pt.pointIndex);
}
}
}
else {
for(i = 0; i < searchTraces.length; i++) {
trace = searchTraces[i].cd[0].trace;
delete trace.selectedpoints;
delete trace._input.selectedpoints;
}
}
// group searchInfo traces by trace modules
var lookup = {};
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
var name = searchInfo._module.name;
if(lookup[name]) {
lookup[name].push(searchInfo);
} else {
lookup[name] = [searchInfo];
}
}
var keys = Object.keys(lookup).sort(sortModules);
for(i = 0; i < keys.length; i++) {
var items = lookup[keys[i]];
var len = items.length;
var item0 = items[0];
var trace0 = item0.cd[0].trace;
if(Registry.traceIs(trace0, 'regl')) {
// plot regl traces per module
var cds = new Array(len);
for(j = 0; j < len; j++) {
cds[j] = items[j].cd;
}
item0._module.style(gd, cds);
} else {
// plot svg trace per trace
for(j = 0; j < len; j++) {
item0._module.style(gd, items[j].cd);
}
}
}
}
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
}, {
regions: [poly],
inverted: false
});
return res.regions;
}
function fillSelectionItem(selection, searchInfo) {
if(Array.isArray(selection)) {
var cd = searchInfo.cd;
var trace = searchInfo.cd[0].trace;
for(var i = 0; i < selection.length; i++) {
selection[i] = makeEventData(selection[i], trace, cd);
}
}
return selection;
}
function clearSelect(zoomlayer) {
// until we get around to persistent selections, remove the outline
// here. The selection itself will be removed when the plot redraws
// at the end.
zoomlayer.selectAll('.select-outline').remove();
}
module.exports = {
prepSelect: prepSelect,
clearSelect: clearSelect
};