Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6d7532

Browse files
committed
support for different text alignments using scattergl
1 parent 458f593 commit e6d7532

File tree

6 files changed

+71
-12
lines changed

6 files changed

+71
-12
lines changed

package-lock.json

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"gl-plot2d": "^1.3.1",
8181
"gl-plot3d": "^1.5.10",
8282
"gl-pointcloud2d": "^1.0.1",
83-
"gl-scatter3d": "^1.0.14",
83+
"gl-scatter3d": "git://github.com/gl-vis/gl-scatter3d.git#2e325c43a31c33737bc547ed3fad749c0f995250",
8484
"gl-select-box": "^1.0.2",
8585
"gl-spikes2d": "^1.0.1",
8686
"gl-streamtube3d": "^1.1.0",

src/traces/scatter3d/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ var attrs = module.exports = overrideAll({
160160
colorAttributes('marker')
161161
),
162162

163-
textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center', arrayOk: false}),
163+
textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center', arrayOk: true}),
164164
textfont: {
165165
color: scatterAttrs.textfont.color,
166166
size: scatterAttrs.textfont.size,

src/traces/scatter3d/convert.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,49 @@ function calculateErrorParams(errors) {
133133
return {capSize: capSize, color: color, lineWidth: lineWidth};
134134
}
135135

136+
function parseAlignmentX(a) {
137+
if (a === null || a === undefined) return 0;
138+
else if (typeof(a) === 'number') return a;
139+
else if (a.indexOf('left') > -1) return -1;
140+
else if (a.indexOf('right') > -1) return 1;
141+
return 0;
142+
}
143+
144+
function parseAlignmentY(a) {
145+
if (a === null || a === undefined) return 0;
146+
else if (typeof(a) === 'number') return a;
147+
else if (a.indexOf('top') > -1) return -1;
148+
else if (a.indexOf('bottom') > -1) return 1;
149+
return 0;
150+
}
151+
136152
function calculateTextOffset(tp) {
137153
// Read out text properties
138-
var textOffset = [0, 0];
139-
if(Array.isArray(tp)) return [0, -1];
140-
if(tp.indexOf('bottom') >= 0) textOffset[1] += 1;
141-
if(tp.indexOf('top') >= 0) textOffset[1] -= 1;
142-
if(tp.indexOf('left') >= 0) textOffset[0] -= 1;
143-
if(tp.indexOf('right') >= 0) textOffset[0] += 1;
154+
155+
var defaultAlignmentX = 0; // center
156+
var defaultAlignmentY = -1; // top
157+
158+
var textOffset = [
159+
defaultAlignmentX,
160+
defaultAlignmentY
161+
];
162+
163+
if(Array.isArray(tp)) {
164+
for (var i = 0; i < tp.length; i++) {
165+
textOffset[i] = [
166+
defaultAlignmentX,
167+
defaultAlignmentY
168+
];
169+
if (tp[i]) {
170+
textOffset[i][0] = parseAlignmentX(tp[i]);
171+
textOffset[i][1] = parseAlignmentY(tp[i]);
172+
}
173+
}
174+
} else {
175+
textOffset[0] = parseAlignmentX(tp);
176+
textOffset[1] = parseAlignmentY(tp);
177+
}
178+
144179
return textOffset;
145180
}
146181

@@ -233,7 +268,7 @@ function convertPlotlyOptions(scene, data) {
233268
}
234269

235270
if('textposition' in data) {
236-
params.textOffset = calculateTextOffset(data.textposition); // arrayOk === false
271+
params.textOffset = calculateTextOffset(data.textposition);
237272
params.textColor = formatColor(data.textfont, 1, len);
238273
params.textSize = formatParam(data.textfont.size, len, Lib.identity, 12);
239274
params.textFont = data.textfont.family; // arrayOk === false
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data": [
3+
{
4+
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
5+
"y": [0, 1, 0, 1, 0, 1, 0, 1, 0],
6+
"z": [0, 1, 2, 3, 4, 5, 6, 7, 8],
7+
"type": "scatter3d",
8+
"mode":"lines+markers+text",
9+
"text": ["middle center", "bottom", "right", "bottom right", "bottom left", "left", "top right", "top", "top left"],
10+
"textposition": ["", "bottom", "right", "bottom right", "bottom left", "left", "top right", "top", "top left"]
11+
}
12+
],
13+
"layout": {
14+
"title":"Texts in scatter3d could be aligned differently using arrays",
15+
"width": 800,
16+
"height": 600,
17+
"scene":{
18+
"camera":{
19+
"eye":{ "x":-1.25,"y":1.25,"z":1.25 },
20+
"center":{ "x":0,"y":0,"z":0 },
21+
"up":{ "x":0,"y":0,"z":1 }
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)