Skip to content

Commit 0fb52cd

Browse files
committed
add label.texttemplate to draw_newshape
1 parent b153b2d commit 0fb52cd

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/components/shapes/draw_newshape/attributes.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var fontAttrs = require('../../../plots/font_attributes');
44
var dash = require('../../drawing/attributes').dash;
5+
var templateFormatStringDescription = require('../../../plots/template_attributes').templateFormatStringDescription;
6+
var describeVariables = require('../../../plots/template_attributes').describeVariables;
57
var extendFlat = require('../../../lib/extend').extendFlat;
68

79
module.exports = {
@@ -86,6 +88,17 @@ module.exports = {
8688
editType: 'none',
8789
description: 'Sets the text to display with the new shape.'
8890
},
91+
texttemplate: {
92+
valType: 'string',
93+
dflt: '',
94+
editType: 'calc+arraydraw',
95+
description: [
96+
'Template string used for rendering the new shape\'s label.',
97+
'Note that this will override `text`.',
98+
templateFormatStringDescription(),
99+
describeVariables(['x0', 'y0', 'x1', 'y1', 'slope']),
100+
].join(' ')
101+
},
89102
font: fontAttrs({
90103
editType: 'none',
91104
description: 'Sets the new shape label text font.'

src/components/shapes/draw_newshape/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ module.exports = function supplyDrawNewShapeDefaults(layoutIn, layoutOut, coerce
2828

2929
var isLine = layoutIn.dragmode === 'drawline';
3030
var labelText = coerce('newshape.label.text');
31-
if(labelText) {
31+
var labelTextTemplate = coerce('newshape.label.texttemplate');
32+
if(labelText || labelTextTemplate) {
3233
coerce('newshape.label.textangle');
3334
var labelTextPosition = coerce('newshape.label.textposition', isLine ? 'middle' : 'middle center');
3435
coerce('newshape.label.xanchor');

test/image/mocks/zz-text_on_shapes_texttemplate.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
"shapes":[
1515
{"label":{"text":"this will be overridden", "texttemplate": "Here is x0 divided by 2: %{x0/2:.2f}"}, "layer":"below","x0":2,"x1":5,"y0":6,"y1":9,"opacity":0.5,"fillcolor":"#05e","line":{"width":3,"color":"#025","dash":"dashdot"}, "editable": true},
1616
{"label":{"text":"this will be overridden", "texttemplate": "Here is the slope: %{slope:.3f}"}, "layer":"below","x0":5,"x1":9,"y0":2.5,"y1":9,"type": "line", "opacity":0.5,"fillcolor":"#05e","line":{"width":3,"color":"#025"}, "editable": true}
17+
],
18+
"newshape": {
19+
"label": {
20+
"texttemplate": "New shapes display a slope! Slope = %{slope:.1f}"
21+
}
22+
}
23+
},
24+
"config": {
25+
"modeBarButtonsToAdd": [
26+
"drawline",
27+
"drawopenpath",
28+
"drawclosedpath",
29+
"drawcircle",
30+
"drawrect",
31+
"eraseshape"
1732
]
1833
}
1934
}

0 commit comments

Comments
 (0)