Skip to content

Commit c6370d9

Browse files
committedAug 21, 2019
support formatting date in (hover|text)template
1 parent 8c339df commit c6370d9

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed
 

‎src/components/fx/hovertemplate_attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
12+
var DATE_FORMAT_LINK = require('../../constants/docs').DATE_FORMAT_LINK;
1213

1314
module.exports = function(opts, extra) {
1415
opts = opts || {};
@@ -41,6 +42,9 @@ module.exports = function(opts, extra) {
4142
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{y:$.2f}".',
4243
FORMAT_LINK,
4344
'for details on the formatting syntax.',
45+
'Dates are formatted using d3-time-format\'s syntax %{variable|d3-time-format}, for example "Day: %{2019-01-01|%A}".',
46+
DATE_FORMAT_LINK,
47+
'for details on the date formatting syntax.',
4448
'The variables available in `hovertemplate` are the ones emitted as event data described at this link https://plot.ly/javascript/plotlyjs-events/#event-data.',
4549
'Additionally, every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
4650
descPart,

‎src/lib/index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ lib.numSeparate = function(value, separators, separatethousands) {
964964
return x1 + x2;
965965
};
966966

967-
lib.TEMPLATE_STRING_REGEX = /%{([^\s%{}:]*)(:[^}]*)?}/g;
967+
lib.TEMPLATE_STRING_REGEX = /%{([^\s%{}:]*)([:|\|][^}]*)?}/g;
968968
var SIMPLE_PROPERTY_REGEX = /^\w*$/;
969969

970970
/**
@@ -1011,7 +1011,7 @@ lib.texttemplateString = function() {
10111011
return templateFormatString.apply(texttemplateWarnings, arguments);
10121012
};
10131013

1014-
var TEMPLATE_STRING_FORMAT_SEPARATOR = /^:/;
1014+
var TEMPLATE_STRING_FORMAT_SEPARATOR = /^[:|\|]/;
10151015
/**
10161016
* Substitute values from an object into a string and optionally formats them using d3-format,
10171017
* or fallback to associated labels.
@@ -1067,12 +1067,20 @@ function templateFormatString(string, labels, d3locale) {
10671067

10681068
if(format) {
10691069
var fmt;
1070-
if(d3locale) {
1071-
fmt = d3locale.numberFormat;
1072-
} else {
1073-
fmt = d3.format;
1070+
if(format[0] === ':') {
1071+
if(d3locale) {
1072+
fmt = d3locale.numberFormat;
1073+
} else {
1074+
fmt = d3.format;
1075+
}
1076+
value = fmt(format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''))(value);
1077+
}
1078+
1079+
if(format[0] === '|') {
1080+
fmt = d3.time.format.utc;
1081+
var ms = lib.dateTime2ms(value);
1082+
value = lib.formatDate(ms, format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''), false, fmt);
10741083
}
1075-
value = fmt(format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''))(value);
10761084
} else {
10771085
if(labels.hasOwnProperty(key + 'Label')) value = labels[key + 'Label'];
10781086
}

‎src/plots/texttemplate_attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var FORMAT_LINK = require('../constants/docs').FORMAT_LINK;
12+
var DATE_FORMAT_LINK = require('../constants/docs').DATE_FORMAT_LINK;
1213

1314
module.exports = function(opts, extra) {
1415
opts = opts || {};
@@ -42,6 +43,9 @@ module.exports = function(opts, extra) {
4243
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{y:$.2f}".',
4344
FORMAT_LINK,
4445
'for details on the formatting syntax.',
46+
'Dates are formatted using d3-time-format\'s syntax %{variable|d3-time-format}, for example "Day: %{2019-01-01|%A}".',
47+
DATE_FORMAT_LINK,
48+
'for details on the date formatting syntax.',
4549
'Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.',
4650
descPart
4751
].join(' ')

‎test/jasmine/tests/lib_test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2225,12 +2225,17 @@ describe('Test lib.js:', function() {
22252225
expect(Lib.hovertemplateString('y: %{y}', {}, locale, {y: 0}, {y: 1})).toEqual('y: 0');
22262226
});
22272227

2228-
it('formats value using d3 mini-language', function() {
2228+
it('formats numbers using d3-format mini-language when `:`', function() {
22292229
expect(Lib.hovertemplateString('a: %{a:.0%}', {}, locale, {a: 0.123})).toEqual('a: 12%');
22302230
expect(Lib.hovertemplateString('a: %{a:0.2%}', {}, locale, {a: 0.123})).toEqual('a: 12.30%');
22312231
expect(Lib.hovertemplateString('b: %{b:2.2f}', {}, locale, {b: 43})).toEqual('b: 43.00');
22322232
});
22332233

2234+
it('formats date using d3-time-format mini-language `|`', function() {
2235+
expect(Lib.hovertemplateString('a: %{a|%A}', {}, locale, {a: '2019-05-22'})).toEqual('a: Wednesday');
2236+
expect(Lib.hovertemplateString('%{x|%b %-d, %Y}', {}, locale, {x: '2019-01-01'})).toEqual('Jan 1, 2019');
2237+
});
2238+
22342239
it('looks for default label if no format is provided', function() {
22352240
expect(Lib.hovertemplateString('y: %{y}', {yLabel: '0.1'}, locale, {y: 0.123})).toEqual('y: 0.1');
22362241
});

0 commit comments

Comments
 (0)
Please sign in to comment.