Skip to content

Commit 75b157f

Browse files
committed
Refactoring
1 parent 212f892 commit 75b157f

File tree

2 files changed

+46
-91
lines changed

2 files changed

+46
-91
lines changed

src/plots/template_attributes.js

Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
'use strict';
2+
const { DATE_FORMAT_LINK, FORMAT_LINK } = require('../constants/docs');
23

3-
var docs = require('../constants/docs');
4-
var FORMAT_LINK = docs.FORMAT_LINK;
5-
var DATE_FORMAT_LINK = docs.DATE_FORMAT_LINK;
6-
7-
function templateFormatStringDescription(opts) {
8-
var supportOther = opts && opts.supportOther;
4+
function templateFormatStringDescription(opts = {}) {
5+
const { supportOther } = opts;
6+
const supportOtherText =
7+
' as well as %{xother}, {%_xother}, {%_xother_}, {%xother_}. When showing info for several points, *xother* will be added to those with different x positions from the first point. An underscore before or after *(x|y)other* will add a space on that side, only when this field is shown.';
98

109
return [
1110
'Variables are inserted using %{variable},',
12-
'for example "y: %{y}"' +
13-
(supportOther
14-
? ' as well as %{xother}, {%_xother}, {%_xother_}, {%xother_}. When showing info for several points, *xother* will be added to those with different x positions from the first point. An underscore before or after *(x|y)other* will add a space on that side, only when this field is shown.'
15-
: '.'),
11+
'for example "y: %{y}"' + (supportOther ? supportOtherText : '.'),
1612
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{y:$.2f}".',
1713
FORMAT_LINK,
1814
'for details on the formatting syntax.',
@@ -23,49 +19,23 @@ function templateFormatStringDescription(opts) {
2319
}
2420
exports.templateFormatStringDescription = templateFormatStringDescription;
2521

26-
function shapeTemplateFormatStringDescription() {
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}". See',
31-
FORMAT_LINK,
32-
'for details on the formatting syntax.',
33-
'Dates are formatted using d3-time-format\'s syntax %{variable|d3-time-format}, for example "Day: %{x0|%m %b %Y}". See',
34-
DATE_FORMAT_LINK,
35-
'for details on the date formatting syntax.',
36-
'A single multiplication or division operation may be applied to numeric variables, and combined with',
37-
'd3 number formatting, for example "Length in cm: %{x0*2.54}", "%{slope*60:.1f} meters per second."',
38-
'For log axes, variable values are given in log units.',
39-
'For date axes, x/y coordinate variables and center variables use datetimes, while all other variable values use values in ms.'
40-
].join(' ');
41-
}
42-
43-
function describeVariables(extra) {
44-
var descPart = extra.description ? ' ' + extra.description : '';
45-
var keys = extra.keys || [];
22+
function describeVariables({ description, keys = [] }) {
23+
let descPart = description ? ' ' : '';
4624
if (keys.length > 0) {
47-
var quotedKeys = [];
48-
for (var i = 0; i < keys.length; i++) {
49-
quotedKeys[i] = '`' + keys[i] + '`';
50-
}
51-
descPart = descPart + 'Finally, the template string has access to ';
25+
const quotedKeys = keys.map((k) => `\`${k}\``);
26+
descPart += 'Finally, the template string has access to ';
5227
if (keys.length === 1) {
53-
descPart = descPart + 'variable ' + quotedKeys[0];
28+
descPart += `variable ${quotedKeys[0]}`;
5429
} else {
55-
descPart =
56-
descPart + 'variables ' + quotedKeys.slice(0, -1).join(', ') + ' and ' + quotedKeys.slice(-1) + '.';
30+
descPart += `variables ${quotedKeys.slice(0, -1).join(', ')} and ${quotedKeys.slice(-1)}.`;
5731
}
5832
}
33+
5934
return descPart;
6035
}
6136

62-
exports.hovertemplateAttrs = function (opts, extra) {
63-
opts = opts || {};
64-
extra = extra || {};
65-
66-
var descPart = describeVariables(extra);
67-
68-
var hovertemplate = {
37+
exports.hovertemplateAttrs = (opts = {}, extra = {}) => {
38+
const hovertemplate = {
6939
valType: 'string',
7040
dflt: '',
7141
editType: opts.editType || 'none',
@@ -75,26 +45,19 @@ exports.hovertemplateAttrs = function (opts, extra) {
7545
templateFormatStringDescription({ supportOther: true }),
7646
'The variables available in `hovertemplate` are the ones emitted as event data described at this link https://plotly.com/javascript/plotlyjs-events/#event-data.',
7747
'Additionally, every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
78-
descPart,
48+
describeVariables(extra),
7949
'Anything contained in tag `<extra>` is displayed in the secondary box, for example `<extra>%{fullData.name}</extra>`.',
8050
'To hide the secondary box completely, use an empty tag `<extra></extra>`.'
8151
].join(' ')
8252
};
8353

84-
if (opts.arrayOk !== false) {
85-
hovertemplate.arrayOk = true;
86-
}
54+
if (opts.arrayOk !== false) hovertemplate.arrayOk = true;
8755

8856
return hovertemplate;
8957
};
9058

91-
exports.texttemplateAttrs = function (opts, extra) {
92-
opts = opts || {};
93-
extra = extra || {};
94-
95-
var descPart = describeVariables(extra);
96-
97-
var texttemplate = {
59+
exports.texttemplateAttrs = (opts = {}, extra = {}) => {
60+
const texttemplate = {
9861
valType: 'string',
9962
dflt: '',
10063
editType: opts.editType || 'calc',
@@ -103,34 +66,34 @@ exports.texttemplateAttrs = function (opts, extra) {
10366
'Note that this will override `textinfo`.',
10467
templateFormatStringDescription(),
10568
'Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
106-
descPart
69+
describeVariables(extra)
10770
].join(' ')
10871
};
10972

110-
if (opts.arrayOk !== false) {
111-
texttemplate.arrayOk = true;
112-
}
113-
return texttemplate;
114-
};
115-
116-
exports.shapeTexttemplateAttrs = function (opts, extra) {
117-
opts = opts || {};
118-
extra = extra || {};
119-
120-
var newStr = opts.newshape ? 'new ' : '';
121-
122-
var descPart = describeVariables(extra);
73+
if (opts.arrayOk !== false) texttemplate.arrayOk = true;
12374

124-
var texttemplate = {
125-
valType: 'string',
126-
dflt: '',
127-
editType: opts.editType || 'arraydraw',
128-
description: [
129-
'Template string used for rendering the ' + newStr + "shape's label.",
130-
'Note that this will override `text`.',
131-
shapeTemplateFormatStringDescription(),
132-
descPart
133-
].join(' ')
134-
};
13575
return texttemplate;
13676
};
77+
78+
exports.shapeTexttemplateAttrs = (opts = {}, extra = {}) => ({
79+
valType: 'string',
80+
dflt: '',
81+
editType: opts.editType || 'arraydraw',
82+
description: [
83+
`Template string used for rendering the ${opts.newshape ? 'new ' : ''}shape's label.`,
84+
'Note that this will override `text`.',
85+
'Variables are inserted using %{variable},',
86+
'for example "x0: %{x0}".',
87+
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{x0:$.2f}". See',
88+
FORMAT_LINK,
89+
'for details on the formatting syntax.',
90+
'Dates are formatted using d3-time-format\'s syntax %{variable|d3-time-format}, for example "Day: %{x0|%m %b %Y}". See',
91+
DATE_FORMAT_LINK,
92+
'for details on the date formatting syntax.',
93+
'A single multiplication or division operation may be applied to numeric variables, and combined with',
94+
'd3 number formatting, for example "Length in cm: %{x0*2.54}", "%{slope*60:.1f} meters per second."',
95+
'For log axes, variable values are given in log units.',
96+
'For date axes, x/y coordinate variables and center variables use datetimes, while all other variable values use values in ms.',
97+
describeVariables(extra)
98+
].join(' ')
99+
});

src/traces/histogram2d/attributes.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,8 @@ module.exports = extendFlat(
6969
xhoverformat: axisHoverFormat('x'),
7070
yhoverformat: axisHoverFormat('y'),
7171
zhoverformat: axisHoverFormat('z', 1),
72-
hovertemplate: hovertemplateAttrs({}, { keys: 'z' }),
73-
texttemplate: texttemplateAttrs(
74-
{
75-
arrayOk: false,
76-
editType: 'plot'
77-
},
78-
{
79-
keys: 'z'
80-
}
81-
),
72+
hovertemplate: hovertemplateAttrs({}, { keys: ['z'] }),
73+
texttemplate: texttemplateAttrs({ arrayOk: false, editType: 'plot' }, { keys: ['z'] }),
8274
textfont: heatmapAttrs.textfont,
8375
showlegend: extendFlat({}, baseAttrs.showlegend, { dflt: false })
8476
},

0 commit comments

Comments
 (0)