Skip to content

Commit 90479a7

Browse files
committed
Make text selection work
1 parent 4d67fa4 commit 90479a7

File tree

4 files changed

+101
-64
lines changed

4 files changed

+101
-64
lines changed

package-lock.json

Lines changed: 3 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
@@ -83,7 +83,7 @@
8383
"gl-select-box": "^1.0.2",
8484
"gl-spikes2d": "^1.0.1",
8585
"gl-surface3d": "^1.3.5",
86-
"gl-text": "^1.0.2",
86+
"gl-text": "^1.0.3",
8787
"glslify": "^6.1.1",
8888
"has-hover": "^1.0.1",
8989
"has-passive-events": "^1.0.0",

src/traces/scattergl/convert.js

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,61 +46,7 @@ function convertStyle(gd, trace) {
4646
if(trace.visible !== true) return opts;
4747

4848
if(subTypes.hasText(trace)) {
49-
opts.text = [];
50-
51-
for(i = 0; i < trace.text.length; i++) {
52-
var textOptions = opts.text[i] = {};
53-
54-
var textfont = unarr(trace.textfont, i);
55-
var fontSize = unarr(textfont.size, i);
56-
if(!isNumeric(fontSize)) continue;
57-
58-
textOptions.color = unarr(textfont.color, i);
59-
60-
textOptions.font = {
61-
family: unarr(textfont.family, i),
62-
size: fontSize
63-
};
64-
65-
66-
var textpos = unarr(trace.textposition, i);
67-
textpos = textpos.split(/\s+/);
68-
switch(textpos[1]) {
69-
case 'left':
70-
textOptions.align = 'right';
71-
break;
72-
case 'right':
73-
textOptions.align = 'left';
74-
break;
75-
default:
76-
textOptions.align = textpos[1];
77-
}
78-
79-
switch(textpos[0]) {
80-
case 'top':
81-
textOptions.baseline = 'bottom';
82-
break;
83-
case 'bottom':
84-
textOptions.baseline = 'top';
85-
break;
86-
default:
87-
textOptions.baseline = textpos[0];
88-
}
89-
90-
// corresponds to textPointPosition from component.drawing
91-
if(trace.marker) {
92-
var hSign = TEXTOFFSETSIGN[textOptions.align];
93-
var markerRadius = unarr(unarr(trace.marker, i).size, i) / 2;
94-
var xPad = markerRadius ? markerRadius / 0.8 + 1 : 0;
95-
var vSign = TEXTOFFSETSIGN[textOptions.baseline];
96-
var yPad = - vSign * xPad - vSign * 0.5;
97-
textOptions.offset = [hSign * xPad / fontSize, yPad / fontSize];
98-
}
99-
100-
textOptions.position = [unarr(trace.x, i), unarr(trace.y, i)];
101-
102-
textOptions.text = unarr(trace.text, i);
103-
}
49+
opts.text = convertTextfont(trace, trace.textfont);
10450
}
10551

10652
if(subTypes.hasMarkers(trace)) {
@@ -151,6 +97,69 @@ function convertStyle(gd, trace) {
15197
return opts;
15298
}
15399

100+
function convertTextfont(trace, textfont) {
101+
var opts = [], i;
102+
103+
for(i = 0; i < trace.text.length; i++) {
104+
var textOptions = opts[i] = {};
105+
106+
textOptions.color = unarr(textfont.color, i);
107+
108+
var textpos = unarr(trace.textposition, i);
109+
textpos = textpos.split(/\s+/);
110+
switch(textpos[1]) {
111+
case 'left':
112+
textOptions.align = 'right';
113+
break;
114+
case 'right':
115+
textOptions.align = 'left';
116+
break;
117+
default:
118+
textOptions.align = textpos[1];
119+
}
120+
121+
switch(textpos[0]) {
122+
case 'top':
123+
textOptions.baseline = 'bottom';
124+
break;
125+
case 'bottom':
126+
textOptions.baseline = 'top';
127+
break;
128+
default:
129+
textOptions.baseline = textpos[0];
130+
}
131+
132+
textfont = unarr(textfont, i);
133+
134+
var fontSize = unarr(textfont.size, i);
135+
136+
if(!isNumeric(fontSize)) {
137+
continue;
138+
}
139+
140+
textOptions.font = {
141+
family: unarr(textfont.family, i),
142+
size: fontSize
143+
};
144+
145+
// corresponds to textPointPosition from component.drawing
146+
if(trace.marker) {
147+
var hSign = TEXTOFFSETSIGN[textOptions.align];
148+
var markerRadius = unarr(unarr(trace.marker, i).size, i) / 2;
149+
var xPad = markerRadius ? markerRadius / 0.8 + 1 : 0;
150+
var vSign = TEXTOFFSETSIGN[textOptions.baseline];
151+
var yPad = - vSign * xPad - vSign * 0.5;
152+
textOptions.offset = [hSign * xPad / fontSize, yPad / fontSize];
153+
}
154+
155+
textOptions.position = [unarr(trace.x, i), unarr(trace.y, i)];
156+
157+
textOptions.text = unarr(trace.text, i);
158+
159+
}
160+
161+
return opts;
162+
}
154163

155164
// FIXME: find proper util method for this
156165
function unarr(obj, i) {
@@ -294,6 +303,10 @@ function convertMarkerSelection(trace, target) {
294303
if(target.marker.opacity !== undefined) optsOut.opacity = target.marker.opacity;
295304
}
296305

306+
if(target.textfont) {
307+
optsOut.textfont = convertTextfont(trace, target.textfont);
308+
}
309+
297310
return optsOut;
298311
}
299312

src/traces/scattergl/index.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,13 @@ function sceneUpdate(gd, subplot) {
316316
if(scene.error2d) scene.error2d.destroy();
317317
if(scene.line2d) scene.line2d.destroy();
318318
if(scene.select2d) scene.select2d.destroy();
319-
if(scene.glText) {scene.glText.forEach(function(items) {
320-
items.forEach(function(item) {
321-
item.destroy();
319+
if(scene.glText) {
320+
scene.glText.forEach(function(items) {
321+
items.forEach(function(item) {
322+
item.destroy();
323+
});
322324
});
323-
});}
325+
}
324326

325327
scene.textOptions = null;
326328
scene.lineOptions = null;
@@ -826,13 +828,15 @@ function selectPoints(searchInfo, polygon) {
826828

827829
if(!scene) return selection;
828830

829-
var hasOnlyLines = (!subTypes.hasMarkers(trace) && !subTypes.hasText(trace));
831+
var hasText = subTypes.hasText(trace);
832+
var hasOnlyLines = !subTypes.hasMarkers(trace) && !hasText;
830833
if(trace.visible !== true || hasOnlyLines) return selection;
831834

832835
// degenerate polygon does not enable selection
833836
// filter out points by visible scatter ones
834837
var els = null;
835838
var unels = null;
839+
// FIXME: clearing selection does not work here
836840
var i;
837841
if(polygon !== false && !polygon.degenerate) {
838842
els = [], unels = [];
@@ -869,6 +873,26 @@ function selectPoints(searchInfo, polygon) {
869873
scene.scatter2d.update(scene.unselectedOptions);
870874
}
871875

876+
// update texts selection
877+
if(hasText) {
878+
var el, textOptions;
879+
if(els) {
880+
for(i = 0; i < els.length; i++) {
881+
el = els[i];
882+
textOptions = scene.selectedOptions[stash.index].textfont;
883+
if(!textOptions) continue;
884+
scene.glText[stash.index][el].update(textOptions[el]);
885+
}
886+
}
887+
if(unels) {
888+
for(i = 0; i < unels.length; i++) {
889+
el = unels[i];
890+
textOptions = scene.unselectedOptions[stash.index].textfont || scene.textOptions[stash.index][el];
891+
scene.glText[stash.index][el].update(textOptions);
892+
}
893+
}
894+
}
895+
872896
scene.selectBatch[stash.index] = els;
873897
scene.unselectBatch[stash.index] = unels;
874898

0 commit comments

Comments
 (0)