@@ -35,10 +35,24 @@ var FROM_TL = require('../../constants/alignment').FROM_TL;
35
35
// if opt is blank, val can be 'add' or a full options object to add a new
36
36
// annotation at that point in the array, or 'remove' to delete this one
37
37
38
+ var shapeLabelTemplateVars = {
39
+ x0 : ( function ( shape ) { return shape . x0 ; } ) ,
40
+ x1 : ( function ( shape ) { return shape . x1 ; } ) ,
41
+ y0 : ( function ( shape ) { return shape . y0 ; } ) ,
42
+ y1 : ( function ( shape ) { return shape . y1 ; } ) ,
43
+ slope : ( function ( shape ) { return ( shape . y1 - shape . y0 ) / ( shape . x1 - shape . x0 ) ; } ) ,
44
+ width : ( function ( shape ) { return shape . x1 - shape . x0 ; } ) ,
45
+ height : ( function ( shape ) { return shape . y1 - shape . y0 ; } ) ,
46
+ length : ( function ( shape ) { return Math . sqrt ( Math . pow ( ( shape . x1 - shape . x0 ) , 2 ) + Math . pow ( ( shape . y1 - shape . y0 ) , 2 ) ) ; } ) ,
47
+ xcenter : ( function ( shape ) { return ( shape . x1 - shape . x0 ) / 2 ; } ) ,
48
+ ycenter : ( function ( shape ) { return ( shape . y1 - shape . y0 ) / 2 ; } ) ,
49
+ } ;
50
+
38
51
module . exports = {
39
52
draw : draw ,
40
53
drawOne : drawOne ,
41
- eraseActiveShape : eraseActiveShape
54
+ eraseActiveShape : eraseActiveShape ,
55
+ shapeLabelTemplateVars : Object . keys ( shapeLabelTemplateVars ) ,
42
56
} ;
43
57
44
58
function draw ( gd ) {
@@ -611,16 +625,16 @@ function drawLabel(gd, index, options, shapeGroup) {
611
625
// Text template overrides text
612
626
var text = options . label . text ;
613
627
if ( options . label . texttemplate ) {
628
+ var templateValues = { } ;
629
+ if ( options . type !== 'path' ) {
630
+ Object . keys ( shapeLabelTemplateVars ) . forEach ( function ( key ) {
631
+ templateValues [ key ] = shapeLabelTemplateVars [ key ] ( options ) ;
632
+ } ) ;
633
+ }
614
634
text = Lib . texttemplateStringForShapes ( options . label . texttemplate ,
615
635
{ } ,
616
636
gd . _fullLayout . _d3locale ,
617
- {
618
- x0 : options . x0 ,
619
- y0 : options . y0 ,
620
- x1 : options . x1 ,
621
- y1 : options . y1 ,
622
- slope : ( options . y1 - options . y0 ) / ( options . x1 - options . x0 ) ,
623
- } ) ;
637
+ templateValues ) ;
624
638
}
625
639
626
640
var labelGroupAttrs = {
0 commit comments