Skip to content

Commit 8e4cdc7

Browse files
committed
Merge pull request plotly#245 from plotly/fix-geo-toggle
Bring back geo trace toggle [fixes plotly#244]
2 parents 9f73cfd + c70aa94 commit 8e4cdc7

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

src/plots/geo/geo.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ var d3 = require('d3');
1515

1616
var Color = require('../../components/color');
1717
var Drawing = require('../../components/drawing');
18-
19-
var Plots = require('../../plots/plots');
2018
var Axes = require('../../plots/cartesian/axes');
2119

2220
var addProjectionsToD3 = require('./projections');
@@ -139,6 +137,20 @@ proto.plot = function(geoData, fullLayout, promises) {
139137
// to avoid making multiple request while streaming
140138
};
141139

140+
// filter out non-visible trace
141+
// geo plot routine use the classic join/enter/exit pattern to update traces
142+
function filterData(dataIn) {
143+
var dataOut = [];
144+
145+
for(var i = 0; i < dataIn.length; i++) {
146+
var trace = dataIn[i];
147+
148+
if(trace.visible === true) dataOut.push(trace);
149+
}
150+
151+
return dataOut;
152+
}
153+
142154
proto.onceTopojsonIsLoaded = function(geoData, geoLayout) {
143155
var traceData = {};
144156

@@ -153,8 +165,10 @@ proto.onceTopojsonIsLoaded = function(geoData, geoLayout) {
153165

154166
var traceKeys = Object.keys(traceData);
155167
for(var j = 0; j < traceKeys.length; j++){
156-
var traceKey = traceKeys[j];
157-
Plots.getModule(traceKey).plot(this, traceData[traceKey], geoLayout);
168+
var moduleData = traceData[traceKeys[j]];
169+
var _module = moduleData[0]._module;
170+
171+
_module.plot(this, filterData(moduleData), geoLayout);
158172
}
159173

160174
this.render();

src/traces/choropleth/plot.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
6868

6969
var gChoroplethTraces = gChoropleth
7070
.selectAll('g.trace.choropleth')
71-
.data(choroplethData);
71+
.data(choroplethData, function(trace) { return trace.uid; });
7272

7373
gChoroplethTraces.enter().append('g')
7474
.attr('class', 'trace choropleth');
@@ -77,8 +77,6 @@ plotChoropleth.plot = function(geo, choroplethData, geoLayout) {
7777

7878
gChoroplethTraces
7979
.each(function(trace) {
80-
if(trace.visible !== true) return;
81-
8280
var cdi = plotChoropleth.calcGeoJSON(trace, geo.topojson),
8381
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace),
8482
eventDataFunc = makeEventDataFunc(trace);

src/traces/scattergeo/plot.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ plotScatterGeo.calcGeoJSON = function(trace, topojson) {
3131
var cdi = [],
3232
hasLocationData = Array.isArray(trace.locations);
3333

34-
var len, features, getLonLat, lonlat, locations, calcItem;
34+
var len, features, getLonLat, locations;
3535

3636
if(hasLocationData) {
3737
locations = trace.locations;
@@ -53,11 +53,11 @@ plotScatterGeo.calcGeoJSON = function(trace, topojson) {
5353
}
5454

5555
for(var i = 0; i < len; i++) {
56-
lonlat = getLonLat(trace, i);
56+
var lonlat = getLonLat(trace, i);
5757

5858
if(!lonlat) continue; // filter the blank points here
5959

60-
calcItem = {
60+
var calcItem = {
6161
lon: lonlat[0],
6262
lat: lonlat[1],
6363
location: hasLocationData ? trace.locations[i] : null
@@ -104,7 +104,7 @@ function makeLineGeoJSON(trace) {
104104
var N = trace.lon.length,
105105
coordinates = new Array(N);
106106

107-
for (var i = 0; i < N; i++) {
107+
for(var i = 0; i < N; i++) {
108108
coordinates[i] = [trace.lon[i], trace.lat[i]];
109109
}
110110

@@ -118,7 +118,7 @@ function makeLineGeoJSON(trace) {
118118
plotScatterGeo.plot = function(geo, scattergeoData) {
119119
var gScatterGeoTraces = geo.framework.select('.scattergeolayer')
120120
.selectAll('g.trace.scattergeo')
121-
.data(scattergeoData);
121+
.data(scattergeoData, function(trace) { return trace.uid; });
122122

123123
gScatterGeoTraces.enter().append('g')
124124
.attr('class', 'trace scattergeo');
@@ -128,7 +128,8 @@ plotScatterGeo.plot = function(geo, scattergeoData) {
128128
// TODO add hover - how?
129129
gScatterGeoTraces
130130
.each(function(trace) {
131-
if(!subTypes.hasLines(trace) || trace.visible !== true) return;
131+
if(!subTypes.hasLines(trace)) return;
132+
132133
d3.select(this)
133134
.append('path')
134135
.datum(makeLineGeoJSON(trace))
@@ -142,10 +143,7 @@ plotScatterGeo.plot = function(geo, scattergeoData) {
142143
showMarkers = subTypes.hasMarkers(trace),
143144
showText = subTypes.hasText(trace);
144145

145-
if((!showMarkers && !showText) || trace.visible !== true) {
146-
s.remove();
147-
return;
148-
}
146+
if((!showMarkers && !showText)) return;
149147

150148
var cdi = plotScatterGeo.calcGeoJSON(trace, geo.topojson),
151149
cleanHoverLabelsFunc = makeCleanHoverLabelsFunc(geo, trace),

test/jasmine/tests/geo_interact_test.js

+38
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,43 @@ describe('Test geo interactions', function() {
180180
});
181181
});
182182

183+
describe('trace visibility toggle', function() {
184+
function countTraces(type) {
185+
return d3.selectAll('g.trace.' + type).size();
186+
}
187+
188+
it('should toggle scattergeo elements', function(done) {
189+
expect(countTraces('scattergeo')).toBe(1);
190+
expect(countTraces('choropleth')).toBe(1);
191+
192+
Plotly.restyle(gd, 'visible', false, [0]).then(function() {
193+
expect(countTraces('scattergeo')).toBe(0);
194+
expect(countTraces('choropleth')).toBe(1);
195+
196+
Plotly.restyle(gd, 'visible', true, [0]).then(function() {
197+
expect(countTraces('scattergeo')).toBe(1);
198+
expect(countTraces('choropleth')).toBe(1);
199+
done();
200+
});
201+
});
202+
});
203+
204+
it('should toggle choropleth elements', function(done) {
205+
expect(countTraces('scattergeo')).toBe(1);
206+
expect(countTraces('choropleth')).toBe(1);
207+
208+
Plotly.restyle(gd, 'visible', false, [1]).then(function() {
209+
expect(countTraces('scattergeo')).toBe(1);
210+
expect(countTraces('choropleth')).toBe(0);
211+
212+
Plotly.restyle(gd, 'visible', true, [1]).then(function() {
213+
expect(countTraces('scattergeo')).toBe(1);
214+
expect(countTraces('choropleth')).toBe(1);
215+
done();
216+
});
217+
});
218+
});
219+
220+
});
183221
});
184222
});

0 commit comments

Comments
 (0)