Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c49733

Browse files
committed
misc fixups:
- got map to resize on 'domain' relayout calls - got deleteTrace to work - got the hasMarkers legend item to display properly - got rid of some console errors upon trace toggle - got Mapbox to update user layout on pan/zoom
1 parent 53434bb commit 2c49733

File tree

4 files changed

+76
-49
lines changed

4 files changed

+76
-49
lines changed

src/plots/mapbox/layout_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ function handleDefaults(containerIn, containerOut, coerce) {
2727
coerce('center.lon');
2828
coerce('center.lat');
2929
coerce('zoom');
30+
31+
containerOut._input = containerIn;
3032
}

src/plots/mapbox/mapbox.js

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@
1212
var mapboxgl = require('mapbox-gl');
1313

1414
var constants = require('./constants');
15+
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1516

1617

1718
function Mapbox(opts) {
1819
this.id = opts.id;
1920
this.gd = opts.gd;
2021
this.container = opts.container;
21-
var fullLayout = this.fullLayout = opts.fullLayout;
2222

23-
this.uid = this.fullLayout._uid + '-' + this.id;
24-
this.opts = this.fullLayout[this.id];
25-
this.userOpts = this.gd.layout[this.id] || {};
23+
var fullLayout = opts.fullLayout;
24+
this.uid = fullLayout._uid + '-' + this.id;
25+
this.opts = fullLayout[this.id];
2626

27-
// create div on instantiation for smoother first plot call
28-
this.div = this.createDiv();
29-
this.updateDiv(fullLayout);
27+
// create div on instantiation for a smoother first plot call
28+
this.div = null;
29+
this.hoverLayer = null;
30+
this.createFramework(fullLayout);
3031

3132
this.map = null;
3233
this.traceHash = {};
@@ -42,8 +43,11 @@ module.exports = function createMapbox(opts) {
4243

4344
proto.plot = function(fullData, fullLayout, promises) {
4445
var self = this;
45-
var promise;
4646

47+
// feed in new mapbox options
48+
self.opts = fullLayout[this.id];
49+
50+
var promise;
4751
// might want to use map.loaded() ???
4852

4953
// how to get streaming to work ???
@@ -63,8 +67,8 @@ proto.plot = function(fullData, fullLayout, promises) {
6367
};
6468

6569
proto.createMap = function(fullData, fullLayout, resolve) {
66-
var self = this;
67-
var opts = self.opts;
70+
var self = this,
71+
opts = this.opts;
6872

6973
var map = self.map = new mapboxgl.Map({
7074
container: self.uid,
@@ -84,30 +88,33 @@ proto.createMap = function(fullData, fullLayout, resolve) {
8488
// hover code goes here !!!
8589
});
8690

87-
// TODO is that enough to keep layout and fullLayout in sync ???
88-
map.on('move', function(eventData) {
89-
map.getCenter();
90-
map.getZoom();
91+
// keep track of pan / zoom in user layout
92+
map.on('move', function() {
93+
var center = map.getCenter();
94+
opts._input.center = opts.center = { lon: center.lng, lat: center.lat };
95+
opts._input.zoom = opts.zoom = map.getZoom();
9196
});
9297

9398
};
9499

95100
proto.updateMap = function(fullData, fullLayout, resolve) {
96101
var self = this,
97-
map = self.map,
98-
opts = fullLayout[self.id];
102+
map = self.map;
99103

100104
var currentStyle = self.getStyle(),
101-
style = opts.style;
105+
style = self.opts.style;
102106

103107
if(style !== currentStyle) {
104108
console.log('reload style')
105109
map.setStyle(convertStyleUrl(style));
106110

107111
map.style.once('load', function() {
108112
console.log('on style reload')
109-
// ...
113+
114+
// need to rebuild trace layers on reload
115+
// to avoid 'lost event' errors
110116
self.traceHash = {};
117+
111118
self.updateData(fullData);
112119
self.updateLayout(fullLayout);
113120
resolve();
@@ -153,41 +160,53 @@ proto.updateData = function(fullData) {
153160
};
154161

155162
proto.updateLayout = function(fullLayout) {
156-
var opts = fullLayout[this.id],
157-
map = this.map;
163+
var map = this.map,
164+
opts = this.opts;
158165

159166
map.setCenter(convertCenter(opts.center));
160167
map.setZoom(opts.zoom);
161168

162-
this.updateDiv(fullLayout)
163-
// resize()
169+
170+
this.updateFramework(fullLayout)
171+
this.map.resize();
164172
};
165173

166-
proto.createDiv = function() {
167-
var div = document.createElement('div');
168-
this.container.appendChild(div);
174+
proto.createFramework = function(fullLayout) {
175+
var div = this.div = document.createElement('div');
169176

170177
div.id = this.uid;
171178
div.style.position = 'absolute';
172179

173-
return div;
174-
};
180+
var hoverLayer = this.hoverLayer = document.createElementNS(
181+
xmlnsNamespaces.svg, 'svg'
182+
);
183+
184+
var hoverStyle = hoverLayer.style;
175185

176-
proto.updateDiv = function(fullLayout) {
177-
var div = this.div;
186+
hoverStyle.position = 'absolute';
187+
hoverStyle.top = hoverStyle.left = '0px';
188+
hoverStyle.width = hoverStyle.height = '100%';
189+
hoverStyle['z-index'] = 20;
190+
hoverStyle['pointer-events'] = 'none';
178191

192+
this.container.appendChild(div);
193+
this.container.appendChild(hoverLayer);
194+
195+
this.updateFramework(fullLayout);
196+
};
197+
198+
proto.updateFramework = function(fullLayout) {
179199
var domain = fullLayout[this.id].domain,
180-
size = fullLayout._size,
181-
style = div.style;
200+
size = fullLayout._size;
201+
202+
var style = this.div.style;
203+
204+
// Is this correct? It seems to get the map zoom level wrong?
182205

183-
style.left = size.l + domain.x[0] * size.w + 'px';
184-
style.top = size.t + (1 - domain.y[1]) * size.h + 'px';
185206
style.width = size.w * (domain.x[1] - domain.x[0]) + 'px';
186207
style.height = size.h * (domain.y[1] - domain.y[0]) + 'px';
187-
188-
// style.top = 0;
189-
// style.bottom = 0;
190-
// style.width = '100%';
208+
style.left = size.l + domain.x[0] * size.w + 'px';
209+
style.top = size.t + (1 - domain.y[1]) * size.h + 'px';
191210
};
192211

193212
proto.destroy = function() {

src/traces/scattermapbox/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ ScatterMapbox.plot = require('./plot');
2727
ScatterMapbox.moduleType = 'trace';
2828
ScatterMapbox.name = 'scattermapbox';
2929
ScatterMapbox.basePlotModule = require('../../plots/mapbox');
30-
ScatterMapbox.categories = ['mapbox', 'gl', 'markerColorscale', 'showLegend'];
30+
ScatterMapbox.categories = ['mapbox', 'gl', 'symbols', 'showLegend'];
31+
// ScatterMapbox.categories = ['mapbox', 'gl', 'symbols', 'markerColorscale', 'showLegend'];
3132
ScatterMapbox.meta = {
3233
hrName: 'scatter_mapbox',
3334
description: [
3435
'The data visualized as scatter point or lines',
3536
'on a Mapbox GL geographic map',
36-
'is provided either by longitude/latitude pairs in `lon` and `lat`',
37-
'respectively or by geographic location IDs or names in `locations`.'
37+
'is provided by longitude/latitude pairs in `lon` and `lat`.'
3838
].join(' ')
3939
};
4040

src/traces/scattermapbox/plot.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,27 @@ proto.update = function update(trace) {
5252

5353
console.log('scatter update');
5454

55-
this.sourceLines.setData(opts.geojsonLines);
5655
setOptions(this.map, this.idLayerLines, 'setLayoutProperty', opts.layoutLines);
57-
setOptions(this.map, this.idLayerLines, 'setPaintProperty', opts.paintLines);
58-
59-
this.sourceMarkers.setData(opts.geojsonMarkers);
6056
setOptions(this.map, this.idLayerMarkers, 'setLayoutProperty', opts.layoutMarkers);
61-
setOptions(this.map, this.idLayerMarkers, 'setPaintProperty', opts.paintMarkers);
57+
58+
if(opts.layoutLines.visibility === 'visible') {
59+
this.sourceLines.setData(opts.geojsonLines);
60+
setOptions(this.map, this.idLayerLines, 'setPaintProperty', opts.paintLines);
61+
}
62+
63+
if(opts.layoutMarkers.visibility === 'visible') {
64+
this.sourceMarkers.setData(opts.geojsonMarkers);
65+
setOptions(this.map, this.idLayerMarkers, 'setPaintProperty', opts.paintMarkers);
66+
}
6267
};
6368

6469
proto.dispose = function dispose() {
65-
this.removeLayer(this.idLayerMarkers);
66-
this.removeSource(this.idSourceMarkers);
70+
var map = this.map;
6771

68-
this.removeLayer(this.idLayerLines);
69-
this.removeSource(this.idSourceLines);
72+
map.removeLayer(this.idLayerMarkers);
73+
map.removeSource(this.idSourceMarkers);
74+
map.removeLayer(this.idLayerLines);
75+
map.removeSource(this.idSourceLines);
7076
};
7177

7278
function setOptions(map, id, methodName, opts) {

0 commit comments

Comments
 (0)