-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathselect.js
287 lines (244 loc) · 9.5 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
/**
* Copyright 2012-2017, 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 polygon = require('../../lib/polygon');
var throttle = require('../../lib/throttle');
var color = require('../../components/color');
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
var axes = require('./axes');
var constants = require('./constants');
var filteredPolygon = polygon.filter;
var polygonTester = polygon.tester;
var MINSELECT = constants.MINSELECT;
function getAxId(ax) { return ax._id; }
module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
var gd = dragOptions.gd,
fullLayout = gd._fullLayout,
zoomLayer = fullLayout._zoomlayer,
dragBBox = dragOptions.element.getBoundingClientRect(),
plotinfo = dragOptions.plotinfo,
xs = plotinfo.xaxis._offset,
ys = plotinfo.yaxis._offset,
x0 = startX - dragBBox.left,
y0 = startY - dragBBox.top,
x1 = x0,
y1 = y0,
path0 = 'M' + x0 + ',' + y0,
pw = dragOptions.xaxes[0]._length,
ph = dragOptions.yaxes[0]._length,
xAxisIds = dragOptions.xaxes.map(getAxId),
yAxisIds = dragOptions.yaxes.map(getAxId),
allAxes = dragOptions.xaxes.concat(dragOptions.yaxes),
pts;
if(mode === 'lasso') {
pts = filteredPolygon([[x0, y0]], constants.BENDPX);
}
var outlines = zoomLayer.selectAll('path.select-outline').data([1, 2]);
outlines.enter()
.append('path')
.attr('class', function(d) { return 'select-outline select-outline-' + d; })
.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 = [];
var i, cd, trace, searchInfo, eventData;
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({
selectPoints: trace._module.selectPoints,
style: trace._module.style,
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({
selectPoints: trace._module.selectPoints,
style: trace._module.style,
cd: cd,
xaxis: axes.getFromId(gd, trace.xaxis),
yaxis: axes.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, pts) {
var dataPts = eventData.lassoPoints = {};
for(i = 0; i < allAxes.length; i++) {
var ax = allAxes[i];
dataPts[ax._id] = pts.filtered.map(axValue(ax));
}
};
}
}
dragOptions.moveFn = function(dx0, dy0) {
var poly;
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') {
if(dy < Math.min(dx * 0.6, MINSELECT)) {
// horizontal motion: make a vertical box
poly = polygonTester([[x0, 0], [x0, ph], [x1, ph], [x1, 0]]);
// extras to guide users in keeping a straight selection
corners.attr('d', 'M' + poly.xmin + ',' + (y0 - MINSELECT) +
'h-4v' + (2 * MINSELECT) + 'h4Z' +
'M' + (poly.xmax - 1) + ',' + (y0 - MINSELECT) +
'h4v' + (2 * MINSELECT) + 'h-4Z');
}
else if(dx < Math.min(dy * 0.6, MINSELECT)) {
// vertical motion: make a horizontal box
poly = polygonTester([[0, y0], [0, y1], [pw, y1], [pw, y0]]);
corners.attr('d', 'M' + (x0 - MINSELECT) + ',' + poly.ymin +
'v-4h' + (2 * MINSELECT) + 'v4Z' +
'M' + (x0 - MINSELECT) + ',' + (poly.ymax - 1) +
'v4h' + (2 * MINSELECT) + 'v-4Z');
}
else {
// diagonal motion
poly = polygonTester([[x0, y0], [x0, y1], [x1, y1], [x1, y0]]);
corners.attr('d', 'M0,0Z');
}
outlines.attr('d', 'M' + poly.xmin + ',' + poly.ymin +
'H' + (poly.xmax - 1) + 'V' + (poly.ymax - 1) +
'H' + poly.xmin + 'Z');
}
else if(mode === 'lasso') {
pts.addPt([x1, y1]);
poly = polygonTester(pts.filtered);
outlines.attr('d', 'M' + pts.filtered.join('L') + 'Z');
}
throttle.throttle(
throttleID,
constants.SELECTDELAY,
function() {
selection = [];
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
var thisSelection = fillSelectionItem(
searchInfo.selectPoints(searchInfo, poly), 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, poly, pts);
dragOptions.gd.emit('plotly_selecting', eventData);
}
);
};
dragOptions.doneFn = function(dragged, numclicks) {
corners.remove();
throttle.done(throttleID).then(function() {
throttle.clear(throttleID);
if(!dragged && numclicks === 2) {
// clear selection on doubleclick
outlines.remove();
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo.selectPoints(searchInfo, false);
}
updateSelectedState(gd, searchTraces);
gd.emit('plotly_deselect', null);
}
else {
dragOptions.gd.emit('plotly_selected', eventData);
}
});
};
};
function updateSelectedState(gd, searchTraces, eventData) {
var i, searchInfo;
if(eventData) {
var pts = eventData.points || [];
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo.cd[0].trace.selectedpoints = [];
searchInfo.cd[0].trace._input.selectedpoints = [];
}
for(i = 0; i < pts.length; i++) {
var pt = pts[i];
var ptNumber = pt.pointNumber;
pt.data.selectedpoints.push(ptNumber);
pt.fullData.selectedpoints.push(ptNumber);
}
}
else {
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
delete searchInfo.cd[0].trace.selectedpoints;
delete searchInfo.cd[0].trace._input.selectedpoints;
}
}
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
if(searchInfo.style) searchInfo.style(gd, searchInfo.cd);
}
}
function fillSelectionItem(selection, searchInfo) {
if(Array.isArray(selection)) {
var trace = searchInfo.cd[0].trace;
for(var i = 0; i < selection.length; i++) {
var sel = selection[i];
sel.curveNumber = trace.index;
sel.data = trace._input;
sel.fullData = trace;
appendArrayPointValue(sel, trace, sel.pointNumber);
}
}
return selection;
}