Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b209a55

Browse files
committed
Partial stateful select
1 parent 8b60b4e commit b209a55

File tree

8 files changed

+154
-18
lines changed

8 files changed

+154
-18
lines changed

src/plots/cartesian/select.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
204204

205205
eventData = {points: selection};
206206

207-
updateSelectionState(gd, eventData);
207+
updatedSelectedState(gd, eventData);
208208

209209
fillRangeItems(eventData, poly, pts);
210210
dragOptions.gd.emit('plotly_selecting', eventData);
211+
212+
var bpms = gd._fullLayout._basePlotModules;
213+
for (var i = 0; i < bpms.length; i++) {
214+
bpms[i].plot(gd, null, {duration: 0});
215+
}
211216
}
212217
);
213218
};
@@ -225,20 +230,25 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
225230
searchInfo.selectPoints(searchInfo, false);
226231
}
227232

228-
updateSelectionState(gd, {points: []});
233+
updatedSelectedState(gd, {points: []});
229234

230235
gd.emit('plotly_deselect', null);
231236
}
232237
else {
233-
updateSelectionState(gd, eventData);
238+
updatedSelectedState(gd, eventData);
234239

235240
dragOptions.gd.emit('plotly_selected', eventData);
236241
}
242+
243+
var bpms = gd._fullLayout._basePlotModules;
244+
for (var i = 0; i < bpms.length; i++) {
245+
bpms[i].plot(gd, null, {duration: 0});
246+
}
237247
});
238248
};
239249
};
240250

