Skip to content

Implementation of arrayOk textposition for scatter3d traces #3200

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

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#2e325c43a31c33737bc547ed3fad749c0f995250",
"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', arrayOk: true}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Making this line

textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center'}),

would suffice as textposition in the scatter attributes is already arrayOk:

textposition: {
valType: 'enumerated',
values: [
'top left', 'top center', 'top right',
'middle left', 'middle center', 'middle right',
'bottom left', 'bottom center', 'bottom right'
],
dflt: 'middle center',
arrayOk: true,
role: 'style',
editType: 'calc',
description: [
'Sets the positions of the `text` elements',
'with respects to the (x,y) coordinates.'
].join(' ')
},

textfont: {
color: scatterAttrs.textfont.color,
size: scatterAttrs.textfont.size,
Expand Down
49 changes: 42 additions & 7 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,49 @@ function calculateErrorParams(errors) {
return {capSize: capSize, color: color, lineWidth: lineWidth};
}

function parseAlignmentX(a) {
if(a === null || a === undefined) return 0;
else if(typeof(a) === 'number') return a;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would textposition[i] ever be a number here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I thought we could somehow make use of other numbers (instead than only -1|0|1) to scale the distances. But yeah I may remove those condition checks (also for null & undefined) since I noticed the inputs seem to be parsed and validated before getting to there?

else if(a.indexOf('left') > -1) return -1;
else if(a.indexOf('right') > -1) return 1;
return 0;
}

function parseAlignmentY(a) {
if(a === null || a === undefined) return 0;
else if(typeof(a) === 'number') return a;
else if(a.indexOf('top') > -1) return -1;
else if(a.indexOf('bottom') > -1) return 1;
return 0;
}

function calculateTextOffset(tp) {
// 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 = 0; // center
var defaultAlignmentY = -1; // top

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 +268,7 @@ function convertPlotlyOptions(scene, data) {
}

if('textposition' in data) {
params.textOffset = calculateTextOffset(data.textposition); // arrayOk === false
params.textOffset = calculateTextOffset(data.textposition);
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 }
}
}
}
}