Skip to content

Commit 680c793

Browse files
committed
Add helper function for template fallback attributes
1 parent 85b67e9 commit 680c793

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

src/plots/template_attributes.js

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22
const { DATE_FORMAT_LINK, FORMAT_LINK } = require('../constants/docs');
33

4-
function templateFormatStringDescription(opts = {}) {
5-
const { supportOther } = opts;
4+
function templateFormatStringDescription({ supportOther } = {}) {
65
const supportOtherText =
76
' 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.';
87

@@ -34,53 +33,43 @@ function describeVariables({ description, keys = [] }) {
3433
return descPart;
3534
}
3635

37-
exports.hovertemplateAttrs = (opts = {}, extra = {}) => {
38-
const hovertemplate = {
39-
valType: 'string',
40-
dflt: '',
41-
editType: opts.editType || 'none',
42-
description: [
43-
'Template string used for rendering the information that appear on hover box.',
44-
'Note that this will override `hoverinfo`.',
45-
templateFormatStringDescription({ supportOther: true }),
46-
'The variables available in `hovertemplate` are the ones emitted as event data described at this link https://plotly.com/javascript/plotlyjs-events/#event-data.',
47-
'Additionally, every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
48-
describeVariables(extra),
49-
'Anything contained in tag `<extra>` is displayed in the secondary box, for example `<extra>%{fullData.name}</extra>`.',
50-
'To hide the secondary box completely, use an empty tag `<extra></extra>`.'
51-
].join(' ')
52-
};
53-
54-
if (opts.arrayOk !== false) hovertemplate.arrayOk = true;
55-
56-
return hovertemplate;
57-
};
58-
59-
exports.texttemplateAttrs = (opts = {}, extra = {}) => {
60-
const texttemplate = {
61-
valType: 'string',
62-
dflt: '',
63-
editType: opts.editType || 'calc',
64-
description: [
65-
'Template string used for rendering the information text that appear on points.',
66-
'Note that this will override `textinfo`.',
67-
templateFormatStringDescription(),
68-
'Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
69-
describeVariables(extra)
70-
].join(' ')
71-
};
72-
73-
if (opts.arrayOk !== false) texttemplate.arrayOk = true;
36+
exports.hovertemplateAttrs = ({ editType = 'none', arrayOk } = {}, extra = {}) => ({
37+
valType: 'string',
38+
dflt: '',
39+
editType,
40+
description: [
41+
'Template string used for rendering the information that appear on hover box.',
42+
'Note that this will override `hoverinfo`.',
43+
templateFormatStringDescription({ supportOther: true }),
44+
'The variables available in `hovertemplate` are the ones emitted as event data described at this link https://plotly.com/javascript/plotlyjs-events/#event-data.',
45+
'Additionally, every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
46+
describeVariables(extra),
47+
'Anything contained in tag `<extra>` is displayed in the secondary box, for example `<extra>%{fullData.name}</extra>`.',
48+
'To hide the secondary box completely, use an empty tag `<extra></extra>`.'
49+
].join(' '),
50+
...(arrayOk !== false ? { arrayOk: true } : {})
51+
});
7452

75-
return texttemplate;
76-
};
53+
exports.texttemplateAttrs = ({ editType = 'calc', arrayOk } = {}, extra = {}) => ({
54+
valType: 'string',
55+
dflt: '',
56+
editType,
57+
description: [
58+
'Template string used for rendering the information text that appears on points.',
59+
'Note that this will override `textinfo`.',
60+
templateFormatStringDescription(),
61+
'Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
62+
describeVariables(extra)
63+
].join(' '),
64+
...(arrayOk !== false ? { arrayOk: true } : {})
65+
});
7766

78-
exports.shapeTexttemplateAttrs = (opts = {}, extra = {}) => ({
67+
exports.shapeTexttemplateAttrs = ({ editType = 'arraydraw', newshape }, extra = {}) => ({
7968
valType: 'string',
8069
dflt: '',
81-
editType: opts.editType || 'arraydraw',
70+
editType,
8271
description: [
83-
`Template string used for rendering the ${opts.newshape ? 'new ' : ''}shape's label.`,
72+
`Template string used for rendering the ${newshape ? 'new ' : ''}shape's label.`,
8473
'Note that this will override `text`.',
8574
'Variables are inserted using %{variable},',
8675
'for example "x0: %{x0}".',
@@ -97,3 +86,10 @@ exports.shapeTexttemplateAttrs = (opts = {}, extra = {}) => ({
9786
describeVariables(extra)
9887
].join(' ')
9988
});
89+
90+
exports.templatefallbackAttrs = ({ editType = 'none' } = {}) => ({
91+
valType: 'string',
92+
dflt: '',
93+
editType,
94+
description: "Fallback value that's displayed when a variable referenced in a template can't be found."
95+
});

0 commit comments

Comments
 (0)