Skip to content

Commit 2ea6486

Browse files
committed
add choropleth lasso/select
- with test support. - 'simply' use the location's centroid to determine if it is selected or not.
1 parent 3c327e6 commit 2ea6486

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/traces/choropleth/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Choropleth.calc = require('./calc');
1818
Choropleth.plot = require('./plot');
1919
Choropleth.hoverPoints = require('./hover');
2020
Choropleth.eventData = require('./event_data');
21+
Choropleth.selectPoints = require('./select');
2122

2223
Choropleth.moduleType = 'trace';
2324
Choropleth.name = 'choropleth';

src/traces/choropleth/select.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 xa = searchInfo.xaxis;
16+
var ya = searchInfo.yaxis;
17+
var selection = [];
18+
var node3 = cd[0].node3;
19+
20+
var i, di, ct, x, y;
21+
22+
if(polygon === false) {
23+
for(i = 0; i < cd.length; i++) {
24+
cd[i].dim = 0;
25+
}
26+
} else {
27+
for(i = 0; i < cd.length; i++) {
28+
di = cd[i];
29+
ct = di.ct;
30+
31+
if(!ct) continue;
32+
33+
x = xa.c2p(ct);
34+
y = ya.c2p(ct);
35+
36+
if(polygon.contains([x, y])) {
37+
selection.push({
38+
pointNumber: i,
39+
lon: ct[0],
40+
lat: ct[1]
41+
});
42+
di.dim = 0;
43+
} else {
44+
di.dim = 1;
45+
}
46+
}
47+
}
48+
49+
node3.selectAll('path').style('opacity', function(d) {
50+
return d.dim ? DESELECTDIM : 1;
51+
});
52+
53+
return selection;
54+
};

test/jasmine/tests/select_test.js

+41
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,45 @@ describe('Test select box and lasso per trace:', function() {
703703
.then(done);
704704
}, LONG_TIMEOUT_INTERVAL);
705705

706+
it('should work on choropleth traces', function(done) {
707+
gd = createGraphDiv();
708+
709+
var assertPoints = makeAssertPoints(['location', 'z']);
710+
var assertRanges = makeAssertRanges('geo', -0.5);
711+
var assertLassoPoints = makeAssertLassoPoints('geo', -0.5);
712+
713+
var fig = Lib.extendDeep({}, require('@mocks/geo_choropleth-text'));
714+
fig.layout.width = 870;
715+
fig.layout.height = 450;
716+
fig.layout.dragmode = 'select';
717+
fig.layout.geo.scope = 'europe';
718+
719+
Plotly.plot(gd, fig)
720+
.then(function() {
721+
return _run([[350, 200], [400, 250]], function() {
722+
assertPoints([['GBR', 26.507354205352502], ['IRL', 86.4125147625692]]);
723+
assertRanges([[-19.11, 63.06], [7.31, 53.72]]);
724+
}, [280, 190]);
725+
})
726+
.then(function() {
727+
return Plotly.relayout(gd, 'dragmode', 'lasso');
728+
})
729+
.then(function() {
730+
return _run([[350, 200], [400, 200], [400, 250], [350, 250], [350, 200]], function() {
731+
assertPoints([['GBR', 26.507354205352502], ['IRL', 86.4125147625692]]);
732+
assertLassoPoints([
733+
[-19.11, 63.06], [5.50, 65.25], [7.31, 53.72], [-12.90, 51.70], [-19.11, 63.06]
734+
]);
735+
}, [280, 190]);
736+
})
737+
.then(function() {
738+
// make selection handlers don't get called in 'pan' dragmode
739+
return Plotly.relayout(gd, 'dragmode', 'pan');
740+
})
741+
.then(function() {
742+
return _runNoSelect([[370, 120], [500, 200]]);
743+
})
744+
.catch(fail)
745+
.then(done);
746+
}, LONG_TIMEOUT_INTERVAL);
706747
});

0 commit comments

Comments
 (0)