Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7900_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` silently ignoring d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)]
11 changes: 9 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ lib.adjustFormat = function adjustFormat(formatStr) {
if (/^\d%/.test(formatStr)) return '~%';
if (/^\ds/.test(formatStr)) return '~s';

// try adding tilde to the start of format in order to trim
if (!/^[~,.0$]/.test(formatStr) && /[&fps]/.test(formatStr)) return '~' + formatStr;
// A d3-format spec may begin with a sign flag (+, -, (, space) and/or a
// symbol ($, #). Look past that prefix before deciding whether to trim, and
// reattach it: prepending the tilde to the whole string (e.g. "~+.2f") is an
// invalid spec that d3Format rejects, so "+.2f" used to be silently dropped.
var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0];
var rest = formatStr.slice(prefix.length);

// try adding tilde to trim trailing zeros
if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;

return formatStr;
};
Expand Down
6 changes: 6 additions & 0 deletions test/jasmine/tests/lib_number_format_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('number format', function() {
{ format: '0f', number: float, exp: '12345.678901'},
{ format: '1f', number: float, exp: '12345.678901'},

// sign flag with an explicit precision must not be dropped (was silently
// ignored because adjustFormat produced the invalid spec "~+.2f")
{ format: '+.2f', number: float, exp: '+12345.68'},
{ format: '+.0f', number: float, exp: '+12346'},
{ format: '-.4f', number: float, exp: '12345.6789'},

// space-filled and default sign
{ format: '-13', number: float, exp: '-12345.678901'},
{ format: '-14', number: float, exp: ' -12345.678901'},
Expand Down
Loading