Skip to content

Commit ead380a

Browse files
committed
rename some functions/variables, add comments, improve docstrings
1 parent a5b9aa8 commit ead380a

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

src/components/shapes/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var annAttrs = require('../annotations/attributes');
44
var fontAttrs = require('../../plots/font_attributes');
55
var scatterLineAttrs = require('../../traces/scatter/attributes').line;
66
var dash = require('../drawing/attributes').dash;
7-
var templateFormatStringDescription = require('../../plots/template_attributes').templateFormatStringDescription;
7+
var templateFormatStringDescriptionForShapes = require('../../plots/template_attributes').templateFormatStringDescriptionForShapes;
88
var describeVariables = require('../../plots/template_attributes').describeVariables;
99
var extendFlat = require('../../lib/extend').extendFlat;
1010
var templatedArray = require('../../plot_api/plot_template').templatedArray;
@@ -241,7 +241,7 @@ module.exports = templatedArray('shape', {
241241
description: [
242242
'Template string used for rendering the shape\'s label.',
243243
'Note that this will override `text`.',
244-
templateFormatStringDescription(),
244+
templateFormatStringDescriptionForShapes(),
245245
describeVariables(['x0', 'y0', 'x1', 'y1', 'slope']),
246246
].join(' ')
247247
},

src/components/shapes/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ function drawLabel(gd, index, options, shapeGroup) {
611611
// Text template overrides text
612612
var text = options.label.text;
613613
if(options.label.texttemplate) {
614-
text = Lib.texttemplateStringWithMath(options.label.texttemplate,
614+
text = Lib.texttemplateStringForShapes(options.label.texttemplate,
615615
{},
616616
gd._fullLayout._d3locale,
617617
{

src/components/shapes/draw_newshape/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var fontAttrs = require('../../../plots/font_attributes');
44
var dash = require('../../drawing/attributes').dash;
5-
var templateFormatStringDescription = require('../../../plots/template_attributes').templateFormatStringDescription;
5+
var templateFormatStringDescriptionForShapes = require('../../../plots/template_attributes').templateFormatStringDescriptionForShapes;
66
var describeVariables = require('../../../plots/template_attributes').describeVariables;
77
var extendFlat = require('../../../lib/extend').extendFlat;
88

@@ -95,7 +95,7 @@ module.exports = {
9595
description: [
9696
'Template string used for rendering the new shape\'s label.',
9797
'Note that this will override `text`.',
98-
templateFormatStringDescription(),
98+
templateFormatStringDescriptionForShapes(),
9999
describeVariables(['x0', 'y0', 'x1', 'y1', 'slope']),
100100
].join(' ')
101101
},

src/lib/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,20 +1074,24 @@ lib.texttemplateString = function() {
10741074
return templateFormatString.apply(texttemplateWarnings, arguments);
10751075
};
10761076

1077+
// Regex for parsing multiplication and division operations applied to a template key
1078+
// Used for shape.label.texttemplate
1079+
// Matches a key name (non-whitespace characters), followed by a * or / character, followed by a number
1080+
// For example, the following strings are matched: `x0*2`, `slope/1.60934`, `y1*2.54`
10771081
var MULT_DIV_REGEX = /^(\S+)([\*\/])(-?\d+(\.\d+)?)$/;
1078-
function parseMultDiv(inputStr) {
1082+
function multDivParser(inputStr) {
10791083
var match = inputStr.match(MULT_DIV_REGEX);
10801084
if(match) return { key: match[1], op: match[2], number: Number(match[3]) };
10811085
else return { key: inputStr, op: null, number: null };
10821086
}
1083-
var texttemplateWarningsWithMath = {
1087+
var texttemplateWarningsForShapes = {
10841088
max: 10,
10851089
count: 0,
10861090
name: 'texttemplate',
1087-
parseMath: true,
1091+
parseMultDiv: true,
10881092
};
1089-
lib.texttemplateStringWithMath = function() {
1090-
return templateFormatString.apply(texttemplateWarningsWithMath, arguments);
1093+
lib.texttemplateStringForShapes = function() {
1094+
return templateFormatString.apply(texttemplateWarningsForShapes, arguments);
10911095
};
10921096

10931097
var TEMPLATE_STRING_FORMAT_SEPARATOR = /^[:|\|]/;
@@ -1139,10 +1143,11 @@ function templateFormatString(string, labels, d3locale) {
11391143
if(isOtherSpace || isSpaceOtherSpace) key = key.substring(0, key.length - 1);
11401144

11411145
// Shape labels support * and / operators in template string
1146+
// Parse these if the parseMultDiv param is set to true
11421147
var parsedOp = null;
11431148
var parsedNumber = null;
1144-
if(opts.parseMath) {
1145-
var _match = parseMultDiv(key);
1149+
if(opts.parseMultDiv) {
1150+
var _match = multDivParser(key);
11461151
key = _match.key;
11471152
parsedOp = _match.op;
11481153
parsedNumber = _match.number;
@@ -1171,6 +1176,7 @@ function templateFormatString(string, labels, d3locale) {
11711176
}
11721177
}
11731178

1179+
// Apply mult/div operation (if applicable)
11741180
if(parsedOp) {
11751181
value = {
11761182
'*': (function(v) { return v * parsedNumber; }),

src/plots/template_attributes.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ function templateFormatStringDescription(opts) {
2222
'for details on the date formatting syntax.'
2323
].join(' ');
2424
}
25-
exports.templateFormatStringDescription = templateFormatStringDescription;
25+
26+
function templateFormatStringDescriptionForShapes() {
27+
return [
28+
'Variables are inserted using %{variable},',
29+
'for example "x0: %{x0}".',
30+
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{x0:$.2f}".',
31+
FORMAT_LINK,
32+
'for details on the formatting syntax.',
33+
'A single multiplication or division operation may be applied to the variable, and combined with',
34+
'd3 number formatting, for example "Length in cm: %{x0*2.54}", "%{slope*60:.1f} meters per second."'
35+
].join(' ');
36+
}
37+
exports.templateFormatStringDescriptionForShapes = templateFormatStringDescriptionForShapes;
2638

2739
function describeVariables(extra) {
2840
var descPart = extra.description ? ' ' + extra.description : '';

0 commit comments

Comments
 (0)