@@ -23,6 +23,7 @@ var svgTextUtils = require('../../lib/svg_text_utils');
23
23
var constants = require ( './constants' ) ;
24
24
var helpers = require ( './helpers' ) ;
25
25
var getPathString = helpers . getPathString ;
26
+ var FROM_TL = require ( '../../constants/alignment' ) . FROM_TL ;
26
27
27
28
28
29
// Shapes are stored in gd.layout.shapes, an array of objects
@@ -604,7 +605,7 @@ function drawLabel(gd, index, options, shapeGroup) {
604
605
shapeGroup . selectAll ( '.shape-label' ) . remove ( ) ;
605
606
606
607
// If no label, return
607
- if ( ! options . label ) return ;
608
+ if ( ! options . label . text ) return ;
608
609
609
610
var labelGroupAttrs = {
610
611
'data-index' : index ,
@@ -743,38 +744,58 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
743
744
// Text position functions differently for lines vs. other shapes
744
745
if ( shapeType === 'line' ) {
745
746
// Set base position for start vs. center vs. end of line (default is 'center')
746
- if ( textPosition . indexOf ( 'start' ) !== - 1 ) {
747
+ if ( textPosition === 'start' ) {
747
748
textx = shapex0 ;
748
749
texty = shapey0 ;
749
- if ( xanchor === 'auto' ) {
750
- if ( textAngle === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'left' : 'right' ;
751
- else xanchor = ( shapex1 >= shapex0 ) ? 'right' : 'left' ;
752
- }
753
- } else if ( textPosition . indexOf ( 'end' ) !== - 1 ) {
750
+ } else if ( textPosition === 'end' ) {
754
751
textx = shapex1 ;
755
752
texty = shapey1 ;
756
- if ( xanchor === 'auto' ) {
757
- if ( textAngle === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'right' : 'left' ;
758
- else xanchor = ( shapex1 >= shapex0 ) ? 'left' : 'right' ;
759
- }
760
753
} else { // Default: center
761
754
textx = ( shapex0 + shapex1 ) / 2 ;
762
755
texty = ( shapey0 + shapey1 ) / 2 ;
763
- if ( xanchor === 'auto' ) xanchor = 'center' ;
756
+ }
757
+
758
+ // Set xanchor if xanchor is 'auto'
759
+ if ( xanchor === 'auto' ) {
760
+ if ( textPosition === 'start' ) {
761
+ if ( textAngle === 'auto' ) {
762
+ if ( shapex1 > shapex0 ) xanchor = 'left' ;
763
+ else if ( shapex1 < shapex0 ) xanchor = 'right' ;
764
+ else xanchor = 'center' ;
765
+ } else {
766
+ if ( shapex1 > shapex0 ) xanchor = 'right' ;
767
+ else if ( shapex1 < shapex0 ) xanchor = 'left' ;
768
+ else xanchor = 'center' ;
769
+ }
770
+ } else if ( textPosition === 'end' ) {
771
+ if ( textAngle === 'auto' ) {
772
+ if ( shapex1 > shapex0 ) xanchor = 'right' ;
773
+ else if ( shapex1 < shapex0 ) xanchor = 'left' ;
774
+ else xanchor = 'center' ;
775
+ } else {
776
+ if ( shapex1 > shapex0 ) xanchor = 'left' ;
777
+ else if ( shapex1 < shapex0 ) xanchor = 'right' ;
778
+ else xanchor = 'center' ;
779
+ }
780
+ } else {
781
+ xanchor = 'center' ;
782
+ }
764
783
}
765
784
766
785
// Special case for padding when angle is 'auto' for lines
767
786
// Padding should be treated as an orthogonal offset in this case
768
787
// Otherwise, padding is just a simple x and y offset
788
+ var paddingConstantsX = { left : 1 , center : 0 , right : - 1 } ;
789
+ var paddingConstantsY = { bottom : - 1 , middle : 0 , top : 1 } ;
769
790
if ( textAngle === 'auto' ) {
770
791
// Set direction to apply padding (based on `yanchor` only)
771
- var paddingDirection = { bottom : 1 , middle : 0 , top : - 1 } [ yanchor ] ;
772
- paddingX = textPadding * Math . sin ( textAngleRad ) * paddingDirection ;
773
- paddingY = - textPadding * Math . cos ( textAngleRad ) * paddingDirection ;
792
+ var paddingDirection = paddingConstantsY [ yanchor ] ;
793
+ paddingX = - textPadding * Math . sin ( textAngleRad ) * paddingDirection ;
794
+ paddingY = textPadding * Math . cos ( textAngleRad ) * paddingDirection ;
774
795
} else {
775
796
// Set direction to apply padding (based on `xanchor` and `yanchor`)
776
- var paddingDirectionX = { left : 1 , center : 0 , right : - 1 } [ xanchor ] ;
777
- var paddingDirectionY = { bottom : - 1 , middle : 0 , top : 1 } [ yanchor ] ;
797
+ var paddingDirectionX = paddingConstantsX [ xanchor ] ;
798
+ var paddingDirectionY = paddingConstantsY [ yanchor ] ;
778
799
paddingX = textPadding * paddingDirectionX ;
779
800
paddingY = textPadding * paddingDirectionY ;
780
801
}
@@ -814,7 +835,7 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
814
835
}
815
836
816
837
// Shift vertical (& horizontal) position according to `yanchor`
817
- var shiftFraction = { middle : 0.5 , bottom : 1 , top : 0 } [ yanchor ] ;
838
+ var shiftFraction = FROM_TL [ yanchor ] ;
818
839
// Adjust so that text is anchored at top of first line rather than at baseline of first line
819
840
var baselineAdjust = shapeOptions . label . font . size ;
820
841
var textHeight = textBB . height ;
0 commit comments