241-
function updateSelectionState(gd, eventData) {
251+
function updatedSelectedState(gd, eventData) {
242252
var i, pt, trace, fullTrace;
243253
var data = gd.data;
244254
var fullData = gd._fullData;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2012-2017, 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 = function applySelectionToCalcdata (cd) {
13+
var i;
14+
console.log('cd:', cd);
15+
16+
var trace = cd[0].t;
17+
console.log('trace:', trace);
18+
19+
var selectedpoints = trace.selectedpoints;
20+
var selectedids = trace.selectedids;
21+
22+
if (!selectedpoints) {
23+
return;
24+
}
25+
26+
var selectedPointIndex = {};
27+
for (i = 0; i < selectedpoints.length; i++) {
28+
selectedPointIndex[selectedpoints[i]] = true;
29+
}
30+
31+
for (i = 0; i < cd.length; i++) {
32+
var pt = cd[i];
33+
34+
if (!selectedpoints) {
35+
delete pt.selected;
36+
} else {
37+
pt.selected = !!selectedPointIndex[i];
38+
}
39+
}
40+
41+
}

src/traces/scatter/attributes.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ module.exports = {
421421
valType: 'number',
422422
min: 0,
423423
max: 1,
424+
dflt: 0.5,
424425
arrayOk: true,
425426
role: 'style',
426427
editType: 'style',
@@ -454,32 +455,28 @@ module.exports = {
454455
)
455456
},
456457
selectedpoints: {
457-
valType: 'data_array',
458+
valType: 'any',
458459
role: 'info',
459-
arrayOk: true,
460460
editType: 'calc',
461-
description: 'Integer index of selected points.',
461+
description: 'Array containing integer indices of selected points.',
462462
},
463463
selectedids: {
464-
valType: 'data_array',
464+
valType: 'any',
465465
role: 'info',
466-
arrayOk: true,
467466
editType: 'calc',
468-
description: 'ID of selected points.',
467+
description: 'Array containing `ids` of selected points.',
469468
},
470469
hoverpoints: {
471-
valType: 'data_array',
470+
valType: 'any',
472471
role: 'info',
473-
arrayOk: true,
474472
editType: 'calc',
475-
description: 'Integer index of hovered points.',
473+
description: 'Array containing integer indices of hovered points.',
476474
},
477475
hoverids: {
478-
valType: 'data_array',
476+
valType: 'any',
479477
role: 'info',
480-
arrayOk: true,
481478
editType: 'calc',
482-
description: 'ID of hovered points.',
479+
description: 'Array containing `ids` of hovered points.',
483480
},
484481
textposition: {
485482
valType: 'enumerated',

src/traces/scatter/calc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var BADNUM = require('../../constants/numerical').BADNUM;
1717
var subTypes = require('./subtypes');
1818
var calcColorscale = require('./colorscale_calc');
1919
var arraysToCalcdata = require('./arrays_to_calcdata');
20+
var applySelectionToCalcdata = require('./apply_selection_to_calcdata');
2021

2122

2223
module.exports = function calc(gd, trace) {
@@ -31,6 +32,10 @@ module.exports = function calc(gd, trace) {
3132
s,
3233
i;
3334

35+
var selectedPointIndex = {};
36+
var hoverPointIndex = {};
37+
var idIndex;
38+
3439
// cancel minimum tick spacings (only applies to bars and boxes)
3540
xa._minDtick = 0;
3641
ya._minDtick = 0;
@@ -124,6 +129,8 @@ module.exports = function calc(gd, trace) {
124129

125130
arraysToCalcdata(cd, trace);
126131

132+
applySelectionToCalcdata(cd);
133+
127134
gd.firstscatter = false;
128135
return cd;
129136
};

src/traces/scatter/defaults.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var handleLineShapeDefaults = require('./line_shape_defaults');
2121
var handleTextDefaults = require('./text_defaults');
2222
var handleFillColorDefaults = require('./fillcolor_defaults');
2323
var errorBarsSupplyDefaults = require('../../components/errorbars/defaults');
24+
var handleInteractionDefaults = require('./interaction_defaults');
2425

2526

2627
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
@@ -40,6 +41,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4041
coerce('hovertext');
4142
coerce('mode', defaultMode);
4243

44+
handleInteractionDefaults(traceIn, traceOut, coerce);
45+
4346
if(subTypes.hasLines(traceOut)) {
4447
handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce);
4548
handleLineShapeDefaults(traceIn, traceOut, coerce);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2012-2017, 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 Lib = require('../../lib');
13+
var isNumeric = require('fast-isnumeric');
14+
15+
module.exports = function supplyInteractionDefaults (traceIn, traceOut, coerce) {
16+
17+
function coerceInteractionDefault(attr, containsNumbers) {
18+
var i, arr;
19+
20+
if (!Array.isArray(traceIn[attr])) {
21+
return;
22+
}
23+
coerce(attr);
24+
25+
arr = traceOut[attr];
26+
27+
console.log('arr:', arr);
28+
29+
if (containsNumbers) {
30+
for (i = arr.length - 1; i >= 0; i--) {
31+
if (!isNumeric(arr[i])) {
32+
arr.splice(i, 1);
33+
} else {
34+
arr[i] = +arr[i];
35+
}
36+
}
37+
} else { // only other implemented is integer:
38+
for (i = arr.length - 1; i >= 0; i--) {
39+
if (!arr[i]) {
40+
arr.splice(i, 1);
41+
} else {
42+
arr[i] = String(arr[i]);
43+
}
44+
}
45+
}
46+
47+
if (arr.length === 0) {
48+
delete traceOut[attr];
49+
}
50+
}
51+
52+
coerceInteractionDefault('selectedpoints', true);
53+
coerceInteractionDefault('selectedids');
54+
coerceInteractionDefault('hoverpoints', true);
55+
coerceInteractionDefault('hoverids');
56+
};

src/traces/scatter/marker_defaults.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ module.exports = function markerDefaults(traceIn, traceOut, defaultColor, layout
3939
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'});
4040
}
4141

42+
coerce('selected.marker.opacity', isBubble ? 0.7 : 1);
43+
coerce('unselected.marker.opacity', 0.5);
44+
45+
coerce('selected.marker.color', defaultColor);
46+
coerce('unselected.marker.color', defaultColor);
47+
48+
coerce('selected.marker.size')
49+
coerce('unselected.marker.size')
50+
4251
if(!opts.noLine) {
4352
// if there's a line with a different color than the marker, use
4453
// that line color as the default marker line color

test/image/mocks/point-selection.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"color": "red",
88
"size": 12
99
},
10+
"selectedpoints": [1, 2, 3],
1011
"selected": {
1112
"marker": {
1213
"color": "blue",
@@ -19,7 +20,13 @@
1920
"size": 6,
2021
"opacity": 0.5
2122
}
22-
}
23+
},
24+
"transforms": [{
25+
"type": "filter",
26+
"target": "x",
27+
"operation": "][",
28+
"value": [3.5, 4.5]
29+
}]
2330
}, {
2431
"x": [1, 2, 3, 4, 5, 6],
2532
"y": [6, 5, 6, 5, 4, 3],
@@ -40,7 +47,13 @@
4047
"size": 6,
4148
"opacity": 0.5
4249
}
43-
}
50+
},
51+
"transforms": [{
52+
"type": "filter",
53+
"target": "x",
54+
"operation": "][",
55+
"value": [3.5, 4.5]
56+
}]
4457
}],
4558
"layout": {
4659
"dragmode": "lasso"

0 commit comments

Comments
 (0)