Skip to content

Commit 248b760

Browse files
authored
Merge pull request #1564 from plotly/scattermapbox-badnum2
Scattermapbox BADNUM (w/o upgrading mapbox-gl)
2 parents 6a7e2a4 + b5cd573 commit 248b760

14 files changed

+63
-266
lines changed

src/plots/mapbox/mapbox.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
111111
});
112112

113113
// clear navigation container
114-
var className = constants.controlContainerClassName,
115-
controlContainer = self.div.getElementsByClassName(className)[0];
114+
var className = constants.controlContainerClassName;
115+
var controlContainer = self.div.getElementsByClassName(className)[0];
116116
self.div.removeChild(controlContainer);
117117

118+
// make sure canvas does not inherit left and top css
119+
map._canvas.canvas.style.left = '0px';
120+
map._canvas.canvas.style.top = '0px';
121+
118122
self.rejectOnError(reject);
119123

120124
map.once('load', function() {
@@ -176,7 +180,6 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
176180

177181
map.on('dragstart', unhover);
178182
map.on('zoomstart', unhover);
179-
180183
};
181184

182185
proto.updateMap = function(calcData, fullLayout, resolve, reject) {

src/plots/plots.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var Plotly = require('../plotly');
1616
var Registry = require('../registry');
1717
var Lib = require('../lib');
1818
var Color = require('../components/color');
19+
var BADNUM = require('../constants/numerical').BADNUM;
1920

2021
var plots = module.exports = {};
2122

@@ -2012,11 +2013,8 @@ plots.doCalcdata = function(gd, traces) {
20122013
//
20132014
// This ensures there is a calcdata item for every trace,
20142015
// even if cartesian logic doesn't handle it (for things like legends).
2015-
//
2016-
// Tag this artificial calc point with 'placeholder: true',
2017-
// to make it easier to skip over them in during the plot and hover step.
20182016
if(!Array.isArray(cd) || !cd[0]) {
2019-
cd = [{x: false, y: false, placeholder: true}];
2017+
cd = [{x: BADNUM, y: BADNUM}];
20202018
}
20212019

20222020
// add the trace-wide properties to the first point,

src/traces/scattermapbox/calc.js

-101
This file was deleted.

src/traces/scattermapbox/convert.js

+42-11
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13+
var BADNUM = require('../../constants/numerical').BADNUM;
1314
var geoJsonUtils = require('../../lib/geojson_utils');
1415

16+
var Colorscale = require('../../components/colorscale');
17+
var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
1518
var subTypes = require('../scatter/subtypes');
1619
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');
1720

@@ -43,16 +46,16 @@ module.exports = function convert(calcTrace) {
4346
};
4447

4548
// early return if not visible or placeholder
46-
if(!isVisible || calcTrace[0].placeholder) return opts;
49+
if(!isVisible) return opts;
4750

4851
// fill layer and line layer use the same coords
49-
var coords;
52+
var lineCoords;
5053
if(hasFill || hasLines) {
51-
coords = geoJsonUtils.calcTraceToLineCoords(calcTrace);
54+
lineCoords = geoJsonUtils.calcTraceToLineCoords(calcTrace);
5255
}
5356

5457
if(hasFill) {
55-
fill.geojson = geoJsonUtils.makePolygon(coords);
58+
fill.geojson = geoJsonUtils.makePolygon(lineCoords);
5659
fill.layout.visibility = 'visible';
5760

5861
Lib.extendFlat(fill.paint, {
@@ -61,7 +64,7 @@ module.exports = function convert(calcTrace) {
6164
}
6265

6366
if(hasLines) {
64-
line.geojson = geoJsonUtils.makeLine(coords);
67+
line.geojson = geoJsonUtils.makeLine(lineCoords);
6568
line.layout.visibility = 'visible';
6669

6770
Lib.extendFlat(line.paint, {
@@ -155,12 +158,30 @@ function initContainer() {
155158
//
156159
// The solution prove to be more robust than trying to generate
157160
// `stops` arrays from scale functions.
161+
//
162+
// TODO axe this when we bump mapbox-gl and rewrite this using
163+
// "identity" property functions.
164+
// See https://github.com/plotly/plotly.js/pull/1543
165+
//
158166
function makeCircleGeoJSON(calcTrace, hash) {
159167
var trace = calcTrace[0].trace;
168+
var marker = trace.marker;
169+
170+
var colorFn;
171+
if(Colorscale.hasColorscale(trace, 'marker')) {
172+
colorFn = Colorscale.makeColorScaleFunc(
173+
Colorscale.extractScale(marker.colorscale, marker.cmin, marker.cmax)
174+
);
175+
} else if(Array.isArray(marker.color)) {
176+
colorFn = Lib.identity;
177+
}
160178

161-
var marker = trace.marker,
162-
hasColorArray = Array.isArray(marker.color),
163-
hasSizeArray = Array.isArray(marker.size);
179+
var sizeFn;
180+
if(subTypes.isBubble(trace)) {
181+
sizeFn = makeBubbleSizeFn(trace);
182+
} else if(Array.isArray(marker.size)) {
183+
sizeFn = Lib.identity;
184+
}
164185

165186
// Translate vals in trace arrayOk containers
166187
// into a val-to-index hash object
@@ -174,16 +195,19 @@ function makeCircleGeoJSON(calcTrace, hash) {
174195

175196
for(var i = 0; i < calcTrace.length; i++) {
176197
var calcPt = calcTrace[i];
198+
var lonlat = calcPt.lonlat;
199+
200+
if(isBADNUM(lonlat)) continue;
177201

178202
var props = {};
179-
if(hasColorArray) translate(props, COLOR_PROP, calcPt.mcc, i);
180-
if(hasSizeArray) translate(props, SIZE_PROP, calcPt.mrc, i);
203+
if(colorFn) translate(props, COLOR_PROP, colorFn(calcPt.mc), i);
204+
if(sizeFn) translate(props, SIZE_PROP, sizeFn(calcPt.ms), i);
181205

182206
features.push({
183207
type: 'Feature',
184208
geometry: {
185209
type: 'Point',
186-
coordinates: calcPt.lonlat
210+
coordinates: lonlat
187211
},
188212
properties: props
189213
});
@@ -215,6 +239,8 @@ function makeSymbolGeoJSON(calcTrace) {
215239
for(var i = 0; i < calcTrace.length; i++) {
216240
var calcPt = calcTrace[i];
217241

242+
if(isBADNUM(calcPt.lonlat)) continue;
243+
218244
features.push({
219245
type: 'Feature',
220246
geometry: {
@@ -305,3 +331,8 @@ function getFillFunc(attr) {
305331
}
306332

307333
function blankFillFunc() { return ''; }
334+
335+
// only need to check lon (OR lat)
336+
function isBADNUM(lonlat) {
337+
return lonlat[0] === BADNUM;
338+
}

src/traces/scattermapbox/hover.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111

1212
var Fx = require('../../plots/cartesian/graph_interact');
1313
var getTraceColor = require('../scatter/get_trace_color');
14-
14+
var BADNUM = require('../../constants/numerical').BADNUM;
1515

1616
module.exports = function hoverPoints(pointData, xval, yval) {
1717
var cd = pointData.cd,
1818
trace = cd[0].trace,
1919
xa = pointData.xa,
2020
ya = pointData.ya;
2121

22-
if(cd[0].placeholder) return;
23-
2422
// compute winding number about [-180, 180] globe
2523
var winding = (xval >= 0) ?
2624
Math.floor((xval + 180) / 360) :
@@ -31,10 +29,13 @@ module.exports = function hoverPoints(pointData, xval, yval) {
3129
var xval2 = xval - lonShift;
3230

3331
function distFn(d) {
34-
var lonlat = d.lonlat,
35-
dx = Math.abs(xa.c2p(lonlat) - xa.c2p([xval2, lonlat[1]])),
36-
dy = Math.abs(ya.c2p(lonlat) - ya.c2p([lonlat[0], yval])),
37-
rad = Math.max(3, d.mrc || 0);
32+
var lonlat = d.lonlat;
33+
34+
if(lonlat[0] === BADNUM) return Infinity;
35+
36+
var dx = Math.abs(xa.c2p(lonlat) - xa.c2p([xval2, lonlat[1]]));
37+
var dy = Math.abs(ya.c2p(lonlat) - ya.c2p([lonlat[0], yval]));
38+
var rad = Math.max(3, d.mrc || 0);
3839

3940
return Math.max(Math.sqrt(dx * dx + dy * dy) - rad, 1 - 3 / rad);
4041
}

src/traces/scattermapbox/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var ScatterMapbox = {};
1414
ScatterMapbox.attributes = require('./attributes');
1515
ScatterMapbox.supplyDefaults = require('./defaults');
1616
ScatterMapbox.colorbar = require('../scatter/colorbar');
17-
ScatterMapbox.calc = require('./calc');
17+
ScatterMapbox.calc = require('../scattergeo/calc');
1818
ScatterMapbox.hoverPoints = require('./hover');
1919
ScatterMapbox.eventData = require('./event_data');
2020
ScatterMapbox.plot = require('./plot');

test/image/baselines/mapbox_0.png

-6 Bytes
Loading
262 Bytes
Loading
3 Bytes
Loading
-1.03 KB
Loading

test/image/baselines/mapbox_fill.png

1.81 KB
Loading
-143 Bytes
Loading
0 Bytes
Loading

0 commit comments

Comments
 (0)