Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
factor out convert text opts logic into file of its own
  • Loading branch information
etpinard committed Jun 22, 2016
commit cf27e55c643a3cf5db2f14dd541b8b61936dc5bb
72 changes: 72 additions & 0 deletions src/plots/mapbox/convert_text_opts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');


/**
* Convert plotly.js 'textposition' to mapbox-gl 'anchor' and 'offset'
* (with the help of the icon size).
*
* @param {string} textpostion : plotly.js textposition value
* @param {number} iconSize : plotly.js icon size (e.g. marker.size for traces)
*
* @return {object}
* - anchor
* - offset
*/
module.exports = function convertTextOpts(textposition, iconSize) {
var parts = textposition.split(' '),
vPos = parts[0],
hPos = parts[1];

// ballpack values
var factor = Array.isArray(iconSize) ? Lib.mean(iconSize) : iconSize,
xInc = 0.5 + (factor / 100),
yInc = 1.5 + (factor / 100);

var anchorVals = ['', ''],
offset = [0, 0];

switch(vPos) {
case 'top':
anchorVals[0] = 'top';
offset[1] = -yInc;
break;
case 'bottom':
anchorVals[0] = 'bottom';
offset[1] = yInc;
break;
}

switch(hPos) {
case 'left':
anchorVals[1] = 'right';
offset[0] = -xInc;
break;
case 'right':
anchorVals[1] = 'left';
offset[0] = xInc;
break;
}

// Mapbox text-anchor must be one of:
// center, left, right, top, bottom,
// top-left, top-right, bottom-left, bottom-right

var anchor;
if(anchorVals[0] && anchorVals[1]) anchor = anchorVals.join('-');
else if(anchorVals[0]) anchor = anchorVals[0];
else if(anchorVals[1]) anchor = anchorVals[1];
else anchor = 'center';

return { anchor: anchor, offset: offset };
};
54 changes: 3 additions & 51 deletions src/traces/scattermapbox/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var Lib = require('../../lib');
var subTypes = require('../scatter/subtypes');
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');

var COLOR_PROP = 'circle-color';
var SIZE_PROP = 'circle-radius';
Expand Down Expand Up @@ -108,7 +109,8 @@ module.exports = function convert(calcTrace) {
}

if(hasText) {
var textOpts = calcTextOpts(trace);
var iconSize = (trace.marker || {}).size,
textOpts = convertTextOpts(trace.textposition, iconSize);

Lib.extendFlat(symbol.layout, {
'text-size': trace.textfont.size,
Expand Down Expand Up @@ -309,56 +311,6 @@ function calcCircleRadius(trace, hash) {
return out;
}

function calcTextOpts(trace) {
var textposition = trace.textposition,
parts = textposition.split(' '),
vPos = parts[0],
hPos = parts[1];

// ballpack values
var ms = (trace.marker || {}).size,
factor = Array.isArray(ms) ? Lib.mean(ms) : ms,
xInc = 0.5 + (factor / 100),
yInc = 1.5 + (factor / 100);

var anchorVals = ['', ''],
offset = [0, 0];

switch(vPos) {
case 'top':
anchorVals[0] = 'top';
offset[1] = -yInc;
break;
case 'bottom':
anchorVals[0] = 'bottom';
offset[1] = yInc;
break;
}

switch(hPos) {
case 'left':
anchorVals[1] = 'right';
offset[0] = -xInc;
break;
case 'right':
anchorVals[1] = 'left';
offset[0] = xInc;
break;
}

// Mapbox text-anchor must be one of:
// center, left, right, top, bottom,
// top-left, top-right, bottom-left, bottom-right

var anchor;
if(anchorVals[0] && anchorVals[1]) anchor = anchorVals.join('-');
else if(anchorVals[0]) anchor = anchorVals[0];
else if(anchorVals[1]) anchor = anchorVals[1];
else anchor = 'center';

return { anchor: anchor, offset: offset };
}

function getCoords(calcTrace) {
var trace = calcTrace[0].trace,
connectgaps = trace.connectgaps;
Expand Down