Skip to content

Commit 6e6bb32

Browse files
committed
add lasso/select box to bar traces
- use coordinates of middle of bar in the pos axis and outmost span in the size axis
1 parent e7c7149 commit 6e6bb32

File tree

4 files changed

+140
-15
lines changed

4 files changed

+140
-15
lines changed

src/traces/bar/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Bar.arraysToCalcdata = require('./arrays_to_calcdata');
2222
Bar.plot = require('./plot');
2323
Bar.style = require('./style');
2424
Bar.hoverPoints = require('./hover');
25+
Bar.selectPoints = require('./select');
2526

2627
Bar.moduleType = 'trace';
2728
Bar.name = 'bar';

src/traces/bar/plot.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ module.exports = function plot(gd, plotinfo, cdbar) {
4545
bartraces.append('g')
4646
.attr('class', 'points')
4747
.each(function(d) {
48-
var t = d[0].t,
49-
trace = d[0].trace,
50-
poffset = t.poffset,
51-
poffsetIsArray = Array.isArray(poffset);
48+
var sel = d[0].node3 = d3.select(this);
49+
var t = d[0].t;
50+
var trace = d[0].trace;
51+
var poffset = t.poffset;
52+
var poffsetIsArray = Array.isArray(poffset);
5253

53-
d3.select(this).selectAll('g.point')
54+
sel.selectAll('g.point')
5455
.data(Lib.identity)
5556
.enter().append('g').classed('point', true)
5657
.each(function(di, i) {
@@ -69,12 +70,18 @@ module.exports = function plot(gd, plotinfo, cdbar) {
6970
y1 = ya.c2p(p1, true);
7071
x0 = xa.c2p(s0, true);
7172
x1 = xa.c2p(s1, true);
73+
74+
// for selections
75+
di.ct = [x1, (y0 + y1) / 2];
7276
}
7377
else {
7478
x0 = xa.c2p(p0, true);
7579
x1 = xa.c2p(p1, true);
7680
y0 = ya.c2p(s0, true);
7781
y1 = ya.c2p(s1, true);
82+
83+
// for selections
84+
di.ct = [(x0 + x1) / 2, y1];
7885
}
7986

8087
if(!isNumeric(x0) || !isNumeric(x1) ||

src/traces/bar/select.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
'use strict';
10+
11+
var DESELECTDIM = require('../../constants/interactions').DESELECTDIM;
12+
13+
module.exports = function selectPoints(searchInfo, polygon) {
14+
var cd = searchInfo.cd;
15+
var selection = [];
16+
var trace = cd[0].trace;
17+
var node3 = cd[0].node3;
18+
var i;
19+
20+
if(trace.visible !== true) return;
21+
22+
if(polygon === false) {
23+
// clear selection
24+
for(i = 0; i < cd.length; i++) {
25+
cd[i].dim = 0;
26+
}
27+
} else {
28+
for(i = 0; i < cd.length; i++) {
29+
var di = cd[i];
30+
31+
if(polygon.contains(di.ct)) {
32+
selection.push({
33+
pointNumber: i,
34+
x: di.x,
35+
y: di.y
36+
});
37+
di.dim = 0;
38+
} else {
39+
di.dim = 1;
40+
}
41+
}
42+
}
43+
44+
node3.selectAll('.point').style('opacity', function(d) {
45+
return d.dim ? DESELECTDIM : 1;
46+
});
47+
node3.selectAll('text').style('opacity', function(d) {
48+
return d.dim ? DESELECTDIM : 1;
49+
});
50+
51+
return selection;
52+
};

test/jasmine/tests/select_test.js

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,18 @@ describe('Test select box and lasso per trace:', function() {
428428
var callNumber = 0;
429429

430430
return function(expected) {
431-
var msg = '(call #' + callNumber + ') ';
432-
var ranges = (selectedData.range || {})[subplot] || [];
433-
434-
expect(ranges)
435-
.toBeCloseTo2DArray(expected, tol, msg + 'select box range for ' + subplot);
431+
var msg = '(call #' + callNumber + ') select box range ';
432+
var ranges = selectedData.range || {};
433+
434+
if(subplot) {
435+
expect(ranges[subplot] || [])
436+
.toBeCloseTo2DArray(expected, tol, msg + 'for ' + subplot);
437+
} else {
438+
expect(ranges.x || [])
439+
.toBeCloseToArray(expected[0], tol, msg + 'x coords');
440+
expect(ranges.y || [])
441+
.toBeCloseToArray(expected[1], tol, msg + 'y coords');
442+
}
436443

437444
callNumber++;
438445
};
@@ -443,11 +450,18 @@ describe('Test select box and lasso per trace:', function() {
443450
var callNumber = 0;
444451

445452
return function(expected) {
446-
var msg = '(call #' + callNumber + ') ';
447-
var lassoPoints = (selectedData.lassoPoints || {})[subplot] || [];
448-
449-
expect(lassoPoints)
450-
.toBeCloseTo2DArray(expected, tol, msg + 'lasso points for ' + subplot);
453+
var msg = '(call #' + callNumber + ') lasso points ';
454+
var lassoPoints = selectedData.lassoPoints || {};
455+
456+
if(subplot) {
457+
expect(lassoPoints[subplot] || [])
458+
.toBeCloseTo2DArray(expected, tol, msg + 'for ' + subplot);
459+
} else {
460+
expect(lassoPoints.x || [])
461+
.toBeCloseToArray(expected[0], tol, msg + 'x coords');
462+
expect(lassoPoints.y || [])
463+
.toBeCloseToArray(expected[1], tol, msg + 'y coords');
464+
}
451465

452466
callNumber++;
453467
};
@@ -708,4 +722,55 @@ describe('Test select box and lasso per trace:', function() {
708722
.catch(fail)
709723
.then(done);
710724
}, LONG_TIMEOUT_INTERVAL);
725+
726+
it('should work for bar traces', function(done) {
727+
var assertPoints = makeAssertPoints(['curveNumber', 'x', 'y']);
728+
var assertRanges = makeAssertRanges();
729+
var assertLassoPoints = makeAssertLassoPoints();
730+
731+
var fig = Lib.extendDeep({}, require('@mocks/0'));
732+
fig.layout.dragmode = 'lasso';
733+
734+
Plotly.plot(gd, fig)
735+
.then(function() {
736+
return _run(
737+
[[350, 200], [400, 200], [400, 250], [350, 250], [350, 200]],
738+
function() {
739+
assertPoints([
740+
[0, 4.9, 0.371], [0, 5, 0.368], [0, 5.1, 0.356], [0, 5.2, 0.336],
741+
[0, 5.3, 0.309], [0, 5.4, 0.275], [0, 5.5, 0.235], [0, 5.6, 0.192],
742+
[0, 5.7, 0.145],
743+
[1, 5.1, 0.485], [1, 5.2, 0.40900000000000003], [1, 5.3, 0.327],
744+
[1, 5.4, 0.24000000000000002], [1, 5.5, 0.149], [1, 5.6, 0.059],
745+
[2, 4.9, 0.473], [2, 5, 0.36800000000000005], [2, 5.1, 0.258],
746+
[2, 5.2, 0.14600000000000002], [2, 5.3, 0.03600000000000003]
747+
]);
748+
assertLassoPoints([
749+
[4.87, 5.74, 5.74, 4.87, 4.87],
750+
[0.53, 0.53, -0.02, -0.02, 0.53]
751+
]);
752+
},
753+
null, LASSOEVENTS, 'bar lasso'
754+
);
755+
})
756+
.then(function() {
757+
return Plotly.relayout(gd, 'dragmode', 'select');
758+
})
759+
.then(function() {
760+
return _run(
761+
[[350, 200], [370, 220]],
762+
function() {
763+
assertPoints([
764+
[0, 4.9, 0.371], [0, 5, 0.368], [0, 5.1, 0.356], [0, 5.2, 0.336],
765+
[1, 5.1, 0.485], [1, 5.2, 0.40900000000000003],
766+
[2, 4.9, 0.473], [2, 5, 0.36800000000000005]
767+
]);
768+
assertRanges([[4.87, 5.22], [0.31, 0.53]]);
769+
},
770+
null, BOXEVENTS, 'bar select'
771+
);
772+
})
773+
.catch(fail)
774+
.then(done);
775+
});
711776
});

0 commit comments

Comments
 (0)