Skip to content

Commit 6a8a8dc

Browse files
authored
Merge pull request #4575 from zouhairm/dev/mbx_symbol
adding support for icon-rotate and icon-allow-overlap in mapbox
2 parents c82ad2e + cd91905 commit 6a8a8dc

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-5
lines changed

src/traces/scattermapbox/attributes.js

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ module.exports = overrideAll({
8787
'are only available for *circle* symbols.'
8888
].join(' ')
8989
},
90+
angle: {
91+
valType: 'number',
92+
dflt: 0,
93+
role: 'style',
94+
arrayOk: true,
95+
description: [
96+
'Sets the marker rotation, in degrees clockwise.'
97+
].join(' ')
98+
},
99+
allowoverlap: {
100+
valType: 'boolean',
101+
dflt: false,
102+
role: 'style',
103+
description: [
104+
'Flag to draw all symbols, even if they overlap.'
105+
].join(' ')
106+
},
90107
opacity: markerAttrs.opacity,
91108
size: markerAttrs.size,
92109
sizeref: markerAttrs.sizeref,

src/traces/scattermapbox/convert.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ module.exports = function convert(gd, calcTrace) {
104104
'icon-size': trace.marker.size / 10
105105
});
106106

107+
if('angle' in trace.marker) {
108+
Lib.extendFlat(symbol.layout, {
109+
// unfortunately cant use {angle} do to this issue:
110+
// https://github.com/mapbox/mapbox-gl-js/issues/873
111+
'icon-rotate': {
112+
type: 'identity', property: 'angle'
113+
},
114+
'icon-rotation-alignment': 'map'
115+
});
116+
}
117+
118+
symbol.layout['icon-allow-overlap'] = trace.marker.allowoverlap;
119+
107120
Lib.extendFlat(symbol.paint, {
108121
'icon-opacity': trace.opacity * trace.marker.opacity,
109122

@@ -239,15 +252,21 @@ function makeSymbolGeoJSON(calcTrace, gd) {
239252

240253
var marker = trace.marker || {};
241254
var symbol = marker.symbol;
255+
var angle = marker.angle;
242256

243257
var fillSymbol = (symbol !== 'circle') ?
244258
getFillFunc(symbol) :
245259
blankFillFunc;
246260

261+
var fillAngle = (angle) ?
262+
getFillFunc(angle, true) :
263+
blankFillFunc;
264+
247265
var fillText = subTypes.hasText(trace) ?
248266
getFillFunc(trace.text) :
249267
blankFillFunc;
250268

269+
251270
var features = [];
252271

253272
for(var i = 0; i < calcTrace.length; i++) {
@@ -266,7 +285,7 @@ function makeSymbolGeoJSON(calcTrace, gd) {
266285
var meta = trace._meta || {};
267286
text = Lib.texttemplateString(tt, labels, fullLayout._d3locale, pointValues, calcPt, meta);
268287
} else {
269-
text = fillText(calcPt.tx);
288+
text = fillText(i);
270289
}
271290

272291
if(text) {
@@ -280,7 +299,8 @@ function makeSymbolGeoJSON(calcTrace, gd) {
280299
coordinates: calcPt.lonlat
281300
},
282301
properties: {
283-
symbol: fillSymbol(calcPt.mx),
302+
symbol: fillSymbol(i),
303+
angle: fillAngle(i),
284304
text: text
285305
}
286306
});
@@ -292,9 +312,12 @@ function makeSymbolGeoJSON(calcTrace, gd) {
292312
};
293313
}
294314

295-
function getFillFunc(attr) {
315+
function getFillFunc(attr, numeric) {
296316
if(Lib.isArrayOrTypedArray(attr)) {
297-
return function(v) { return v; };
317+
if(numeric) {
318+
return function(i) { return isNumeric(attr[i]) ? +attr[i] : 0; };
319+
}
320+
return function(i) { return attr[i]; };
298321
} else if(attr) {
299322
return function() { return attr; };
300323
} else {

src/traces/scattermapbox/defaults.js

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4343
if(subTypes.hasMarkers(traceOut)) {
4444
handleMarkerDefaults(traceIn, traceOut, defaultColor, layout, coerce, {noLine: true});
4545

46+
coerce('marker.allowoverlap');
47+
coerce('marker.angle');
48+
4649
// array marker.size and marker.color are only supported with circles
4750
var marker = traceOut.marker;
4851
if(marker.symbol !== 'circle') {
-30.2 KB
Loading

test/image/mocks/mapbox_angles.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"monument",
4242
"harbor",
4343
"music"
44-
]
44+
],
45+
"angle": [-45, 45, 0],
46+
"allowoverlap":true
4547
},
4648
"subplot": "mapbox2"
4749
}

test/jasmine/tests/scattermapbox_test.js

+24
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,30 @@ describe('scattermapbox convert', function() {
465465
expect(symbolProps).toEqual(expected, 'geojson properties');
466466
});
467467

468+
469+
it('should allow symbols to be rotated and overlapped', function() {
470+
var opts = _convert(Lib.extendFlat({}, base, {
471+
mode: 'markers',
472+
marker: {
473+
symbol: ['monument', 'music', 'harbor'],
474+
angle: [0, 90, 45],
475+
allowoverlap: true
476+
},
477+
}));
478+
479+
var symbolAngle = opts.symbol.geojson.features.map(function(f) {
480+
return f.properties.angle;
481+
});
482+
483+
var expected = [0, 90, 45, 0, 0];
484+
expect(symbolAngle).toEqual(expected, 'geojson properties');
485+
486+
487+
expect(opts.symbol.layout['icon-rotate'].property).toEqual('angle', 'symbol.layout.icon-rotate');
488+
expect(opts.symbol.layout['icon-allow-overlap']).toEqual(true, 'symbol.layout.icon-allow-overlap');
489+
});
490+
491+
468492
it('should generate correct output for text + lines traces', function() {
469493
var opts = _convert(Lib.extendFlat({}, base, {
470494
mode: 'lines+text',

0 commit comments

Comments
 (0)