@@ -98,74 +98,137 @@ function convertStyle(gd, trace) {
98
98
}
99
99
100
100
function convertTextfont ( trace , textfont ) {
101
- var opts = [ ] , i ;
101
+ var textOptions = { } , i ;
102
102
103
- for ( i = 0 ; i < trace . text . length ; i ++ ) {
104
- var textOptions = opts [ i ] = { } ;
103
+ textOptions . color = textfont . color ;
105
104
106
- textOptions . color = unarr ( textfont . color , i ) ;
105
+ textOptions . align = [ ] ;
106
+ textOptions . baseline = [ ] ;
107
+
108
+ var textposition = Array . isArray ( trace . textposition ) ? trace . textposition : [ trace . textposition ] ;
109
+
110
+ for ( i = 0.0 ; i < textposition . length ; i ++ ) {
111
+ var textpos = textposition [ i ] . split ( / \s + / ) ;
107
112
108
- var textpos = unarr ( trace . textposition , i ) ;
109
- textpos = textpos . split ( / \s + / ) ;
110
113
switch ( textpos [ 1 ] ) {
111
114
case 'left' :
112
- textOptions . align = 'right' ;
115
+ textOptions . align . push ( 'right' ) ;
113
116
break ;
114
117
case 'right' :
115
- textOptions . align = 'left' ;
118
+ textOptions . align . push ( 'left' ) ;
116
119
break ;
117
120
default :
118
- textOptions . align = textpos [ 1 ] ;
121
+ textOptions . align . push ( textpos [ 1 ] ) ;
119
122
}
120
123
121
124
switch ( textpos [ 0 ] ) {
122
125
case 'top' :
123
- textOptions . baseline = 'bottom' ;
126
+ textOptions . baseline . push ( 'bottom' ) ;
124
127
break ;
125
128
case 'bottom' :
126
- textOptions . baseline = 'top' ;
129
+ textOptions . baseline . push ( 'top' ) ;
127
130
break ;
128
131
default :
129
- textOptions . baseline = textpos [ 0 ] ;
132
+ textOptions . baseline . push ( textpos [ 0 ] ) ;
130
133
}
134
+ }
131
135
132
- textfont = unarr ( textfont , i ) ;
133
-
134
- var fontSize = unarr ( textfont . size , i ) ;
135
-
136
- if ( ! isNumeric ( fontSize ) ) {
137
- continue ;
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 ) ;
138
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
+ } ;
170
+ }
171
+ textOptions . color = textfont . color ;
172
+ }
139
173
140
- textOptions . font = {
141
- family : unarr ( textfont . family , i ) ,
142
- size : fontSize
143
- } ;
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
+ }
144
190
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 ;
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 [ i ] : textOptions . align ;
197
+ var baseline = Array . isArray ( textOptions . baseline ) ? textOptions . baseline [ i ] : textOptions . baseline ;
198
+ var hSign = TEXTOFFSETSIGN [ align ] ;
199
+ var vSign = TEXTOFFSETSIGN [ baseline ] ;
149
200
var xPad = markerRadius ? markerRadius / 0.8 + 1 : 0 ;
150
- var vSign = TEXTOFFSETSIGN [ textOptions . baseline ] ;
151
201
var yPad = - vSign * xPad - vSign * 0.5 ;
152
- textOptions . offset = [ hSign * xPad / fontSize , yPad / fontSize ] ;
153
- }
154
202
155
- textOptions . position = [ unarr ( trace . x , i ) , unarr ( trace . y , i ) ] ;
203
+ textOptions . offset . push (
204
+ [ hSign * xPad / fontSize , yPad / fontSize ]
205
+ ) ;
206
+ }
207
+ }
156
208
157
- textOptions . text = unarr ( trace . text , i ) ;
209
+ textOptions . position = [ ] ;
210
+ for ( i = 0 ; i < trace . x . length ; i ++ ) {
211
+ textOptions . position . push ( trace . x [ i ] , trace . y [ i ] ) ;
212
+ }
213
+ textOptions . text = trace . text ;
158
214
215
+ // filter out bad font sizes
216
+ if ( Array . isArray ( textOptions . font ) ) {
217
+ for ( i = 0 ; i < textOptions . font . length ; i ++ ) {
218
+ if ( ! isNumeric ( textOptions . font [ i ] . size ) ) {
219
+ textOptions . font [ i ] . size = 0 ;
220
+ }
221
+ }
222
+ }
223
+ else {
224
+ if ( ! isNumeric ( textOptions . font . size ) ) {
225
+ textOptions . font . size = 0 ;
226
+ }
159
227
}
160
228
161
- return opts ;
229
+ return textOptions ;
162
230
}
163
231
164
- // FIXME: find proper util method for this
165
- function unarr ( obj , i ) {
166
- if ( Array . isArray ( obj ) ) return obj [ i ] ;
167
- return obj ;
168
- }
169
232
170
233
function convertMarkerStyle ( trace ) {
171
234
var count = trace . _length ;
0 commit comments