Skip to content

Commit 3e64aa0

Browse files
committed
update scattermapbox convert & hover
- so that it handles BADNUM items - does colorscale / size fn item conversions - these were handled in scattermapbox/calc.js previously
1 parent 1abbd90 commit 3e64aa0

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

src/traces/scattermapbox/convert.js

Lines changed: 42 additions & 11 deletions
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

Lines changed: 8 additions & 7 deletions
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
}

test/jasmine/tests/scattermapbox_test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ describe('scattermapbox convert', function() {
239239
return f.properties.text;
240240
});
241241

242-
expect(actualText).toEqual(['A', 'B', 'C', 'F', '']);
242+
expect(actualText).toEqual(['A', 'B', 'C', 'F', undefined]);
243243
});
244244

245245
it('should generate correct output for lines traces with trailing gaps', function() {
@@ -314,7 +314,9 @@ describe('scattermapbox convert', function() {
314314
fill: 'toself'
315315
}));
316316

317-
assertVisibility(opts, ['none', 'none', 'none', 'none']);
317+
// not optimal, but doesn't break anything as mapbox-gl accepts empty
318+
// coordinate arrays
319+
assertVisibility(opts, ['visible', 'visible', 'none', 'none']);
318320

319321
expect(opts.line.geojson.coordinates).toEqual([], 'line coords');
320322
expect(opts.fill.geojson.coordinates).toEqual([], 'fill coords');

0 commit comments

Comments
 (0)