@@ -21,6 +21,8 @@ var ErrorBars = require('../../components/errorbars');
21
21
22
22
var arraysToCalcdata = require ( './arrays_to_calcdata' ) ;
23
23
24
+ // padding in pixels around text
25
+ var TEXTPAD = 3 ;
24
26
25
27
module . exports = function plot ( gd , plotinfo , cdbar ) {
26
28
var xa = plotinfo . xaxis ,
@@ -147,6 +149,8 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
147
149
text = traceText ;
148
150
}
149
151
152
+ if ( ! text ) return ;
153
+
150
154
// get text position
151
155
var traceTextPosition = trace . textposition ,
152
156
textPosition ;
@@ -160,21 +164,63 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
160
164
161
165
if ( textPosition === 'none' ) return ;
162
166
163
- var barWidth = Math . abs ( x1 - x0 ) ,
164
- barHeight = Math . abs ( y1 - y0 ) ,
165
- barIsTooSmall = ( barWidth < 8 || barHeight < 8 ) ,
167
+ // get text font
168
+ var traceTextFont = trace . textfont ,
169
+ textFont = ( Array . isArray ( traceTextFont ) ) ?
170
+ traceTextFont [ i ] : traceTextFont ;
171
+ textFont = textFont || gd . _fullLayout . font ;
172
+
173
+ // get outside text font
174
+ var traceOutsideTextFont = trace . outsidetextfont ,
175
+ outsideTextFont = ( Array . isArray ( traceOutsideTextFont ) ) ?
176
+ traceOutsideTextFont [ i ] : traceOutsideTextFont ;
177
+ outsideTextFont = outsideTextFont || textFont ;
178
+
179
+ // get inside text font
180
+ var traceInsideTextFont = trace . insidetextfont ,
181
+ insideTextFont = ( Array . isArray ( traceInsideTextFont ) ) ?
182
+ traceInsideTextFont [ i ] : traceInsideTextFont ;
183
+ insideTextFont = insideTextFont || textFont ;
184
+
185
+ // append text node
186
+ function appendTextNode ( bar , text , textFont ) {
187
+ var textSelection = bar . append ( 'text' )
188
+ // prohibit tex interpretation until we can handle
189
+ // tex and regular text together
190
+ . attr ( 'data-notex' , 1 )
191
+ . text ( text )
192
+ . attr ( {
193
+ 'class' : 'bartext' ,
194
+ transform : '' ,
195
+ 'data-bb' : '' ,
196
+ 'text-anchor' : 'middle' ,
197
+ x : 0 ,
198
+ y : 0
199
+ } )
200
+ . call ( Drawing . font , textFont ) ;
201
+
202
+ textSelection . call ( svgTextUtils . convertToTspans ) ;
203
+ textSelection . selectAll ( 'tspan.line' ) . attr ( { x : 0 , y : 0 } ) ;
204
+
205
+ return textSelection ;
206
+ }
166
207
167
- barmode = gd . _fullLayout . barmode ,
208
+ var barmode = gd . _fullLayout . barmode ,
168
209
inStackMode = ( barmode === 'stack' ) ,
169
210
inRelativeMode = ( barmode === 'relative' ) ,
170
211
inStackOrRelativeMode = inStackMode || inRelativeMode ,
171
212
172
213
calcBar = calcTrace [ i ] ,
173
- isOutmostBar = ! inStackOrRelativeMode || calcBar . _outmost ;
214
+ isOutmostBar = ! inStackOrRelativeMode || calcBar . _outmost ,
174
215
175
- if ( textPosition === 'auto' ) {
176
- textPosition = ( barIsTooSmall && isOutmostBar ) ? 'outside' : 'inside' ;
177
- }
216
+ barWidth = Math . abs ( x1 - x0 ) - 2 * TEXTPAD , // padding excluded
217
+ barHeight = Math . abs ( y1 - y0 ) - 2 * TEXTPAD , // padding excluded
218
+ barIsTooSmall = ( barWidth <= 0 || barHeight <= 0 ) ,
219
+
220
+ textSelection ,
221
+ textBB ,
222
+ textWidth ,
223
+ textHeight ;
178
224
179
225
if ( textPosition === 'outside' ) {
180
226
if ( ! isOutmostBar ) textPosition = 'inside' ;
@@ -184,72 +230,46 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
184
230
if ( barIsTooSmall ) return ;
185
231
}
186
232
187
-
188
- // get text font
189
- var textFont ;
190
-
191
- if ( textPosition === 'outside' ) {
192
- var traceOutsideTextFont = trace . outsidetextfont ;
193
- if ( Array . isArray ( traceOutsideTextFont ) ) {
194
- if ( i >= traceOutsideTextFont . length ) return ;
195
- textFont = traceOutsideTextFont [ i ] ;
196
- }
197
- else {
198
- textFont = traceOutsideTextFont ;
199
- }
200
- }
201
- else {
202
- var traceInsideTextFont = trace . insidetextfont ;
203
- if ( Array . isArray ( traceInsideTextFont ) ) {
204
- if ( i >= traceInsideTextFont . length ) return ;
205
- textFont = traceInsideTextFont [ i ] ;
206
- }
207
- else {
208
- textFont = traceInsideTextFont ;
209
- }
210
- }
211
-
212
- if ( ! textFont ) {
213
- var traceTextFont = trace . textfont ;
214
- if ( Array . isArray ( traceTextFont ) ) {
215
- if ( i >= traceTextFont . length ) return ;
216
- textFont = traceTextFont [ i ] ;
217
- }
218
- else {
219
- textFont = traceTextFont ;
233
+ if ( textPosition === 'auto' ) {
234
+ if ( isOutmostBar ) {
235
+ // draw text using insideTextFont and check if it fits inside bar
236
+ textSelection = appendTextNode ( bar , text , insideTextFont ) ;
237
+
238
+ textBB = Drawing . bBox ( textSelection . node ( ) ) ,
239
+ textWidth = textBB . width ,
240
+ textHeight = textBB . height ;
241
+
242
+ var textHasSize = ( textWidth > 0 && textHeight > 0 ) ,
243
+ fitsInside =
244
+ ( textWidth <= barWidth && textHeight <= barHeight ) ,
245
+ fitsInsideIfRotated =
246
+ ( textWidth <= barHeight && textHeight <= barWidth ) ;
247
+ if ( textHasSize && ( fitsInside || fitsInsideIfRotated ) ) {
248
+ textPosition = 'inside' ;
249
+ }
250
+ else {
251
+ textPosition = 'outside' ;
252
+ textSelection . remove ( ) ;
253
+ textSelection = null ;
254
+ }
220
255
}
256
+ else if ( ! barIsTooSmall ) textPosition = 'inside' ;
257
+ else return ;
221
258
}
222
259
223
- if ( ! textFont ) {
224
- textFont = gd . _fullLayout . font ;
225
- }
260
+ if ( ! textSelection ) {
261
+ textSelection = appendTextNode ( bar , text ,
262
+ ( textPosition === 'outside' ) ?
263
+ outsideTextFont : insideTextFont ) ;
226
264
227
- // append bar text
228
- var textSelection = bar . append ( 'text' )
229
- // prohibit tex interpretation until we can handle
230
- // tex and regular text together
231
- . attr ( 'data-notex' , 1 )
232
- . text ( text )
233
- . attr ( {
234
- 'class' : 'bartext' ,
235
- transform : '' ,
236
- 'data-bb' : '' ,
237
- 'text-anchor' : 'middle' ,
238
- x : 0 ,
239
- y : 0
240
- } )
241
- . call ( Drawing . font , textFont ) ;
242
-
243
- textSelection . call ( svgTextUtils . convertToTspans ) ;
244
- textSelection . selectAll ( 'tspan.line' ) . attr ( { x : 0 , y : 0 } ) ;
245
-
246
- // position bar text
247
- var textBB = Drawing . bBox ( textSelection . node ( ) ) ,
265
+ textBB = Drawing . bBox ( textSelection . node ( ) ) ,
248
266
textWidth = textBB . width ,
249
267
textHeight = textBB . height ;
250
- if ( ! textWidth || ! textHeight ) {
251
- textSelection . remove ( ) ;
252
- return ;
268
+
269
+ if ( textWidth <= 0 || textHeight <= 0 ) {
270
+ textSelection . remove ( ) ;
271
+ return ;
272
+ }
253
273
}
254
274
255
275
// compute translate transform
@@ -277,16 +297,15 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB) {
277
297
textY = ( textBB . top + textBB . bottom ) / 2 ;
278
298
279
299
// apply 3px target padding
280
- var targetWidth = barWidth - 6 ,
281
- targetHeight = barHeight - 6 ;
300
+ var targetWidth = barWidth - 2 * TEXTPAD ,
301
+ targetHeight = barHeight - 2 * TEXTPAD ;
282
302
283
303
return getTransform (
284
304
textX , textY , textWidth , textHeight ,
285
305
barX , barY , targetWidth , targetHeight ) ;
286
306
}
287
307
288
308
function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , orientation ) {
289
-
290
309
// compute text and target positions
291
310
var textWidth = textBB . width ,
292
311
textHeight = textBB . height ,
@@ -298,14 +317,14 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
298
317
if ( orientation === 'h' ) {
299
318
if ( x1 < x0 ) {
300
319
// bar end is on the left hand side
301
- targetWidth = textWidth + 6 ; // 3px padding included
302
- targetHeight = Math . abs ( y1 - y0 ) - 6 ;
320
+ targetWidth = textWidth + 2 * TEXTPAD ; // padding included
321
+ targetHeight = Math . abs ( y1 - y0 ) - 2 * TEXTPAD ;
303
322
targetX = x1 - targetWidth / 2 ;
304
323
targetY = ( y0 + y1 ) / 2 ;
305
324
}
306
325
else {
307
- targetWidth = textWidth + 6 ; // padding included
308
- targetHeight = Math . abs ( y1 - y0 ) - 6 ;
326
+ targetWidth = textWidth + 2 * TEXTPAD ; // padding included
327
+ targetHeight = Math . abs ( y1 - y0 ) - 2 * TEXTPAD ;
309
328
targetX = x1 + targetWidth / 2 ;
310
329
targetY = ( y0 + y1 ) / 2 ;
311
330
}
0 commit comments