Skip to content

Fix: visible = legendonly in scattermapbox worked wrong #6567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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) {
@@ -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) {
44 changes: 44 additions & 0 deletions test/jasmine/tests/scattermapbox_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});