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

Skip to content

Commit 22af60c

Browse files
committed
move is-numeric and arrayOk logic to calc step
1 parent 13620f1 commit 22af60c

File tree

3 files changed

+303
-64
lines changed

3 files changed

+303
-64
lines changed

src/traces/scattermapbox/calc.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
'use strict';
1111

12+
var isNumeric = require('fast-isnumeric');
13+
1214
var Lib = require('../../lib');
13-
var arrayToCalcItem = require('../../lib/array_to_calc_item');
1415
var hasColorscale = require('../../components/colorscale/has_colorscale');
1516
var makeColorScaleFn = require('../../components/colorscale/make_scale_function');
1617
var subtypes = require('../scatter/subtypes');
@@ -20,8 +21,13 @@ var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
2021

2122
module.exports = function calc(gd, trace) {
2223
var len = trace.lon.length,
23-
marker = trace.marker,
24-
hasMarkers = subtypes.hasMarkers(trace);
24+
marker = trace.marker;
25+
26+
var hasMarkers = subtypes.hasMarkers(trace),
27+
hasColorArray = (hasMarkers && Array.isArray(marker.color)),
28+
hasSizeArray = (hasMarkers && Array.isArray(marker.size)),
29+
hasSymbolArray = (hasMarkers && Array.isArray(marker.symbol)),
30+
hasTextArray = Array.isArray(trace.text);
2531

2632
calcMarkerColorscale(trace);
2733

@@ -33,26 +39,57 @@ module.exports = function calc(gd, trace) {
3339
makeBubbleSizeFn(trace) :
3440
Lib.identity;
3541

36-
var cd = new Array(len);
42+
var calcTrace = [],
43+
cnt = 0;
44+
45+
// Different than cartesian calc step
46+
// as skip over non-numeric lon, lat pairs.
47+
// This makes the hover and convert calculations simpler.
3748

3849
for(var i = 0; i < len; i++) {
39-
var cdi = cd[i] = {};
50+
var lon = trace.lon[i],
51+
lat = trace.lat[i];
4052

41-
// the isNumeric check is done in the convert step
53+
if(!isNumeric(lon) || !isNumeric(lat)) {
54+
if(cnt > 0) calcTrace[cnt - 1].gapAfter = true;
55+
continue;
56+
}
57+
58+
var calcPt = {};
59+
cnt++;
4260

43-
cdi.lonlat = [trace.lon[i], trace.lat[i]];
61+
// coerce numeric strings into numbers
62+
calcPt.lonlat = [+lon, +lat];
4463

4564
if(hasMarkers) {
46-
arrayToCalcItem(marker.color, cdi, 'mc', i);
47-
arrayToCalcItem(marker.size, cdi, 'ms', i);
48-
arrayToCalcItem(marker.symbol, cdi, 'mx', i);
4965

50-
if(cdi.mc !== undefined) cdi.mcc = colorFn(cdi.mc);
51-
if(cdi.ms !== undefined) cdi.mrc = sizeFn(cdi.ms);
66+
if(hasColorArray) {
67+
var mc = marker.color[i];
68+
69+
calcPt.mc = mc;
70+
calcPt.mcc = colorFn(mc);
71+
}
72+
73+
if(hasSizeArray) {
74+
var ms = marker.size[i];
75+
76+
calcPt.ms = ms;
77+
calcPt.mrc = sizeFn(ms);
78+
}
79+
80+
if(hasSymbolArray) {
81+
var mx = marker.symbol[i];
82+
calcPt.mx = (typeof mx === 'string') ? mx : 'circle';
83+
}
84+
}
85+
86+
if(hasTextArray) {
87+
var tx = trace.text[i];
88+
calcPt.tx = (typeof tx === 'string') ? tx : '';
5289
}
5390

54-
arrayToCalcItem(trace.text, cdi, 'tx', i);
91+
calcTrace.push(calcPt);
5592
}
5693

57-
return cd;
94+
return calcTrace;
5895
};

src/traces/scattermapbox/convert.js

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
'use strict';
1111

12-
var isNumeric = require('fast-isnumeric');
13-
1412
var Lib = require('../../lib');
1513
var subTypes = require('../scatter/subtypes');
1614

@@ -176,23 +174,20 @@ function makeCircleGeoJSON(calcTrace, hash) {
176174
var features = [];
177175

178176
for(var i = 0; i < calcTrace.length; i++) {
179-
var calcPt = calcTrace[i],
180-
lonlat = calcPt.lonlat;
177+
var calcPt = calcTrace[i];
181178

182179
var props = {};
183180
if(hasColorArray) translate(props, COLOR_PROP, calcPt.mcc, i);
184181
if(hasSizeArray) translate(props, SIZE_PROP, calcPt.mrc, i);
185182

186-
if(checkLonLat(lonlat)) {
187-
features.push({
188-
type: 'Feature',
189-
geometry: {
190-
type: 'Point',
191-
coordinates: coerceLonLat(lonlat)
192-
},
193-
properties: props
194-
});
195-
}
183+
features.push({
184+
type: 'Feature',
185+
geometry: {
186+
type: 'Point',
187+
coordinates: calcPt.lonlat
188+
},
189+
properties: props
190+
});
196191
}
197192

198193
return {
@@ -219,22 +214,19 @@ function makeSymbolGeoJSON(calcTrace) {
219214
var features = [];
220215

221216
for(var i = 0; i < calcTrace.length; i++) {
222-
var calcPt = calcTrace[i],
223-
lonlat = calcPt.lonlat;
224-
225-
if(checkLonLat(lonlat)) {
226-
features.push({
227-
type: 'Feature',
228-
geometry: {
229-
type: 'Point',
230-
coordinates: coerceLonLat(lonlat)
231-
},
232-
properties: {
233-
symbol: fillSymbol(calcPt.mx),
234-
text: fillText(calcPt.tx)
235-
}
236-
});
237-
}
217+
var calcPt = calcTrace[i];
218+
219+
features.push({
220+
type: 'Feature',
221+
geometry: {
222+
type: 'Point',
223+
coordinates: calcPt.lonlat
224+
},
225+
properties: {
226+
symbol: fillSymbol(calcPt.mx),
227+
text: fillText(calcPt.tx)
228+
}
229+
});
238230
}
239231

240232
return {
@@ -258,7 +250,7 @@ function calcCircleColor(trace, hash) {
258250
}
259251

260252
out = {
261-
property: SIZE_PROP,
253+
property: COLOR_PROP,
262254
stops: stops
263255
};
264256

@@ -350,14 +342,6 @@ function calcTextOpts(trace) {
350342
return { anchor: anchor, offset: offset };
351343
}
352344

353-
function checkLonLat(lonlat) {
354-
return isNumeric(lonlat[0]) && isNumeric(lonlat[1]);
355-
}
356-
357-
function coerceLonLat(lonlat) {
358-
return [+lonlat[0], +lonlat[1]];
359-
}
360-
361345
function getCoords(calcTrace) {
362346
var trace = calcTrace[0].trace,
363347
connectgaps = trace.connectgaps;
@@ -366,13 +350,11 @@ function getCoords(calcTrace) {
366350
lineString = [];
367351

368352
for(var i = 0; i < calcTrace.length; i++) {
369-
var calcPt = calcTrace[i],
370-
lonlat = calcPt.lonlat;
353+
var calcPt = calcTrace[i];
371354

372-
if(checkLonLat(lonlat)) {
373-
lineString.push(coerceLonLat(lonlat));
374-
}
375-
else if(!connectgaps && lineString.length > 0) {
355+
lineString.push(calcPt.lonlat);
356+
357+
if(!connectgaps && calcPt.gapAfter && lineString.length > 0) {
376358
coords.push(lineString);
377359
lineString = [];
378360
}

0 commit comments

Comments
 (0)