Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of arrayOk textposition for scatter3d traces #3200

Merged
merged 10 commits into from
Nov 9, 2018
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"gl-plot2d": "^1.3.1",
"gl-plot3d": "^1.5.10",
"gl-pointcloud2d": "^1.0.1",
"gl-scatter3d": "^1.0.14",
"gl-scatter3d": "git://github.com/gl-vis/gl-scatter3d.git#e1d123ef2c7c97a0bce5042b0eade8166cfe21ca",
"gl-select-box": "^1.0.2",
"gl-spikes2d": "^1.0.1",
"gl-streamtube3d": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ var attrs = module.exports = overrideAll({
colorAttributes('marker')
),

textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center', arrayOk: false}),
textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center'}),
textfont: {
color: scatterAttrs.textfont.color,
size: scatterAttrs.textfont.size,
Expand Down
49 changes: 41 additions & 8 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,47 @@ function calculateErrorParams(errors) {
return {capSize: capSize, color: color, lineWidth: lineWidth};
}

function calculateTextOffset(tp) {
function parseAlignmentX(a) {
if(a === null || a === undefined) return 0;

return (a.indexOf('left') > -1) ? -1 :
(a.indexOf('right') > -1) ? 1 : 0;
}

function parseAlignmentY(a) {
if(a === null || a === undefined) return 0;

return (a.indexOf('top') > -1) ? -1 :
(a.indexOf('bottom') > -1) ? 1 : 0;
}

function calculateTextOffset(tp, dflt) {
// Read out text properties
var textOffset = [0, 0];
if(Array.isArray(tp)) return [0, -1];
if(tp.indexOf('bottom') >= 0) textOffset[1] += 1;
if(tp.indexOf('top') >= 0) textOffset[1] -= 1;
if(tp.indexOf('left') >= 0) textOffset[0] -= 1;
if(tp.indexOf('right') >= 0) textOffset[0] += 1;

var defaultAlignmentX = parseAlignmentX(dflt);
var defaultAlignmentY = parseAlignmentY(dflt);

var textOffset = [
defaultAlignmentX,
defaultAlignmentY
];

if(Array.isArray(tp)) {
for(var i = 0; i < tp.length; i++) {
textOffset[i] = [
defaultAlignmentX,
defaultAlignmentY
];
if(tp[i]) {
textOffset[i][0] = parseAlignmentX(tp[i]);
textOffset[i][1] = parseAlignmentY(tp[i]);
}
}
} else {
textOffset[0] = parseAlignmentX(tp);
textOffset[1] = parseAlignmentY(tp);
}

return textOffset;
}

Expand Down Expand Up @@ -233,7 +266,7 @@ function convertPlotlyOptions(scene, data) {
}

if('textposition' in data) {
params.textOffset = calculateTextOffset(data.textposition); // arrayOk === false
params.textOffset = calculateTextOffset(data.textposition, data.dflt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. What's data.dflt here?

params.textColor = formatColor(data.textfont, 1, len);
params.textSize = formatParam(data.textfont.size, len, Lib.identity, 12);
params.textFont = data.textfont.family; // arrayOk === false
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions test/image/mocks/gl3d_scatter3d-different-align-texts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"data": [
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 1, 0, 1, 0, 1, 0, 1, 0],
"z": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"type": "scatter3d",
"mode":"lines+markers+text",
"text": ["middle center", "bottom", "right", "bottom right", "bottom left", "left", "top right", "top", "top left"],
"textposition": ["", "bottom", "right", "bottom right", "bottom left", "left", "top right", "top", "top left"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice mock!

}
],
"layout": {
"title":"Texts in scatter3d could be aligned differently using arrays",
"width": 800,
"height": 600,
"scene":{
"camera":{
"eye":{ "x":-1.25,"y":1.25,"z":1.25 },
"center":{ "x":0,"y":0,"z":0 },
"up":{ "x":0,"y":0,"z":1 }
}
}
}
}