Skip to content

Commit 5f54668

Browse files
committed
add additional shape label template vars, exclude paths
1 parent 10550bb commit 5f54668

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/components/shapes/draw.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,24 @@ var FROM_TL = require('../../constants/alignment').FROM_TL;
3535
// if opt is blank, val can be 'add' or a full options object to add a new
3636
// annotation at that point in the array, or 'remove' to delete this one
3737

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+
3851
module.exports = {
3952
draw: draw,
4053
drawOne: drawOne,
41-
eraseActiveShape: eraseActiveShape
54+
eraseActiveShape: eraseActiveShape,
55+
shapeLabelTemplateVars: Object.keys(shapeLabelTemplateVars),
4256
};
4357

4458
function draw(gd) {
@@ -611,16 +625,16 @@ function drawLabel(gd, index, options, shapeGroup) {
611625
// Text template overrides text
612626
var text = options.label.text;
613627
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+
}
614634
text = Lib.texttemplateStringForShapes(options.label.texttemplate,
615635
{},
616636
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);
624638
}
625639

626640
var labelGroupAttrs = {

0 commit comments

Comments
 (0)