Skip to content

Commit

Permalink
Merge pull request #6567 from plotly/fix-6508
Browse files Browse the repository at this point in the history
Fix: visible = legendonly in scattermapbox worked wrong
  • Loading branch information
archmoj authored Apr 27, 2023
2 parents 482bb8e + 36ef0ba commit 470125c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions draftlogs/6567_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix scattermapbox visibility restyle [[#6567](https://github.com/plotly/plotly.js/pull/6567)]
27 changes: 24 additions & 3 deletions src/traces/scattermapbox/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ proto.addSource = function(k, opts, cluster) {
clusterMaxZoom: cluster.maxzoom,
});
}

this.subplot.map.addSource(this.sourceIds[k], sourceOpts);
var isSourceExists = this.subplot.map.getSource(this.sourceIds[k]);
if(isSourceExists) {
isSourceExists.setData(opts.geojson);
} else {
this.subplot.map.addSource(this.sourceIds[k], sourceOpts);
}
};

proto.setSourceData = function(k, opts) {
Expand All @@ -77,7 +81,24 @@ proto.addLayer = function(k, opts, below) {
if(opts.filter) {
source.filter = opts.filter;
}
this.subplot.addLayer(source, below);
var currentLayerId = this.layerIds[k];
var layerExist;
var layers = this.subplot.getMapLayers();
for(var i = 0; i < layers.length; i++) {
if(layers[i].id === currentLayerId) {
layerExist = true;
break;
}
}

if(layerExist) {
this.subplot.setOptions(currentLayerId, 'setLayoutProperty', source.layout);
if(source.layout.visibility === 'visible') {
this.subplot.setOptions(currentLayerId, 'setPaintProperty', source.paint);
}
} else {
this.subplot.addLayer(source, below);
}
};

proto.update = function update(calcTrace) {
Expand Down
44 changes: 44 additions & 0 deletions test/jasmine/tests/scattermapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1253,3 +1253,47 @@ describe('Test plotly events on a scattermapbox plot when css transform is prese
});
});
});

describe('scattermapbox restyle', function() {
var gd;

beforeAll(function() {
Plotly.setPlotConfig({
mapboxAccessToken: require('../../../build/credentials.json').MAPBOX_ACCESS_TOKEN
});

gd = createGraphDiv();
});

afterAll(function() {
Plotly.purge(gd);
destroyGraphDiv();
});

it('@gl should be able to update legendonly to visible', function(done) {
Plotly.newPlot(gd, {
data: [{
lat: [0, 2], lon: [0, 2],
type: 'scattermapbox',
mode: 'lines',
visible: 'legendonly'
},
{
lat: [0, 2], lon: [2, 0],
type: 'scattermapbox',
mode: 'lines',
visible: true
}
], layout: {
mapbox: {
style: 'open-street-map',
zoom: 6,
center: { lat: 1, lon: 1 }
},
showlegend: true
}
}).then(function() {
return Plotly.restyle(gd, 'visible', true);
}).then(done, done.fail);
});
});

0 comments on commit 470125c

Please sign in to comment.