Skip to content

Commit 470125c

Browse files
authoredApr 27, 2023
Merge pull request #6567 from plotly/fix-6508
Fix: visible = legendonly in scattermapbox worked wrong
2 parents 482bb8e + 36ef0ba commit 470125c

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed
 

‎draftlogs/6567_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix scattermapbox visibility restyle [[#6567](https://github.com/plotly/plotly.js/pull/6567)]

‎src/traces/scattermapbox/plot.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ proto.addSource = function(k, opts, cluster) {
5656
clusterMaxZoom: cluster.maxzoom,
5757
});
5858
}
59-
60-
this.subplot.map.addSource(this.sourceIds[k], sourceOpts);
59+
var isSourceExists = this.subplot.map.getSource(this.sourceIds[k]);
60+
if(isSourceExists) {
61+
isSourceExists.setData(opts.geojson);
62+
} else {
63+
this.subplot.map.addSource(this.sourceIds[k], sourceOpts);
64+
}
6165
};
6266

6367
proto.setSourceData = function(k, opts) {
@@ -77,7 +81,24 @@ proto.addLayer = function(k, opts, below) {
7781
if(opts.filter) {
7882
source.filter = opts.filter;
7983
}
80-
this.subplot.addLayer(source, below);
84+
var currentLayerId = this.layerIds[k];
85+
var layerExist;
86+
var layers = this.subplot.getMapLayers();
87+
for(var i = 0; i < layers.length; i++) {
88+
if(layers[i].id === currentLayerId) {
89+
layerExist = true;
90+
break;
91+
}
92+
}
93+
94+
if(layerExist) {
95+
this.subplot.setOptions(currentLayerId, 'setLayoutProperty', source.layout);
96+
if(source.layout.visibility === 'visible') {
97+
this.subplot.setOptions(currentLayerId, 'setPaintProperty', source.paint);
98+
}
99+
} else {
100+
this.subplot.addLayer(source, below);
101+
}
81102
};
82103

83104
proto.update = function update(calcTrace) {

‎test/jasmine/tests/scattermapbox_test.js

+44
Original file line numberDiff line numberDiff line change
@@ -1253,3 +1253,47 @@ describe('Test plotly events on a scattermapbox plot when css transform is prese
12531253
});
12541254
});
12551255
});
1256+
1257+
describe('scattermapbox restyle', function() {
1258+
var gd;
1259+
1260+
beforeAll(function() {
1261+
Plotly.setPlotConfig({
1262+
mapboxAccessToken: require('../../../build/credentials.json').MAPBOX_ACCESS_TOKEN
1263+
});
1264+
1265+
gd = createGraphDiv();
1266+
});
1267+
1268+
afterAll(function() {
1269+
Plotly.purge(gd);
1270+
destroyGraphDiv();
1271+
});
1272+
1273+
it('@gl should be able to update legendonly to visible', function(done) {
1274+
Plotly.newPlot(gd, {
1275+
data: [{
1276+
lat: [0, 2], lon: [0, 2],
1277+
type: 'scattermapbox',
1278+
mode: 'lines',
1279+
visible: 'legendonly'
1280+
},
1281+
{
1282+
lat: [0, 2], lon: [2, 0],
1283+
type: 'scattermapbox',
1284+
mode: 'lines',
1285+
visible: true
1286+
}
1287+
], layout: {
1288+
mapbox: {
1289+
style: 'open-street-map',
1290+
zoom: 6,
1291+
center: { lat: 1, lon: 1 }
1292+
},
1293+
showlegend: true
1294+
}
1295+
}).then(function() {
1296+
return Plotly.restyle(gd, 'visible', true);
1297+
}).then(done, done.fail);
1298+
});
1299+
});

0 commit comments

Comments
 (0)
Please sign in to comment.