@@ -33,20 +33,24 @@ function convertStyle(gd, trace) {
3333 var i ;
3434
3535 var opts = {
36- text : undefined ,
3736 marker : undefined ,
3837 markerSel : undefined ,
3938 markerUnsel : undefined ,
4039 line : undefined ,
4140 fill : undefined ,
4241 errorX : undefined ,
4342 errorY : undefined ,
43+ text : undefined ,
44+ textSel : undefined ,
45+ textUnsel : undefined
4446 } ;
4547
4648 if ( trace . visible !== true ) return opts ;
4749
4850 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 ) ;
5054 }
5155
5256 if ( subTypes . hasMarkers ( trace ) ) {
@@ -97,135 +101,75 @@ function convertStyle(gd, trace) {
97101 return opts ;
98102}
99103
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 ;
107114
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 = [ ] ;
109120
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 + / ) ;
112123
113- switch ( textpos [ 1 ] ) {
124+ switch ( tp [ 1 ] ) {
114125 case 'left' :
115- textOptions . align . push ( 'right' ) ;
126+ optsOut . align . push ( 'right' ) ;
116127 break ;
117128 case 'right' :
118- textOptions . align . push ( 'left' ) ;
129+ optsOut . align . push ( 'left' ) ;
119130 break ;
120131 default :
121- textOptions . align . push ( textpos [ 1 ] ) ;
132+ optsOut . align . push ( tp [ 1 ] ) ;
122133 }
123-
124- switch ( textpos [ 0 ] ) {
134+ switch ( tp [ 0 ] ) {
125135 case 'top' :
126- textOptions . baseline . push ( 'bottom' ) ;
136+ optsOut . baseline . push ( 'bottom' ) ;
127137 break ;
128138 case 'bottom' :
129- textOptions . baseline . push ( 'top' ) ;
139+ optsOut . baseline . push ( 'top' ) ;
130140 break ;
131141 default :
132- textOptions . baseline . push ( textpos [ 0 ] ) ;
142+ optsOut . baseline . push ( tp [ 0 ] ) ;
133143 }
134144 }
135145
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 ] ;
170150 }
171- textOptions . color = textfont . color ;
151+ } else {
152+ optsOut . color = tfc ;
172153 }
173154
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 ] = { } ;
207160
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 ;
213164
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 ;
225166 }
167+ } else {
168+ // if both are single values, make render fast single-value
169+ optsOut . font = { size : tfs , family : tff } ;
226170 }
227171
228- return textOptions ;
172+ return optsOut ;
229173}
230174
231175
@@ -581,6 +525,40 @@ function convertErrorBarPositions(gd, trace, positions, x, y) {
581525 return out ;
582526}
583527
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+
584562module . exports = {
585563 style : convertStyle ,
586564
@@ -589,4 +567,5 @@ module.exports = {
589567
590568 linePositions : convertLinePositions ,
591569 errorBarPositions : convertErrorBarPositions ,
570+ textPosition : convertTextPosition
592571} ;
0 commit comments