Skip to content

Commit 4b98be6

Browse files
committed
fixes for mapbox with Plotly.react
1 parent ade3cca commit 4b98be6

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

src/plots/mapbox/index.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ exports.plot = function plotMapbox(gd) {
6262
opts = fullLayout[id],
6363
mapbox = opts._subplot;
6464

65-
// copy access token to fullLayout (to handle the context case)
66-
opts.accesstoken = accessToken;
67-
6865
if(!mapbox) {
6966
mapbox = createMapbox({
7067
gd: gd,
@@ -136,24 +133,17 @@ function findAccessToken(gd, mapboxIds) {
136133
// special case for Mapbox Atlas users
137134
if(context.mapboxAccessToken === '') return '';
138135

139-
// first look for access token in context
140-
var accessToken = context.mapboxAccessToken;
141-
142-
// allow mapbox layout options to override it
136+
// Take the first token we find in a mapbox subplot.
137+
// These default to the context value but may be overridden.
143138
for(var i = 0; i < mapboxIds.length; i++) {
144139
var opts = fullLayout[mapboxIds[i]];
145140

146141
if(opts.accesstoken) {
147-
accessToken = opts.accesstoken;
148-
break;
142+
return opts.accesstoken;
149143
}
150144
}
151145

152-
if(!accessToken) {
153-
throw new Error(constants.noAccessTokenErrorMsg);
154-
}
155-
156-
return accessToken;
146+
throw new Error(constants.noAccessTokenErrorMsg);
157147
}
158148

159149
exports.updateFx = function(fullLayout) {

src/plots/mapbox/layout_defaults.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
2020
type: 'mapbox',
2121
attributes: layoutAttributes,
2222
handleDefaults: handleDefaults,
23-
partition: 'y'
23+
partition: 'y',
24+
accessToken: layoutOut._mapboxAccessToken
2425
});
2526
};
2627

27-
function handleDefaults(containerIn, containerOut, coerce) {
28-
coerce('accesstoken');
28+
function handleDefaults(containerIn, containerOut, coerce, opts) {
29+
coerce('accesstoken', opts.accessToken);
2930
coerce('style');
3031
coerce('center.lon');
3132
coerce('center.lat');

src/plots/plots.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ plots.supplyDefaults = function(gd) {
295295
var newFullData = gd._fullData = [];
296296
var newData = gd.data || [];
297297

298+
var context = gd._context || {};
299+
298300
var i;
299301

300302
// Create all the storage space for frames, but only if doesn't already exist
@@ -322,6 +324,9 @@ plots.supplyDefaults = function(gd) {
322324

323325
var formatObj = getFormatObj(gd, d3FormatKeys);
324326

327+
// stash the token from context so mapbox subplots can use it as default
328+
newFullLayout._mapboxAccessToken = context.mapboxAccessToken;
329+
325330
// first fill in what we can of layout without looking at data
326331
// because fullData needs a few things from layout
327332

@@ -343,7 +348,7 @@ plots.supplyDefaults = function(gd) {
343348

344349
var missingWidthOrHeight = (!newLayout.width || !newLayout.height),
345350
autosize = newFullLayout.autosize,
346-
autosizable = gd._context && gd._context.autosizable,
351+
autosizable = context.autosizable,
347352
initialAutoSize = missingWidthOrHeight && (autosize || autosizable);
348353

349354
if(initialAutoSize) plots.plotAutoSize(gd, newLayout, newFullLayout);

src/traces/scattermapbox/defaults.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ function handleLonLatDefaults(traceIn, traceOut, coerce) {
6969
var lon = coerce('lon') || [];
7070
var lat = coerce('lat') || [];
7171
var len = Math.min(lon.length, lat.length);
72-
73-
if(len < lon.length) traceOut.lon = lon.slice(0, len);
74-
if(len < lat.length) traceOut.lat = lat.slice(0, len);
72+
traceOut._length = len;
7573

7674
return len;
7775
}

test/jasmine/tests/scattermapbox_test.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,28 @@ describe('scattermapbox defaults', function() {
3838
return traceOut;
3939
}
4040

41-
it('should truncate \'lon\' if longer than \'lat\'', function() {
41+
it('should not truncate \'lon\' if longer than \'lat\'', function() {
42+
// this is handled at the calc step now via _length.
4243
var fullTrace = _supply({
4344
lon: [1, 2, 3],
4445
lat: [2, 3]
4546
});
4647

47-
expect(fullTrace.lon).toEqual([1, 2]);
48+
expect(fullTrace.lon).toEqual([1, 2, 3]);
4849
expect(fullTrace.lat).toEqual([2, 3]);
50+
expect(fullTrace._length).toBe(2);
4951
});
5052

51-
it('should truncate \'lat\' if longer than \'lon\'', function() {
53+
it('should not truncate \'lat\' if longer than \'lon\'', function() {
54+
// this is handled at the calc step now via _length.
5255
var fullTrace = _supply({
5356
lon: [1, 2, 3],
5457
lat: [2, 3, 3, 5]
5558
});
5659

5760
expect(fullTrace.lon).toEqual([1, 2, 3]);
58-
expect(fullTrace.lat).toEqual([2, 3, 3]);
61+
expect(fullTrace.lat).toEqual([2, 3, 3, 5]);
62+
expect(fullTrace._length).toBe(3);
5963
});
6064

6165
it('should set \'visible\' to false if \'lat\' and/or \'lon\' has zero length', function() {

0 commit comments

Comments
 (0)