Skip to content

Commit 7d3fabb

Browse files
committed
texttemplate: add support for scattermapbox
the baseline produced with the old image server does not show text labels
1 parent 888cf68 commit 7d3fabb

File tree

7 files changed

+98
-7
lines changed

7 files changed

+98
-7
lines changed

src/traces/scattermapbox/attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
12+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1213
var scatterGeoAttrs = require('../scattergeo/attributes');
1314
var scatterAttrs = require('../scatter/attributes');
1415
var mapboxAttrs = require('../../plots/mapbox/layout_attributes');
@@ -49,6 +50,9 @@ module.exports = overrideAll({
4950
'these elements will be seen in the hover labels.'
5051
].join(' ')
5152
}),
53+
texttemplate: texttemplateAttrs({editType: 'calc'}, {
54+
keys: ['lat', 'lon', 'text']
55+
}),
5256
hovertext: extendFlat({}, scatterAttrs.hovertext, {
5357
description: [
5458
'Sets hover text elements associated with each (lon,lat) pair',

src/traces/scattermapbox/convert.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
2020
var subTypes = require('../scatter/subtypes');
2121
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');
2222

23-
module.exports = function convert(calcTrace) {
23+
module.exports = function convert(calcTrace, gd) {
2424
var trace = calcTrace[0].trace;
2525

2626
var isVisible = (trace.visible === true && trace._length !== 0);
@@ -87,7 +87,7 @@ module.exports = function convert(calcTrace) {
8787
}
8888

8989
if(hasSymbols || hasText) {
90-
symbol.geojson = makeSymbolGeoJSON(calcTrace);
90+
symbol.geojson = makeSymbolGeoJSON(calcTrace, gd);
9191

9292
Lib.extendFlat(symbol.layout, {
9393
visibility: 'visible',
@@ -229,7 +229,7 @@ function makeCircleOpts(calcTrace) {
229229
};
230230
}
231231

232-
function makeSymbolGeoJSON(calcTrace) {
232+
function makeSymbolGeoJSON(calcTrace, gd) {
233233
var trace = calcTrace[0].trace;
234234

235235
var marker = trace.marker || {};
@@ -251,6 +251,16 @@ function makeSymbolGeoJSON(calcTrace) {
251251

252252
if(isBADNUM(calcPt.lonlat)) continue;
253253

254+
var txt = trace.texttemplate;
255+
if(txt) {
256+
var txti = Array.isArray(txt) ? (txt[i] || '') : txt;
257+
calcPt.text = calcPt.tx;
258+
calcPt.lon = calcPt.lonlat[0];
259+
calcPt.lat = calcPt.lonlat[1];
260+
calcPt.customdata = calcPt.data;
261+
calcPt.txt = Lib.texttemplateString(txti, {}, gd._fullLayout._d3locale, calcPt, trace._meta || {});
262+
}
263+
254264
features.push({
255265
type: 'Feature',
256266
geometry: {
@@ -259,7 +269,7 @@ function makeSymbolGeoJSON(calcTrace) {
259269
},
260270
properties: {
261271
symbol: fillSymbol(calcPt.mx),
262-
text: fillText(calcPt.tx)
272+
text: txt ? calcPt.txt : fillText(calcPt.tx)
263273
}
264274
});
265275
}

src/traces/scattermapbox/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2929
}
3030

3131
coerce('text');
32+
coerce('texttemplate');
3233
coerce('hovertext');
3334
coerce('hovertemplate');
3435
coerce('mode');

src/traces/scattermapbox/plot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ proto.addLayer = function(k, opts, below) {
6767
proto.update = function update(calcTrace) {
6868
var subplot = this.subplot;
6969
var map = subplot.map;
70-
var optsAll = convert(calcTrace);
70+
var optsAll = convert(calcTrace, subplot.gd);
7171
var below = subplot.belowLookup['trace-' + this.uid];
7272
var i, k, opts;
7373

@@ -113,7 +113,7 @@ proto.dispose = function dispose() {
113113
module.exports = function createScatterMapbox(subplot, calcTrace) {
114114
var trace = calcTrace[0].trace;
115115
var scatterMapbox = new ScatterMapbox(subplot, trace.uid);
116-
var optsAll = convert(calcTrace);
116+
var optsAll = convert(calcTrace, subplot.gd);
117117
var below = scatterMapbox.below = subplot.belowLookup['trace-' + trace.uid];
118118

119119
for(var i = 0; i < ORDER.length; i++) {
46 KB
Loading
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"data": [{
3+
"type": "scattermapbox",
4+
"mode": "markers+text",
5+
"lon": [
6+
-73.57,
7+
-79.24,
8+
-123.06
9+
],
10+
"lat": [
11+
45.5,
12+
43.4,
13+
49.13
14+
],
15+
"text": [
16+
"Montreal",
17+
"Toronto",
18+
"Vancouver"
19+
],
20+
"texttemplate": "%{text} (%{lonlat[0]}, %{lonlat[1]}): %{customdata:.2s}",
21+
"textposition": "top center",
22+
"customdata": [1780000, 2930000, 675218]
23+
}],
24+
25+
"layout": {
26+
"width": 500,
27+
"height": 500,
28+
"title": {"text": "Mapbox with texttemplate"},
29+
"margin": {"t": 30, "b": 0, "l": 0, "r": 0},
30+
"mapbox": {
31+
"center": {
32+
"lon": -90,
33+
"lat": 45
34+
},
35+
"zoom": 1.5
36+
}
37+
}
38+
}

test/jasmine/tests/scattermapbox_test.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('scattermapbox convert', function() {
139139
Plots.doCalcdata(gd, fullTrace);
140140

141141
var calcTrace = gd.calcdata[0];
142-
return convert(calcTrace);
142+
return convert(calcTrace, {_fullLayout: {_d3locale: false}});
143143
}
144144

145145
function assertVisibility(opts, expectations) {
@@ -478,6 +478,44 @@ describe('scattermapbox convert', function() {
478478
expect(actualText).toEqual(['A', 'B', 'C', 'F', undefined]);
479479
});
480480

481+
it('should generate correct output for texttemplate without text', function() {
482+
var opts = _convert(Lib.extendFlat({}, base, {
483+
mode: 'lines+text',
484+
connectgaps: true,
485+
textposition: 'outside',
486+
texttemplate: ['A', 'B', 'C', 'D', 'E', 'F']
487+
}));
488+
489+
var actualText = opts.symbol.geojson.features.map(function(f) {
490+
return f.properties.text;
491+
});
492+
493+
expect(actualText).toEqual(['A', 'B', 'C', 'F', '']);
494+
});
495+
496+
it('should generate correct output for texttemplate', function() {
497+
var mock = {
498+
'type': 'scattermapbox',
499+
'mode': 'markers+text',
500+
'lon': [-73.57, -79.24, -123.06],
501+
'lat': [45.5, 43.4, 49.13],
502+
'text': ['Montreal', 'Toronto', 'Vancouver'],
503+
'texttemplate': '%{text} (%{lonlat[0]}, %{lonlat[1]}): %{customdata:.2s}',
504+
'textposition': 'top center',
505+
'customdata': [1780000, 2930000, 675218]
506+
};
507+
var opts = _convert(mock);
508+
var actualText = opts.symbol.geojson.features.map(function(f) {
509+
return f.properties.text;
510+
});
511+
512+
expect(actualText).toEqual([
513+
'Montreal (-73.57, 45.5): 1.8M',
514+
'Toronto (-79.24, 43.4): 2.9M',
515+
'Vancouver (-123.06, 49.13): 680k'
516+
]);
517+
});
518+
481519
it('should generate correct output for lines traces with trailing gaps', function() {
482520
var opts = _convert(Lib.extendFlat({}, base, {
483521
mode: 'lines',

0 commit comments

Comments
 (0)