Skip to content

Commit f8c8451

Browse files
committed
provide underscore other keys to add optional space before or after
1 parent ad2ad74 commit f8c8451

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

src/lib/index.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -1043,14 +1043,31 @@ function templateFormatString(string, labels, d3locale) {
10431043
// just in case it speeds things up *slightly*:
10441044
var getterCache = {};
10451045

1046-
1047-
return string.replace(lib.TEMPLATE_STRING_REGEX, function(match, key, format) {
1046+
return string.replace(lib.TEMPLATE_STRING_REGEX, function(match, rawKey, format) {
10481047
var isOther =
1049-
key === 'xother' ||
1050-
key === 'yother';
1048+
rawKey === 'xother' ||
1049+
rawKey === 'yother';
1050+
1051+
var isSpaceOther =
1052+
rawKey === '_xother' ||
1053+
rawKey === '_yother';
1054+
1055+
var isSpaceOtherSpace =
1056+
rawKey === '_xother_' ||
1057+
rawKey === '_yother_';
1058+
1059+
var isOtherSpace =
1060+
rawKey === 'xother_' ||
1061+
rawKey === 'yother_';
1062+
1063+
var hasOther = isOther || isSpaceOther || isOtherSpace || isSpaceOtherSpace;
1064+
1065+
var key = rawKey;
1066+
if(isSpaceOther || isSpaceOtherSpace) key = key.substring(1);
1067+
if(isOtherSpace || isSpaceOtherSpace) key = key.substring(0, key.length - 1);
10511068

10521069
var value;
1053-
if(isOther) {
1070+
if(hasOther) {
10541071
value = labels[key];
10551072
if(value === undefined) return '';
10561073
} else {
@@ -1103,8 +1120,10 @@ function templateFormatString(string, labels, d3locale) {
11031120
if(labels.hasOwnProperty(keyLabel)) value = labels[keyLabel];
11041121
}
11051122

1106-
if(isOther) {
1107-
value = ' (' + value + ') ';
1123+
if(hasOther) {
1124+
value = '(' + value + ')';
1125+
if(isSpaceOther || isSpaceOtherSpace) value = ' ' + value;
1126+
if(isOtherSpace || isSpaceOtherSpace) value = value + ' ';
11081127
}
11091128

11101129
return value;

src/plots/template_attributes.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ function templateFormatStringDescription(opts) {
99

1010
return [
1111
'Variables are inserted using %{variable},',
12-
'for example "y: %{y}".' + (supportOther ? ' as well as %{xother} when positions do not match' : ''),
12+
'for example "y: %{y}"' + (
13+
supportOther ?
14+
' as well as %{xother}, {%_xother}, {%_xother_}, {%xother_}, when positions do not match. Please note that underscore could be used before or after *(x|y)other* to provide optional space.' :
15+
'.'
16+
),
1317
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{y:$.2f}".',
1418
FORMAT_LINK,
1519
'for details on the formatting syntax.',

test/jasmine/tests/hover_label_test.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -4834,17 +4834,27 @@ describe('hovermode: (x|y)unified', function() {
48344834
it('should format differing position using *xother* `hovertemplate` and in respect to `xhoverformat`', function(done) {
48354835
Plotly.newPlot(gd, [{
48364836
type: 'bar',
4837-
hovertemplate: '%{y:.1f}%{xother:.2f}',
4837+
hovertemplate: '%{y}%{_xother:.2f}',
48384838
x: [0, 1.001],
4839-
y: [1.001, 2.001]
4839+
y: [2, 1]
48404840
}, {
48414841
x: [0, 0.749],
4842-
y: [1.999, 0.999]
4842+
y: [1, 2]
48434843
}, {
4844-
hovertemplate: '%{y:.1f}%{xother}',
4844+
hovertemplate: '(x)y:%{xother}%{y}',
48454845
xhoverformat: '.1f',
48464846
x: [0, 1.251],
4847-
y: [2.001, 3.001]
4847+
y: [2, 3]
4848+
}, {
4849+
hovertemplate: '(x)y:%{xother_}%{y}',
4850+
xhoverformat: '.2f',
4851+
x: [0, 1.351],
4852+
y: [3, 4]
4853+
}, {
4854+
hovertemplate: '(x)y:%{_xother_}%{y}',
4855+
xhoverformat: '.3f',
4856+
x: [0, 1.451],
4857+
y: [4, 5]
48484858
}], {
48494859
hoverdistance: -1,
48504860
hovermode: 'x unified',
@@ -4860,26 +4870,32 @@ describe('hovermode: (x|y)unified', function() {
48604870
})
48614871
.then(function() {
48624872
_hover(gd, { xpx: 100, ypx: 200 });
4863-
assertLabel({title: '0', items: [
4864-
'trace 0 : 1.0',
4865-
'trace 1 : 1.999',
4866-
'trace 2 : 2.0 (0.0) '
4873+
assertLabel({title: '0.000', items: [
4874+
'trace 0 : 2 (0.00)',
4875+
'trace 1 : (0, 1)',
4876+
'trace 2 : (x)y:(0.0)2',
4877+
'trace 3 : (x)y:(0.00) 3',
4878+
'trace 4 : (x)y:4',
48674879
]});
48684880
})
48694881
.then(function() {
48704882
_hover(gd, { xpx: 250, ypx: 200 });
48714883
assertLabel({title: '0.749', items: [
4872-
'trace 0 : 2.0 (1.00) ',
4873-
'trace 1 : 0.999',
4874-
'trace 2 : 3.0 (1.3) '
4884+
'trace 0 : 1 (1.00)',
4885+
'trace 1 : 2',
4886+
'trace 2 : (x)y:(1.3)3',
4887+
'trace 3 : (x)y:(1.35) 4',
4888+
'trace 4 : (x)y: (1.451) 5',
48754889
]});
48764890
})
48774891
.then(function() {
48784892
_hover(gd, { xpx: 350, ypx: 200 });
4879-
assertLabel({title: '1.3', items: [
4880-
'trace 0 : 2.0 (1.00) ',
4881-
'trace 1 : (0.749, 0.999)',
4882-
'trace 2 : 3.0'
4893+
assertLabel({title: '1.35', items: [
4894+
'trace 0 : 1 (1.00)',
4895+
'trace 1 : (0.749, 2)',
4896+
'trace 2 : (x)y:(1.3)3',
4897+
'trace 3 : (x)y:4',
4898+
'trace 4 : (x)y: (1.451) 5',
48834899
]});
48844900
})
48854901
.then(done, done.fail);

0 commit comments

Comments
 (0)