@@ -33,32 +33,36 @@ function convertStyle(gd, trace) {
33
33
var i ;
34
34
35
35
var opts = {
36
- text : undefined ,
37
36
marker : undefined ,
37
+ markerSel : undefined ,
38
+ markerUnsel : undefined ,
38
39
line : undefined ,
39
40
fill : undefined ,
40
41
errorX : undefined ,
41
42
errorY : undefined ,
42
- selected : undefined ,
43
- unselected : undefined
43
+ text : undefined ,
44
+ textSel : undefined ,
45
+ textUnsel : undefined
44
46
} ;
45
47
46
48
if ( trace . visible !== true ) return opts ;
47
49
48
50
if ( subTypes . hasText ( trace ) ) {
49
- opts . text = convertTextfont ( trace , trace . textfont ) ;
51
+ opts . text = convertTextStyle ( trace ) ;
52
+ opts . textSel = convertTextSelection ( trace , trace . selected ) ;
53
+ opts . textUnsel = convertTextSelection ( trace , trace . unselected ) ;
50
54
}
51
55
52
56
if ( subTypes . hasMarkers ( trace ) ) {
53
57
opts . marker = convertMarkerStyle ( trace ) ;
54
- opts . selected = convertMarkerSelection ( trace , trace . selected ) ;
55
- opts . unselected = convertMarkerSelection ( trace , trace . unselected ) ;
58
+ opts . markerSel = convertMarkerSelection ( trace , trace . selected ) ;
59
+ opts . markerUnsel = convertMarkerSelection ( trace , trace . unselected ) ;
56
60
57
61
if ( ! trace . unselected && Array . isArray ( trace . marker . opacity ) ) {
58
62
var mo = trace . marker . opacity ;
59
- opts . unselected . opacity = new Array ( mo . length ) ;
63
+ opts . markerUnsel . opacity = new Array ( mo . length ) ;
60
64
for ( i = 0 ; i < mo . length ; i ++ ) {
61
- opts . unselected . opacity [ i ] = DESELECTDIM * mo [ i ] ;
65
+ opts . markerUnsel . opacity [ i ] = DESELECTDIM * mo [ i ] ;
62
66
}
63
67
}
64
68
}
@@ -97,135 +101,75 @@ function convertStyle(gd, trace) {
97
101
return opts ;
98
102
}
99
103
100
- function convertTextfont ( trace , textfont ) {
101
- var textOptions = { } , i ;
102
-
103
- textOptions . color = textfont . color ;
104
-
105
- textOptions . align = [ ] ;
106
- textOptions . baseline = [ ] ;
104
+ function convertTextStyle ( trace ) {
105
+ var count = trace . _length ;
106
+ var textfontIn = trace . textfont ;
107
+ var textpositionIn = trace . textposition ;
108
+ var textPos = Array . isArray ( textpositionIn ) ? textpositionIn : [ textpositionIn ] ;
109
+ var tfc = textfontIn . color ;
110
+ var tfs = textfontIn . size ;
111
+ var tff = textfontIn . family ;
112
+ var optsOut = { } ;
113
+ var i ;
107
114
108
- var textposition = Array . isArray ( trace . textposition ) ? trace . textposition : [ trace . textposition ] ;
115
+ optsOut . text = trace . text ;
116
+ optsOut . opacity = trace . opacity ;
117
+ optsOut . font = { } ;
118
+ optsOut . align = [ ] ;
119
+ optsOut . baseline = [ ] ;
109
120
110
- for ( i = 0.0 ; i < textposition . length ; i ++ ) {
111
- var textpos = textposition [ i ] . split ( / \s + / ) ;
121
+ for ( i = 0 ; i < textPos . length ; i ++ ) {
122
+ var tp = textPos [ i ] . split ( / \s + / ) ;
112
123
113
- switch ( textpos [ 1 ] ) {
124
+ switch ( tp [ 1 ] ) {
114
125
case 'left' :
115
- textOptions . align . push ( 'right' ) ;
126
+ optsOut . align . push ( 'right' ) ;
116
127
break ;
117
128
case 'right' :
118
- textOptions . align . push ( 'left' ) ;
129
+ optsOut . align . push ( 'left' ) ;
119
130
break ;
120
131
default :
121
- textOptions . align . push ( textpos [ 1 ] ) ;
132
+ optsOut . align . push ( tp [ 1 ] ) ;
122
133
}
123
-
124
- switch ( textpos [ 0 ] ) {
134
+ switch ( tp [ 0 ] ) {
125
135
case 'top' :
126
- textOptions . baseline . push ( 'bottom' ) ;
136
+ optsOut . baseline . push ( 'bottom' ) ;
127
137
break ;
128
138
case 'bottom' :
129
- textOptions . baseline . push ( 'top' ) ;
139
+ optsOut . baseline . push ( 'top' ) ;
130
140
break ;
131
141
default :
132
- textOptions . baseline . push ( textpos [ 0 ] ) ;
142
+ optsOut . baseline . push ( tp [ 0 ] ) ;
133
143
}
134
144
}
135
145
136
- // [{family, color, size}, {family, color, size}, ...] →
137
- // {family: [], color: [], size: []}
138
- if ( Array . isArray ( textfont ) ) {
139
- textOptions . font = [ ] ;
140
- textOptions . color = [ ] ;
141
- for ( i = 0 ; i < textfont . length ; i ++ ) {
142
- textOptions . font . push ( {
143
- family : textfont [ i ] . family ,
144
- size : textfont [ i ] . size
145
- } ) ;
146
- textOptions . color . push ( textfont [ i ] . color ) ;
147
- }
148
- }
149
- else {
150
- // if any textfont param is array - make render a batch
151
- if ( Array . isArray ( textfont . family ) || Array . isArray ( textfont . size ) ) {
152
- textOptions . font = Array ( Math . max (
153
- textfont . family && textfont . family . length || 1 ,
154
- textfont . size && textfont . size . length || 1
155
- ) ) ;
156
-
157
- for ( i = 0 ; i < textOptions . font . length ; i ++ ) {
158
- textOptions . font [ i ] = {
159
- family : textfont . family [ i ] || textfont . family ,
160
- size : textfont . size [ i ] || textfont . size
161
- } ;
162
- }
163
- }
164
- // if both are single values, make render fast single-value
165
- else {
166
- textOptions . font = {
167
- family : textfont . family ,
168
- size : textfont . size
169
- } ;
146
+ if ( Array . isArray ( tfc ) ) {
147
+ optsOut . color = new Array ( count ) ;
148
+ for ( i = 0 ; i < count ; i ++ ) {
149
+ optsOut . color [ i ] = tfc [ i ] ;
170
150
}
171
- textOptions . color = textfont . color ;
151
+ } else {
152
+ optsOut . color = tfc ;
172
153
}
173
154
174
- // corresponds to textPointPosition from component.drawing
175
- if ( trace . marker ) {
176
- var sizes = [ ] ;
177
- if ( Array . isArray ( trace . marker . size ) ) {
178
- for ( i = 0 ; i < trace . marker . size . length ; i ++ ) {
179
- sizes . push ( trace . marker . size [ i ] ) ;
180
- }
181
- }
182
- else if ( Array . isArray ( trace . marker ) ) {
183
- for ( i = 0 ; i < trace . marker . length ; i ++ ) {
184
- sizes . push ( trace . marker [ i ] . size ) ;
185
- }
186
- }
187
- else {
188
- sizes . push ( trace . marker . size ) ;
189
- }
190
-
191
- textOptions . offset = [ ] ;
192
- for ( i = 0 ; i < Math . max ( trace . x . length , trace . y . length ) ; i ++ ) {
193
- var size = sizes . length > 1 ? sizes [ i ] : sizes [ 0 ] ;
194
- var markerRadius = size / 2 ;
195
- var fontSize = Array . isArray ( textOptions . font ) ? textOptions . font [ i ] . size : textOptions . font . size ;
196
- var align = Array . isArray ( textOptions . align ) ? textOptions . align . length > 1 ? textOptions . align [ i ] : textOptions . align [ 0 ] : textOptions . align ;
197
- var baseline = Array . isArray ( textOptions . baseline ) ? textOptions . baseline . length > 1 ? textOptions . baseline [ i ] : textOptions . baseline [ 0 ] : textOptions . baseline ;
198
- var hSign = TEXTOFFSETSIGN [ align ] ;
199
- var vSign = TEXTOFFSETSIGN [ baseline ] ;
200
- var xPad = markerRadius ? markerRadius / 0.8 + 1 : 0 ;
201
- var yPad = - vSign * xPad - vSign * 0.5 ;
202
- textOptions . offset . push (
203
- [ hSign * xPad / fontSize , yPad / fontSize ]
204
- ) ;
205
- }
206
- }
155
+ if ( Array . isArray ( tfs ) || Array . isArray ( tff ) ) {
156
+ // if any textfont param is array - make render a batch
157
+ optsOut . font = new Array ( count ) ;
158
+ for ( i = 0 ; i < count ; i ++ ) {
159
+ var fonti = optsOut . font [ i ] = { } ;
207
160
208
- textOptions . position = [ ] ;
209
- for ( i = 0 ; i < trace . x . length ; i ++ ) {
210
- textOptions . position . push ( trace . x [ i ] , trace . y [ i ] ) ;
211
- }
212
- textOptions . text = trace . text ;
161
+ fonti . size = Array . isArray ( tfs ) ?
162
+ ( isNumeric ( tfs [ i ] ) ? tfs [ i ] : 0 ) :
163
+ tfs ;
213
164
214
- // filter out bad font sizes
215
- if ( Array . isArray ( textOptions . font ) ) {
216
- for ( i = 0 ; i < textOptions . font . length ; i ++ ) {
217
- if ( ! isNumeric ( textOptions . font [ i ] . size ) ) {
218
- textOptions . font [ i ] . size = 0 ;
219
- }
220
- }
221
- }
222
- else {
223
- if ( ! isNumeric ( textOptions . font . size ) ) {
224
- textOptions . font . size = 0 ;
165
+ fonti . family = Array . isArray ( tff ) ? tff [ i ] : tff ;
225
166
}
167
+ } else {
168
+ // if both are single values, make render fast single-value
169
+ optsOut . font = { size : tfs , family : tff } ;
226
170
}
227
171
228
- return textOptions ;
172
+ return optsOut ;
229
173
}
230
174
231
175
@@ -360,13 +304,30 @@ function convertMarkerSelection(trace, target) {
360
304
if ( target . marker && target . marker . symbol ) {
361
305
optsOut = convertMarkerStyle ( Lib . extendFlat ( { } , optsIn , target . marker ) ) ;
362
306
} else if ( target . marker ) {
363
- if ( target . marker . size ) optsOut . sizes = target . marker . size / 2 ;
307
+ if ( target . marker . size ) optsOut . size = target . marker . size / 2 ;
364
308
if ( target . marker . color ) optsOut . colors = target . marker . color ;
365
309
if ( target . marker . opacity !== undefined ) optsOut . opacity = target . marker . opacity ;
366
310
}
367
311
312
+ return optsOut ;
313
+ }
314
+
315
+ function convertTextSelection ( trace , target ) {
316
+ var optsOut = { } ;
317
+
318
+ if ( ! target ) return optsOut ;
319
+
368
320
if ( target . textfont ) {
369
- optsOut . textfont = convertTextfont ( trace , target . textfont ) ;
321
+ var optsIn = {
322
+ opacity : 1 ,
323
+ text : trace . text ,
324
+ textposition : trace . textposition ,
325
+ textfont : Lib . extendFlat ( { } , trace . textfont )
326
+ } ;
327
+ if ( target . textfont ) {
328
+ Lib . extendFlat ( optsIn . textfont , target . textfont ) ;
329
+ }
330
+ optsOut = convertTextStyle ( optsIn ) ;
370
331
}
371
332
372
333
return optsOut ;
@@ -564,10 +525,47 @@ function convertErrorBarPositions(gd, trace, positions, x, y) {
564
525
return out ;
565
526
}
566
527
528
+ function convertTextPosition ( gd , trace , textOpts , markerOpts ) {
529
+ var count = trace . _length ;
530
+ var out = { } ;
531
+ var i ;
532
+
533
+ // corresponds to textPointPosition from component.drawing
534
+ if ( subTypes . hasMarkers ( trace ) ) {
535
+ var fontOpts = textOpts . font ;
536
+ var align = textOpts . align ;
537
+ var baseline = textOpts . baseline ;
538
+ out . offset = new Array ( count ) ;
539
+
540
+ for ( i = 0 ; i < count ; i ++ ) {
541
+ var ms = markerOpts . sizes ? markerOpts . sizes [ i ] : markerOpts . size ;
542
+ var fs = Array . isArray ( fontOpts ) ? fontOpts [ i ] . size : fontOpts . size ;
543
+
544
+ var a = Array . isArray ( align ) ?
545
+ ( align . length > 1 ? align [ i ] : align [ 0 ] ) :
546
+ align ;
547
+ var b = Array . isArray ( baseline ) ?
548
+ ( baseline . length > 1 ? baseline [ i ] : baseline [ 0 ] ) :
549
+ baseline ;
550
+
551
+ var hSign = TEXTOFFSETSIGN [ a ] ;
552
+ var vSign = TEXTOFFSETSIGN [ b ] ;
553
+ var xPad = ms ? ms / 0.8 + 1 : 0 ;
554
+ var yPad = - vSign * xPad - vSign * 0.5 ;
555
+ out . offset [ i ] = [ hSign * xPad / fs , yPad / fs ] ;
556
+ }
557
+ }
558
+
559
+ return out ;
560
+ }
561
+
567
562
module . exports = {
568
- convertStyle : convertStyle ,
569
- convertMarkerStyle : convertMarkerStyle ,
570
- convertMarkerSelection : convertMarkerSelection ,
571
- convertLinePositions : convertLinePositions ,
572
- convertErrorBarPositions : convertErrorBarPositions
563
+ style : convertStyle ,
564
+
565
+ markerStyle : convertMarkerStyle ,
566
+ markerSelection : convertMarkerSelection ,
567
+
568
+ linePositions : convertLinePositions ,
569
+ errorBarPositions : convertErrorBarPositions ,
570
+ textPosition : convertTextPosition
573
571
} ;
0 commit comments