Skip to content

Commit 3399a58

Browse files
committed
fill selection event data in data, fullData and array values
- to bring its point data on-par with hover/click/unhover
1 parent d9373aa commit 3399a58

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

src/plots/cartesian/select.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var polygon = require('../../lib/polygon');
1313
var color = require('../../components/color');
14+
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
1415

1516
var axes = require('./axes');
1617
var constants = require('./constants');
@@ -151,7 +152,9 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
151152
selection = [];
152153
for(i = 0; i < searchTraces.length; i++) {
153154
searchInfo = searchTraces[i];
154-
[].push.apply(selection, searchInfo.selectPoints(searchInfo, poly));
155+
[].push.apply(selection, fillSelectionItem(
156+
searchInfo.selectPoints(searchInfo, poly), searchInfo
157+
));
155158
}
156159

157160
eventData = {points: selection};
@@ -196,3 +199,20 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
196199
}
197200
};
198201
};
202+
203+
function fillSelectionItem(selection, searchInfo) {
204+
if(Array.isArray(selection)) {
205+
var trace = searchInfo.cd[0].trace;
206+
207+
for(var i = 0; i < selection.length; i++) {
208+
var sel = selection[i];
209+
210+
sel.curveNumber = trace.index;
211+
sel.data = trace._input;
212+
sel.fullData = trace;
213+
appendArrayPointValue(sel, trace, sel.pointNumber);
214+
}
215+
}
216+
217+
return selection;
218+
}

src/traces/scatter/select.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module.exports = function selectPoints(searchInfo, polygon) {
1919
ya = searchInfo.yaxis,
2020
selection = [],
2121
trace = cd[0].trace,
22-
curveNumber = trace.index,
2322
marker = trace.marker,
2423
i,
2524
di,
@@ -43,12 +42,9 @@ module.exports = function selectPoints(searchInfo, polygon) {
4342

4443
if(polygon.contains([x, y])) {
4544
selection.push({
46-
curveNumber: curveNumber,
4745
pointNumber: i,
4846
x: di.x,
49-
y: di.y,
50-
// TODO generalize with hover/click data handler
51-
id: di.id
47+
y: di.y
5248
});
5349
di.dim = 0;
5450
}

test/jasmine/tests/select_test.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ describe('select box and lasso', function() {
4242
expect(actual.y).toBeCloseToArray(expected.y, PRECISION);
4343
}
4444

45+
function assertEventData(actual, expected, msg) {
46+
expect(actual.length).toBe(expected.length, msg + ' same number of pts');
47+
48+
expected.forEach(function(e, i) {
49+
var a = actual[i];
50+
var m = msg + ' (pt ' + i + ')';
51+
52+
expect(a.data).toBeDefined(m + ' has data ref');
53+
expect(a.fullData).toBeDefined(m + ' has fullData ref');
54+
expect(Object.keys(a).length - 2).toBe(Object.keys(e).length, m + ' has correct number of keys');
55+
56+
Object.keys(e).forEach(function(k) {
57+
expect(a[k]).toBe(e[k], m + ' ' + k);
58+
});
59+
});
60+
}
61+
4562
describe('select elements', function() {
4663
var mockCopy = Lib.extendDeep({}, mock);
4764
mockCopy.layout.dragmode = 'select';
@@ -143,6 +160,10 @@ describe('select box and lasso', function() {
143160
describe('select events', function() {
144161
var mockCopy = Lib.extendDeep({}, mock);
145162
mockCopy.layout.dragmode = 'select';
163+
mockCopy.data[0].ids = mockCopy.data[0].x
164+
.map(function(v) { return 'id-' + v; });
165+
mockCopy.data[0].customdata = mockCopy.data[0].y
166+
.map(function(v) { return 'customdata-' + v; });
146167

147168
var gd;
148169
beforeEach(function(done) {
@@ -175,38 +196,41 @@ describe('select box and lasso', function() {
175196
drag(selectPath);
176197

177198
expect(selectingCnt).toEqual(1, 'with the correct selecting count');
178-
expect(selectingData.points).toEqual([{
199+
assertEventData(selectingData.points, [{
179200
curveNumber: 0,
180201
pointNumber: 0,
181202
x: 0.002,
182203
y: 16.25,
183-
id: undefined
204+
id: 'id-0.002',
205+
customdata: 'customdata-16.25'
184206
}, {
185207
curveNumber: 0,
186208
pointNumber: 1,
187209
x: 0.004,
188210
y: 12.5,
189-
id: undefined
190-
}], 'with the correct selecting points');
211+
id: 'id-0.004',
212+
customdata: 'customdata-12.5'
213+
}], 'with the correct selecting points (1)');
191214
assertRange(selectingData.range, {
192215
x: [0.002000, 0.0046236],
193216
y: [0.10209191961595454, 24.512223978291406]
194217
}, 'with the correct selecting range');
195-
196218
expect(selectedCnt).toEqual(1, 'with the correct selected count');
197-
expect(selectedData.points).toEqual([{
219+
assertEventData(selectedData.points, [{
198220
curveNumber: 0,
199221
pointNumber: 0,
200222
x: 0.002,
201223
y: 16.25,
202-
id: undefined
224+
id: 'id-0.002',
225+
customdata: 'customdata-16.25'
203226
}, {
204227
curveNumber: 0,
205228
pointNumber: 1,
206229
x: 0.004,
207230
y: 12.5,
208-
id: undefined
209-
}], 'with the correct selected points');
231+
id: 'id-0.004',
232+
customdata: 'customdata-12.5'
233+
}], 'with the correct selected points (2)');
210234
assertRange(selectedData.range, {
211235
x: [0.002000, 0.0046236],
212236
y: [0.10209191961595454, 24.512223978291406]
@@ -255,22 +279,20 @@ describe('select box and lasso', function() {
255279
drag(lassoPath);
256280

257281
expect(selectingCnt).toEqual(3, 'with the correct selecting count');
258-
expect(selectingData.points).toEqual([{
282+
assertEventData(selectingData.points, [{
259283
curveNumber: 0,
260284
pointNumber: 10,
261285
x: 0.099,
262-
y: 2.75,
263-
id: undefined
264-
}], 'with the correct selecting points');
286+
y: 2.75
287+
}], 'with the correct selecting points (1)');
265288

266289
expect(selectedCnt).toEqual(1, 'with the correct selected count');
267-
expect(selectedData.points).toEqual([{
290+
assertEventData(selectedData.points, [{
268291
curveNumber: 0,
269292
pointNumber: 10,
270293
x: 0.099,
271294
y: 2.75,
272-
id: undefined
273-
}], 'with the correct selected points');
295+
}], 'with the correct selected points (2)');
274296

275297
doubleClick(250, 200).then(function() {
276298
expect(doubleClickData).toBe(null, 'with the correct deselect data');

0 commit comments

Comments
 (0)