Skip to content

Commit 556cb83

Browse files
committed
split out scatter.selectPoints to a new file
1 parent 057e4ac commit 556cb83

File tree

3 files changed

+110
-76
lines changed

3 files changed

+110
-76
lines changed

src/traces/scatter/index.js

+15-76
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@
99

1010
'use strict';
1111

12-
var Plotly = require('../../plotly');
1312
var d3 = require('d3');
1413
var isNumeric = require('fast-isnumeric');
1514

15+
var Plotly = require('../../plotly');
16+
17+
var subtypes = require('./subtypes');
18+
1619
var scatter = module.exports = {};
1720

21+
scatter.hasLines = subtypes.hasLines;
22+
scatter.hasMarkers = subtypes.hasMarkers;
23+
scatter.hasText = subtypes.hasText;
24+
scatter.isBubble = subtypes.isBubble;
25+
26+
scatter.selectPoints = require('./select');
27+
1828
Plotly.Plots.register(scatter, 'scatter',
1929
['cartesian', 'symbols', 'markerColorscale', 'errorBarsOK', 'showLegend'], {
2030
description: [
@@ -196,26 +206,6 @@ scatter.cleanData = function(fullData) {
196206
}
197207
};
198208

199-
scatter.hasLines = function(trace) {
200-
return trace.visible && trace.mode &&
201-
trace.mode.indexOf('lines') !== -1;
202-
};
203-
204-
scatter.hasMarkers = function(trace) {
205-
return trace.visible && trace.mode &&
206-
trace.mode.indexOf('markers') !== -1;
207-
};
208-
209-
scatter.hasText = function(trace) {
210-
return trace.visible && trace.mode &&
211-
trace.mode.indexOf('text') !== -1;
212-
};
213-
214-
scatter.isBubble = function(trace) {
215-
return (typeof trace.marker === 'object' &&
216-
Array.isArray(trace.marker.size));
217-
};
218-
219209
scatter.colorbar = require('./colorbar');
220210

221211
// used in the drawing step for 'scatter' and 'scattegeo' and
@@ -455,17 +445,17 @@ scatter.plot = function(gd, plotinfo, cdscatter) {
455445
tozero,tonext,nexttonext;
456446
scattertraces.each(function(d){
457447
var trace = d[0].trace,
458-
line = trace.line;
448+
line = trace.line,
449+
tr = d3.select(this);
459450
if(trace.visible !== true) return;
460451

461-
d[0].node = this; // store node for tweaking by selectPoints
452+
d[0].node3 = tr; // store node for tweaking by selectPoints
462453

463454
scatter.arraysToCalcdata(d);
464455

465456
if(!scatter.hasLines(trace) && trace.fill==='none') return;
466457

467-
var tr = d3.select(this),
468-
thispath,
458+
var thispath,
469459
// fullpath is all paths for this curve, joined together straight
470460
// across gaps, for filling
471461
fullpath = '',
@@ -857,54 +847,3 @@ scatter.hoverPoints = function(pointData, xval, yval, hovermode) {
857847

858848
return [pointData];
859849
};
860-
861-
var DESELECTDIM = 0.2;
862-
863-
scatter.selectPoints = function(searchInfo, polygon) {
864-
var cd = searchInfo.cd,
865-
xa = searchInfo.xaxis,
866-
ya = searchInfo.yaxis,
867-
selection = [],
868-
trace = cd[0].trace,
869-
curveNumber = trace.index,
870-
marker = trace.marker,
871-
i,
872-
di,
873-
x,
874-
y;
875-
876-
if(!scatter.hasMarkers(trace)) return; // TODO: include text and/or lines?
877-
878-
var opacity = Array.isArray(marker.opacity) ? 1 : marker.opacity;
879-
880-
if(polygon === false) { // clear selection
881-
for(i = 0; i < cd.length; i++) cd[i].dim = 0;
882-
}
883-
else {
884-
for(i = 0; i < cd.length; i++) {
885-
di = cd[i];
886-
x = xa.c2p(di.x);
887-
y = ya.c2p(di.y);
888-
if(polygon.contains([x, y])) {
889-
selection.push({
890-
curveNumber: curveNumber,
891-
pointNumber: i,
892-
x: di.x,
893-
y: di.y
894-
});
895-
di.dim = 0;
896-
}
897-
else di.dim = 1;
898-
}
899-
}
900-
901-
// do the dimming here, as well as returning the selection
902-
// The logic here duplicates Drawing.pointStyle, but I don't want
903-
// d.dim in pointStyle in case something goes wrong with selection.
904-
d3.select(cd[0].node).selectAll('path.point')
905-
.style('opacity', function(d) {
906-
return ((d.mo+1 || opacity+1) - 1) * (d.dim ? DESELECTDIM : 1);
907-
});
908-
909-
return selection;
910-
};

src/traces/scatter/select.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var hasMarkers = require('./subtypes').hasMarkers;
13+
14+
var DESELECTDIM = 0.2;
15+
16+
module.exports = function selectPoints(searchInfo, polygon) {
17+
var cd = searchInfo.cd,
18+
xa = searchInfo.xaxis,
19+
ya = searchInfo.yaxis,
20+
selection = [],
21+
trace = cd[0].trace,
22+
curveNumber = trace.index,
23+
marker = trace.marker,
24+
i,
25+
di,
26+
x,
27+
y;
28+
29+
if(!hasMarkers(trace)) return; // TODO: include text and/or lines?
30+
31+
var opacity = Array.isArray(marker.opacity) ? 1 : marker.opacity;
32+
33+
if(polygon === false) { // clear selection
34+
for(i = 0; i < cd.length; i++) cd[i].dim = 0;
35+
}
36+
else {
37+
for(i = 0; i < cd.length; i++) {
38+
di = cd[i];
39+
x = xa.c2p(di.x);
40+
y = ya.c2p(di.y);
41+
if(polygon.contains([x, y])) {
42+
selection.push({
43+
curveNumber: curveNumber,
44+
pointNumber: i,
45+
x: di.x,
46+
y: di.y
47+
});
48+
di.dim = 0;
49+
}
50+
else di.dim = 1;
51+
}
52+
}
53+
54+
// do the dimming here, as well as returning the selection
55+
// The logic here duplicates Drawing.pointStyle, but I don't want
56+
// d.dim in pointStyle in case something goes wrong with selection.
57+
cd[0].node3.selectAll('path.point')
58+
.style('opacity', function(d) {
59+
return ((d.mo+1 || opacity+1) - 1) * (d.dim ? DESELECTDIM : 1);
60+
});
61+
62+
return selection;
63+
};

src/traces/scatter/subtypes.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
module.exports = {
13+
hasLines: function(trace) {
14+
return trace.visible && trace.mode &&
15+
trace.mode.indexOf('lines') !== -1;
16+
},
17+
18+
hasMarkers: function(trace) {
19+
return trace.visible && trace.mode &&
20+
trace.mode.indexOf('markers') !== -1;
21+
},
22+
23+
hasText: function(trace) {
24+
return trace.visible && trace.mode &&
25+
trace.mode.indexOf('text') !== -1;
26+
},
27+
28+
isBubble: function(trace) {
29+
return (typeof trace.marker === 'object' &&
30+
Array.isArray(trace.marker.size));
31+
}
32+
};

0 commit comments

Comments
 (0)