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

Skip to content

Commit eeace5b

Browse files
committed
persistent selections for 'choropleth'
- in which a new style per-trace-only style() method is added (similar to 'scattergeo') AND a new attribute: 'marker.opacity' to make selection behavior be attribute-describable.
1 parent 10c5b7b commit eeace5b

File tree

7 files changed

+102
-49
lines changed

7 files changed

+102
-49
lines changed

src/traces/choropleth/attributes.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var ScatterGeoAttrs = require('../scattergeo/attributes');
11+
var scatterGeoAttrs = require('../scattergeo/attributes');
1212
var colorscaleAttrs = require('../../components/colorscale/attributes');
1313
var colorbarAttrs = require('../../components/colorbar/attributes');
1414
var plotAttrs = require('../../plots/attributes');
@@ -17,7 +17,7 @@ var extend = require('../../lib/extend');
1717
var extendFlat = extend.extendFlat;
1818
var extendDeepAll = extend.extendDeepAll;
1919

20-
var ScatterGeoMarkerLineAttrs = ScatterGeoAttrs.marker.line;
20+
var scatterGeoMarkerLineAttrs = scatterGeoAttrs.marker.line;
2121

2222
module.exports = extendFlat({
2323
locations: {
@@ -28,23 +28,49 @@ module.exports = extendFlat({
2828
'See `locationmode` for more info.'
2929
].join(' ')
3030
},
31-
locationmode: ScatterGeoAttrs.locationmode,
31+
locationmode: scatterGeoAttrs.locationmode,
3232
z: {
3333
valType: 'data_array',
3434
editType: 'calc',
3535
description: 'Sets the color values.'
3636
},
37-
text: extendFlat({}, ScatterGeoAttrs.text, {
37+
text: extendFlat({}, scatterGeoAttrs.text, {
3838
description: 'Sets the text elements associated with each location.'
3939
}),
4040
marker: {
4141
line: {
42-
color: ScatterGeoMarkerLineAttrs.color,
43-
width: extendFlat({}, ScatterGeoMarkerLineAttrs.width, {dflt: 1}),
42+
color: scatterGeoMarkerLineAttrs.color,
43+
width: extendFlat({}, scatterGeoMarkerLineAttrs.width, {dflt: 1}),
4444
editType: 'calc'
4545
},
46+
opacity: {
47+
valType: 'number',
48+
arrayOk: true,
49+
min: 0,
50+
max: 1,
51+
dflt: 1,
52+
role: 'style',
53+
editType: 'style',
54+
description: 'Sets the opacity of the locations.'
55+
},
4656
editType: 'calc'
4757
},
58+
59+
selected: {
60+
marker: {
61+
opacity: scatterGeoAttrs.selected.marker.opacity,
62+
editType: 'plot'
63+
},
64+
editType: 'plot'
65+
},
66+
unselected: {
67+
marker: {
68+
opacity: scatterGeoAttrs.unselected.marker.opacity,
69+
editType: 'plot'
70+
},
71+
editType: 'plot'
72+
},
73+
4874
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
4975
editType: 'calc',
5076
flags: ['location', 'z', 'text', 'name']

src/traces/choropleth/calc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var BADNUM = require('../../constants/numerical').BADNUM;
1414

1515
var colorscaleCalc = require('../../components/colorscale/calc');
1616
var arraysToCalcdata = require('../scatter/arrays_to_calcdata');
17+
var calcSelection = require('../scatter/calc_selection');
1718

1819
module.exports = function calc(gd, trace) {
1920
var len = trace.locations.length;
@@ -30,6 +31,7 @@ module.exports = function calc(gd, trace) {
3031

3132
arraysToCalcdata(calcTrace, trace);
3233
colorscaleCalc(trace, trace.z, '', 'z');
34+
calcSelection(calcTrace, trace);
3335

3436
return calcTrace;
3537
};

src/traces/choropleth/defaults.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13-
1413
var colorscaleDefaults = require('../../components/colorscale/defaults');
1514
var attributes = require('./attributes');
16-
15+
var DESELECTDIM = require('../../constants/interactions').DESELECTDIM;
1716

1817
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
1918
function coerce(attr, dflt) {
@@ -45,6 +44,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4544
coerce('marker.line.color');
4645
coerce('marker.line.width');
4746

47+
var mo = coerce('marker.opacity');
48+
var moEffective = Array.isArray(mo) ? 1 : mo;
49+
50+
coerce('selected.marker.opacity', moEffective);
51+
coerce('unselected.marker.opacity', DESELECTDIM * moEffective);
52+
4853
colorscaleDefaults(
4954
traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'}
5055
);

src/traces/choropleth/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Choropleth.supplyDefaults = require('./defaults');
1616
Choropleth.colorbar = require('../heatmap/colorbar');
1717
Choropleth.calc = require('./calc');
1818
Choropleth.plot = require('./plot');
19+
Choropleth.style = require('./style');
1920
Choropleth.hoverPoints = require('./hover');
2021
Choropleth.eventData = require('./event_data');
2122
Choropleth.selectPoints = require('./select');

src/traces/choropleth/plot.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var d3 = require('d3');
1312

1413
var Lib = require('../../lib');
15-
var Color = require('../../components/color');
16-
var Drawing = require('../../components/drawing');
17-
var Colorscale = require('../../components/colorscale');
1814
var polygon = require('../../lib/polygon');
1915

2016
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures;
2117
var locationToFeature = require('../../lib/geo_location_utils').locationToFeature;
18+
var style = require('./style');
2219

2320
module.exports = function plot(geo, calcData) {
2421
for(var i = 0; i < calcData.length; i++) {
@@ -46,9 +43,10 @@ module.exports = function plot(geo, calcData) {
4643
.classed('choroplethlocation', true);
4744

4845
paths.exit().remove();
49-
});
5046

51-
style(geo);
47+
// call style here within topojson request callback
48+
style(geo.graphDiv, calcTrace);
49+
});
5250
};
5351

5452
function calcGeoJSON(calcTrace, topojson) {
@@ -176,28 +174,3 @@ function feature2polygons(feature) {
176174

177175
return polygons;
178176
}
179-
180-
function style(geo) {
181-
var gTraces = geo.layers.backplot.selectAll('.trace.choropleth');
182-
183-
gTraces.each(function(calcTrace) {
184-
var trace = calcTrace[0].trace;
185-
var marker = trace.marker || {};
186-
var markerLine = marker.line || {};
187-
188-
var sclFunc = Colorscale.makeColorScaleFunc(
189-
Colorscale.extractScale(
190-
trace.colorscale,
191-
trace.zmin,
192-
trace.zmax
193-
)
194-
);
195-
196-
d3.select(this).selectAll('.choroplethlocation').each(function(d) {
197-
d3.select(this)
198-
.attr('fill', sclFunc(d.z))
199-
.call(Color.stroke, d.mlc || markerLine.color)
200-
.call(Drawing.dashLine, '', d.mlw || markerLine.width || 0);
201-
});
202-
});
203-
}

src/traces/choropleth/select.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@
88

99
'use strict';
1010

11-
var DESELECTDIM = require('../../constants/interactions').DESELECTDIM;
12-
1311
module.exports = function selectPoints(searchInfo, polygon) {
1412
var cd = searchInfo.cd;
1513
var xa = searchInfo.xaxis;
1614
var ya = searchInfo.yaxis;
1715
var selection = [];
18-
var node3 = cd[0].node3;
1916

2017
var i, di, ct, x, y;
2118

2219
if(polygon === false) {
2320
for(i = 0; i < cd.length; i++) {
24-
cd[i].dim = 0;
21+
cd[i].selected = 0;
2522
}
2623
} else {
2724
for(i = 0; i < cd.length; i++) {
@@ -39,16 +36,12 @@ module.exports = function selectPoints(searchInfo, polygon) {
3936
lon: ct[0],
4037
lat: ct[1]
4138
});
42-
di.dim = 0;
39+
di.selected = 1;
4340
} else {
44-
di.dim = 1;
41+
di.selected = 0;
4542
}
4643
}
4744
}
4845

49-
node3.selectAll('path').style('opacity', function(d) {
50-
return d.dim ? DESELECTDIM : 1;
51-
});
52-
5346
return selection;
5447
};

src/traces/choropleth/style.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 d3 = require('d3');
12+
var Color = require('../../components/color');
13+
var Drawing = require('../../components/drawing');
14+
var Colorscale = require('../../components/colorscale');
15+
16+
module.exports = function style(gd, calcTrace) {
17+
if(calcTrace) styleTrace(gd, calcTrace);
18+
};
19+
20+
function styleTrace(gd, calcTrace) {
21+
var trace = calcTrace[0].trace;
22+
var s = calcTrace[0].node3;
23+
var locs = s.selectAll('.choroplethlocation');
24+
var marker = trace.marker || {};
25+
var markerLine = marker.line || {};
26+
27+
var sclFunc = Colorscale.makeColorScaleFunc(
28+
Colorscale.extractScale(
29+
trace.colorscale,
30+
trace.zmin,
31+
trace.zmax
32+
)
33+
);
34+
35+
locs.each(function(d) {
36+
d3.select(this)
37+
.attr('fill', sclFunc(d.z))
38+
.call(Color.stroke, d.mlc || markerLine.color)
39+
.call(Drawing.dashLine, '', d.mlw || markerLine.width || 0)
40+
.style('opacity', marker.opacity);
41+
});
42+
43+
if(trace.selectedpoints) {
44+
var selectedAttrs = trace.selected || {};
45+
var unselectedAttrs = trace.unselected || {};
46+
47+
locs.style('opacity', function(d) {
48+
return d.selected ?
49+
selectedAttrs.marker.opacity :
50+
unselectedAttrs.marker.opacity;
51+
});
52+
}
53+
}

0 commit comments

Comments
 (0)