Skip to content

Commit cf27e55

Browse files
committed
factor out convert text opts logic into file of its own
1 parent 34d5275 commit cf27e55

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed

src/plots/mapbox/convert_text_opts.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('../../lib');
13+
14+
15+
/**
16+
* Convert plotly.js 'textposition' to mapbox-gl 'anchor' and 'offset'
17+
* (with the help of the icon size).
18+
*
19+
* @param {string} textpostion : plotly.js textposition value
20+
* @param {number} iconSize : plotly.js icon size (e.g. marker.size for traces)
21+
*
22+
* @return {object}
23+
* - anchor
24+
* - offset
25+
*/
26+
module.exports = function convertTextOpts(textposition, iconSize) {
27+
var parts = textposition.split(' '),
28+
vPos = parts[0],
29+
hPos = parts[1];
30+
31+
// ballpack values
32+
var factor = Array.isArray(iconSize) ? Lib.mean(iconSize) : iconSize,
33+
xInc = 0.5 + (factor / 100),
34+
yInc = 1.5 + (factor / 100);
35+
36+
var anchorVals = ['', ''],
37+
offset = [0, 0];
38+
39+
switch(vPos) {
40+
case 'top':
41+
anchorVals[0] = 'top';
42+
offset[1] = -yInc;
43+
break;
44+
case 'bottom':
45+
anchorVals[0] = 'bottom';
46+
offset[1] = yInc;
47+
break;
48+
}
49+
50+
switch(hPos) {
51+
case 'left':
52+
anchorVals[1] = 'right';
53+
offset[0] = -xInc;
54+
break;
55+
case 'right':
56+
anchorVals[1] = 'left';
57+
offset[0] = xInc;
58+
break;
59+
}
60+
61+
// Mapbox text-anchor must be one of:
62+
// center, left, right, top, bottom,
63+
// top-left, top-right, bottom-left, bottom-right
64+
65+
var anchor;
66+
if(anchorVals[0] && anchorVals[1]) anchor = anchorVals.join('-');
67+
else if(anchorVals[0]) anchor = anchorVals[0];
68+
else if(anchorVals[1]) anchor = anchorVals[1];
69+
else anchor = 'center';
70+
71+
return { anchor: anchor, offset: offset };
72+
};

src/traces/scattermapbox/convert.js

+3-51
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Lib = require('../../lib');
1313
var subTypes = require('../scatter/subtypes');
14+
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');
1415

1516
var COLOR_PROP = 'circle-color';
1617
var SIZE_PROP = 'circle-radius';
@@ -108,7 +109,8 @@ module.exports = function convert(calcTrace) {
108109
}
109110

110111
if(hasText) {
111-
var textOpts = calcTextOpts(trace);
112+
var iconSize = (trace.marker || {}).size,
113+
textOpts = convertTextOpts(trace.textposition, iconSize);
112114

113115
Lib.extendFlat(symbol.layout, {
114116
'text-size': trace.textfont.size,
@@ -309,56 +311,6 @@ function calcCircleRadius(trace, hash) {
309311
return out;
310312
}
311313

312-
function calcTextOpts(trace) {
313-
var textposition = trace.textposition,
314-
parts = textposition.split(' '),
315-
vPos = parts[0],
316-
hPos = parts[1];
317-
318-
// ballpack values
319-
var ms = (trace.marker || {}).size,
320-
factor = Array.isArray(ms) ? Lib.mean(ms) : ms,
321-
xInc = 0.5 + (factor / 100),
322-
yInc = 1.5 + (factor / 100);
323-
324-
var anchorVals = ['', ''],
325-
offset = [0, 0];
326-
327-
switch(vPos) {
328-
case 'top':
329-
anchorVals[0] = 'top';
330-
offset[1] = -yInc;
331-
break;
332-
case 'bottom':
333-
anchorVals[0] = 'bottom';
334-
offset[1] = yInc;
335-
break;
336-
}
337-
338-
switch(hPos) {
339-
case 'left':
340-
anchorVals[1] = 'right';
341-
offset[0] = -xInc;
342-
break;
343-
case 'right':
344-
anchorVals[1] = 'left';
345-
offset[0] = xInc;
346-
break;
347-
}
348-
349-
// Mapbox text-anchor must be one of:
350-
// center, left, right, top, bottom,
351-
// top-left, top-right, bottom-left, bottom-right
352-
353-
var anchor;
354-
if(anchorVals[0] && anchorVals[1]) anchor = anchorVals.join('-');
355-
else if(anchorVals[0]) anchor = anchorVals[0];
356-
else if(anchorVals[1]) anchor = anchorVals[1];
357-
else anchor = 'center';
358-
359-
return { anchor: anchor, offset: offset };
360-
}
361-
362314
function getCoords(calcTrace) {
363315
var trace = calcTrace[0].trace,
364316
connectgaps = trace.connectgaps;

0 commit comments

Comments
 (0)