11
11
12
12
var d3 = require ( 'd3' ) ;
13
13
var isNumeric = require ( 'fast-isnumeric' ) ;
14
- var tinycolor = require ( 'tinycolor2' ) ;
15
14
16
15
var Lib = require ( '../../lib' ) ;
17
16
var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
@@ -22,10 +21,9 @@ var Registry = require('../../registry');
22
21
23
22
var attributes = require ( './attributes' ) ,
24
23
attributeText = attributes . text ,
25
- attributeTextPosition = attributes . textposition ,
26
- attributeTextFont = attributes . textfont ,
27
- attributeInsideTextFont = attributes . insidetextfont ,
28
- attributeOutsideTextFont = attributes . outsidetextfont ;
24
+ attributeTextPosition = attributes . textposition ;
25
+ var helpers = require ( './helpers' ) ;
26
+ var style = require ( './style' ) ;
29
27
30
28
// padding in pixels around text
31
29
var TEXTPAD = 3 ;
@@ -177,9 +175,10 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
177
175
return ;
178
176
}
179
177
180
- var textFont = getTextFont ( trace , i , gd . _fullLayout . font ) ,
181
- insideTextFont = getInsideTextFont ( trace , i , textFont , calcTrace [ i ] . mc ) ,
182
- outsideTextFont = getOutsideTextFont ( trace , i , textFont ) ;
178
+ var layoutFont = gd . _fullLayout . font ;
179
+ var barColor = calcTrace [ i ] . mc ;
180
+ var insideTextFont = style . getInsideTextFont ( trace , i , layoutFont , barColor ) ;
181
+ var outsideTextFont = style . getOutsideTextFont ( trace , i , layoutFont ) ;
183
182
184
183
// compute text position
185
184
var barmode = gd . _fullLayout . barmode ,
@@ -429,109 +428,11 @@ function getTransform(textX, textY, targetX, targetY, scale, rotate) {
429
428
}
430
429
431
430
function getText ( trace , index ) {
432
- var value = getValue ( trace . text , index ) ;
433
- return coerceString ( attributeText , value ) ;
431
+ var value = helpers . getValue ( trace . text , index ) ;
432
+ return helpers . coerceString ( attributeText , value ) ;
434
433
}
435
434
436
435
function getTextPosition ( trace , index ) {
437
- var value = getValue ( trace . textposition , index ) ;
438
- return coerceEnumerated ( attributeTextPosition , value ) ;
439
- }
440
-
441
- function getTextFont ( trace , index , defaultValue ) {
442
- return getFontValue (
443
- attributeTextFont , trace . textfont , index , defaultValue ) ;
444
- }
445
-
446
- function getInsideTextFont ( trace , index , defaultFont , barColor ) {
447
- var wouldFallBackToLayoutFont =
448
- ( trace . _input . textfont === undefined || trace . _input . textfont . color === undefined ) ||
449
- ( Array . isArray ( trace . textfont . color ) && trace . textfont . color [ index ] === undefined ) ;
450
- if ( wouldFallBackToLayoutFont ) {
451
- defaultFont = {
452
- color : Color . contrast ( barColor ) ,
453
- family : defaultFont . family ,
454
- size : defaultFont . size
455
- } ;
456
- }
457
-
458
- return getFontValue (
459
- attributeInsideTextFont , trace . insidetextfont , index , defaultFont ) ;
460
- }
461
-
462
- function getOutsideTextFont ( trace , index , defaultValue ) {
463
- return getFontValue (
464
- attributeOutsideTextFont , trace . outsidetextfont , index , defaultValue ) ;
465
- }
466
-
467
- function getFontValue ( attributeDefinition , attributeValue , index , defaultValue ) {
468
- attributeValue = attributeValue || { } ;
469
-
470
- var familyValue = getValue ( attributeValue . family , index ) ,
471
- sizeValue = getValue ( attributeValue . size , index ) ,
472
- colorValue = getValue ( attributeValue . color , index ) ;
473
-
474
- return {
475
- family : coerceString (
476
- attributeDefinition . family , familyValue , defaultValue . family ) ,
477
- size : coerceNumber (
478
- attributeDefinition . size , sizeValue , defaultValue . size ) ,
479
- color : coerceColor (
480
- attributeDefinition . color , colorValue , defaultValue . color )
481
- } ;
482
- }
483
-
484
- function getValue ( arrayOrScalar , index ) {
485
- var value ;
486
- if ( ! Array . isArray ( arrayOrScalar ) ) value = arrayOrScalar ;
487
- else if ( index < arrayOrScalar . length ) value = arrayOrScalar [ index ] ;
488
- return value ;
489
- }
490
-
491
- function coerceString ( attributeDefinition , value , defaultValue ) {
492
- if ( typeof value === 'string' ) {
493
- if ( value || ! attributeDefinition . noBlank ) return value ;
494
- }
495
- else if ( typeof value === 'number' ) {
496
- if ( ! attributeDefinition . strict ) return String ( value ) ;
497
- }
498
-
499
- return ( defaultValue !== undefined ) ?
500
- defaultValue :
501
- attributeDefinition . dflt ;
502
- }
503
-
504
- function coerceEnumerated ( attributeDefinition , value , defaultValue ) {
505
- if ( attributeDefinition . coerceNumber ) value = + value ;
506
-
507
- if ( attributeDefinition . values . indexOf ( value ) !== - 1 ) return value ;
508
-
509
- return ( defaultValue !== undefined ) ?
510
- defaultValue :
511
- attributeDefinition . dflt ;
512
- }
513
-
514
- function coerceNumber ( attributeDefinition , value , defaultValue ) {
515
- if ( isNumeric ( value ) ) {
516
- value = + value ;
517
-
518
- var min = attributeDefinition . min ,
519
- max = attributeDefinition . max ,
520
- isOutOfBounds = ( min !== undefined && value < min ) ||
521
- ( max !== undefined && value > max ) ;
522
-
523
- if ( ! isOutOfBounds ) return value ;
524
- }
525
-
526
- return ( defaultValue !== undefined ) ?
527
- defaultValue :
528
- attributeDefinition . dflt ;
529
- }
530
-
531
- function coerceColor ( attributeDefinition , value , defaultValue ) {
532
- if ( tinycolor ( value ) . isValid ( ) ) return value ;
533
-
534
- return ( defaultValue !== undefined ) ?
535
- defaultValue :
536
- attributeDefinition . dflt ;
436
+ var value = helpers . getValue ( trace . textposition , index ) ;
437
+ return helpers . coerceEnumerated ( attributeTextPosition , value ) ;
537
438
}
0 commit comments