Skip to content

Commit

Permalink
fill selection event data in data, fullData and array values
Browse files Browse the repository at this point in the history
- to bring its point data on-par with hover/click/unhover
  • Loading branch information
etpinard committed Jun 14, 2017
1 parent d9373aa commit 3399a58
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
22 changes: 21 additions & 1 deletion src/plots/cartesian/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var polygon = require('../../lib/polygon');
var color = require('../../components/color');
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;

var axes = require('./axes');
var constants = require('./constants');
Expand Down Expand Up @@ -151,7 +152,9 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
selection = [];
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
[].push.apply(selection, searchInfo.selectPoints(searchInfo, poly));
[].push.apply(selection, fillSelectionItem(
searchInfo.selectPoints(searchInfo, poly), searchInfo
));
}

eventData = {points: selection};
Expand Down Expand Up @@ -196,3 +199,20 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
}
};
};

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;
}
6 changes: 1 addition & 5 deletions src/traces/scatter/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = function selectPoints(searchInfo, polygon) {
ya = searchInfo.yaxis,
selection = [],
trace = cd[0].trace,
curveNumber = trace.index,
marker = trace.marker,
i,
di,
Expand All @@ -43,12 +42,9 @@ module.exports = function selectPoints(searchInfo, polygon) {

if(polygon.contains([x, y])) {
selection.push({
curveNumber: curveNumber,
pointNumber: i,
x: di.x,
y: di.y,
// TODO generalize with hover/click data handler
id: di.id
y: di.y
});
di.dim = 0;
}
Expand Down
54 changes: 38 additions & 16 deletions test/jasmine/tests/select_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ describe('select box and lasso', function() {
expect(actual.y).toBeCloseToArray(expected.y, PRECISION);
}

function assertEventData(actual, expected, msg) {
expect(actual.length).toBe(expected.length, msg + ' same number of pts');

expected.forEach(function(e, i) {
var a = actual[i];
var m = msg + ' (pt ' + i + ')';

expect(a.data).toBeDefined(m + ' has data ref');
expect(a.fullData).toBeDefined(m + ' has fullData ref');
expect(Object.keys(a).length - 2).toBe(Object.keys(e).length, m + ' has correct number of keys');

Object.keys(e).forEach(function(k) {
expect(a[k]).toBe(e[k], m + ' ' + k);
});
});
}

describe('select elements', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.layout.dragmode = 'select';
Expand Down Expand Up @@ -143,6 +160,10 @@ describe('select box and lasso', function() {
describe('select events', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.layout.dragmode = 'select';
mockCopy.data[0].ids = mockCopy.data[0].x
.map(function(v) { return 'id-' + v; });
mockCopy.data[0].customdata = mockCopy.data[0].y
.map(function(v) { return 'customdata-' + v; });

var gd;
beforeEach(function(done) {
Expand Down Expand Up @@ -175,38 +196,41 @@ describe('select box and lasso', function() {
drag(selectPath);

expect(selectingCnt).toEqual(1, 'with the correct selecting count');
expect(selectingData.points).toEqual([{
assertEventData(selectingData.points, [{
curveNumber: 0,
pointNumber: 0,
x: 0.002,
y: 16.25,
id: undefined
id: 'id-0.002',
customdata: 'customdata-16.25'
}, {
curveNumber: 0,
pointNumber: 1,
x: 0.004,
y: 12.5,
id: undefined
}], 'with the correct selecting points');
id: 'id-0.004',
customdata: 'customdata-12.5'
}], 'with the correct selecting points (1)');
assertRange(selectingData.range, {
x: [0.002000, 0.0046236],
y: [0.10209191961595454, 24.512223978291406]
}, 'with the correct selecting range');

expect(selectedCnt).toEqual(1, 'with the correct selected count');
expect(selectedData.points).toEqual([{
assertEventData(selectedData.points, [{
curveNumber: 0,
pointNumber: 0,
x: 0.002,
y: 16.25,
id: undefined
id: 'id-0.002',
customdata: 'customdata-16.25'
}, {
curveNumber: 0,
pointNumber: 1,
x: 0.004,
y: 12.5,
id: undefined
}], 'with the correct selected points');
id: 'id-0.004',
customdata: 'customdata-12.5'
}], 'with the correct selected points (2)');
assertRange(selectedData.range, {
x: [0.002000, 0.0046236],
y: [0.10209191961595454, 24.512223978291406]
Expand Down Expand Up @@ -255,22 +279,20 @@ describe('select box and lasso', function() {
drag(lassoPath);

expect(selectingCnt).toEqual(3, 'with the correct selecting count');
expect(selectingData.points).toEqual([{
assertEventData(selectingData.points, [{
curveNumber: 0,
pointNumber: 10,
x: 0.099,
y: 2.75,
id: undefined
}], 'with the correct selecting points');
y: 2.75
}], 'with the correct selecting points (1)');

expect(selectedCnt).toEqual(1, 'with the correct selected count');
expect(selectedData.points).toEqual([{
assertEventData(selectedData.points, [{
curveNumber: 0,
pointNumber: 10,
x: 0.099,
y: 2.75,
id: undefined
}], 'with the correct selected points');
}], 'with the correct selected points (2)');

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

0 comments on commit 3399a58

Please sign in to comment.