From dbf61a35a9f473408707973f252b736e68b8d7a1 Mon Sep 17 00:00:00 2001 From: NickMelnikov Date: Fri, 14 Apr 2023 18:28:45 +0600 Subject: [PATCH 1/7] fixed issue 6508 of plotly.js. visible=legendonly in scattermapbox worked wrong --- src/traces/scattermapbox/plot.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/traces/scattermapbox/plot.js b/src/traces/scattermapbox/plot.js index 03db06144fd..8660c3a1c1e 100644 --- a/src/traces/scattermapbox/plot.js +++ b/src/traces/scattermapbox/plot.js @@ -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,6 +81,13 @@ proto.addLayer = function(k, opts, below) { if(opts.filter) { source.filter = opts.filter; } + var layerId = this.layerIds[k]; + var layerExist = this.subplot.getMapLayers().filter(function(mbLayer) { + return mbLayer.id === layerId; + }) + if (layerExist.length) { + this.subplot.map.removeLayer(layerId); + } this.subplot.addLayer(source, below); }; From 6c33613c643a9a4a8d48d1f76c5ce35833f2ac01 Mon Sep 17 00:00:00 2001 From: NickMelnikov Date: Fri, 14 Apr 2023 19:11:44 +0600 Subject: [PATCH 2/7] fixed issue 6508 of plotly.js. fix lint. --- src/traces/scattermapbox/plot.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/traces/scattermapbox/plot.js b/src/traces/scattermapbox/plot.js index 8660c3a1c1e..d7860b8bcab 100644 --- a/src/traces/scattermapbox/plot.js +++ b/src/traces/scattermapbox/plot.js @@ -57,7 +57,7 @@ proto.addSource = function(k, opts, cluster) { }); } var isSourceExists = this.subplot.map.getSource(this.sourceIds[k]); - if (!!isSourceExists) { + if(isSourceExists) { isSourceExists.setData(opts.geojson); } else { this.subplot.map.addSource(this.sourceIds[k], sourceOpts); @@ -84,8 +84,8 @@ proto.addLayer = function(k, opts, below) { var layerId = this.layerIds[k]; var layerExist = this.subplot.getMapLayers().filter(function(mbLayer) { return mbLayer.id === layerId; - }) - if (layerExist.length) { + }); + if(layerExist.length) { this.subplot.map.removeLayer(layerId); } this.subplot.addLayer(source, below); From e0ab8fdf7554ac76aad1366df1c25b29f92297c2 Mon Sep 17 00:00:00 2001 From: NickMelnikov Date: Fri, 14 Apr 2023 20:39:02 +0600 Subject: [PATCH 3/7] fixed issue 6508 of plotly.js. renamed variable currentLayerId. --- src/traces/scattermapbox/plot.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/traces/scattermapbox/plot.js b/src/traces/scattermapbox/plot.js index d7860b8bcab..0c816bef808 100644 --- a/src/traces/scattermapbox/plot.js +++ b/src/traces/scattermapbox/plot.js @@ -81,12 +81,12 @@ proto.addLayer = function(k, opts, below) { if(opts.filter) { source.filter = opts.filter; } - var layerId = this.layerIds[k]; + var currentLayerId = this.layerIds[k]; var layerExist = this.subplot.getMapLayers().filter(function(mbLayer) { - return mbLayer.id === layerId; + return mbLayer.id === currentLayerId; }); - if(layerExist.length) { - this.subplot.map.removeLayer(layerId); + if(layerExist && layerExist.length) { + this.subplot.map.removeLayer(currentLayerId); } this.subplot.addLayer(source, below); }; From 7f99ee7c7a05dd6657393bf9638b83c3df262764 Mon Sep 17 00:00:00 2001 From: NickMelnikov Date: Tue, 25 Apr 2023 11:46:54 +0600 Subject: [PATCH 4/7] changed behaviour from removeLayer to setOptions --- src/traces/scattermapbox/plot.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/traces/scattermapbox/plot.js b/src/traces/scattermapbox/plot.js index 0c816bef808..cc06e1a9da7 100644 --- a/src/traces/scattermapbox/plot.js +++ b/src/traces/scattermapbox/plot.js @@ -86,9 +86,13 @@ proto.addLayer = function(k, opts, below) { return mbLayer.id === currentLayerId; }); if(layerExist && layerExist.length) { - this.subplot.map.removeLayer(currentLayerId); + 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); } - this.subplot.addLayer(source, below); }; proto.update = function update(calcTrace) { From faf590e99843111a05f95927ffb03100af435f75 Mon Sep 17 00:00:00 2001 From: Mojtaba Samimi <33888540+archmoj@users.noreply.github.com> Date: Thu, 27 Apr 2023 08:37:34 -0400 Subject: [PATCH 5/7] Update src/traces/scattermapbox/plot.js --- src/traces/scattermapbox/plot.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/traces/scattermapbox/plot.js b/src/traces/scattermapbox/plot.js index cc06e1a9da7..f53f32b99b7 100644 --- a/src/traces/scattermapbox/plot.js +++ b/src/traces/scattermapbox/plot.js @@ -82,10 +82,16 @@ proto.addLayer = function(k, opts, below) { source.filter = opts.filter; } var currentLayerId = this.layerIds[k]; - var layerExist = this.subplot.getMapLayers().filter(function(mbLayer) { - return mbLayer.id === currentLayerId; - }); - if(layerExist && layerExist.length) { + 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); From e40d20837021b41603b48b28cbb9cfc2066761aa Mon Sep 17 00:00:00 2001 From: Mojtaba Samimi Date: Thu, 27 Apr 2023 09:05:20 -0400 Subject: [PATCH 6/7] add test --- test/jasmine/tests/scattermapbox_test.js | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/jasmine/tests/scattermapbox_test.js b/test/jasmine/tests/scattermapbox_test.js index 68c2e267a84..aa07efcd5c8 100644 --- a/test/jasmine/tests/scattermapbox_test.js +++ b/test/jasmine/tests/scattermapbox_test.js @@ -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); + }); +}); From 36ef0baa5b3f8e2e02519af4b22a0aca545185e3 Mon Sep 17 00:00:00 2001 From: Mojtaba Samimi Date: Thu, 27 Apr 2023 09:26:38 -0400 Subject: [PATCH 7/7] draft log for PR 6567 --- draftlogs/6567_fix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/6567_fix.md diff --git a/draftlogs/6567_fix.md b/draftlogs/6567_fix.md new file mode 100644 index 00000000000..4c005d6b20c --- /dev/null +++ b/draftlogs/6567_fix.md @@ -0,0 +1 @@ + - Fix scattermapbox visibility restyle [[#6567](https://github.com/plotly/plotly.js/pull/6567)]