Skip to content

Fix adjustFormat dropping d3-format specs that start with a sign flag - #7900

Open
TemRevil wants to merge 4 commits into
plotly:masterfrom
TemRevil:fix/adjustformat-sign-flag
Open

Fix adjustFormat dropping d3-format specs that start with a sign flag#7900
TemRevil wants to merge 4 commits into
plotly:masterfrom
TemRevil:fix/adjustformat-sign-flag

Conversation

@TemRevil

@TemRevil TemRevil commented Jul 10, 2026

Copy link
Copy Markdown

Fixes #7897

A d3-format specifier that begins with a sign flag (+, -, (, or a space) was silently ignored in hovertemplate, texttemplate, tickformat, and hoverformat, showing the raw unformatted number instead. For example %{y:+.2f} rendered 12.3456789 rather than +12.35.

Cause: Lib.adjustFormat prepends a trim tilde to the start of the whole string, so +.2f became ~+.2f — an invalid spec (the ~ flag must sit just before the type). d3.format threw, Lib.numberFormat caught it and fell back to Lib.noFormat.

Fix: skip past an optional leading sign flag and symbol ($, #) before the trim heuristic, then reattach that prefix, so the tilde is only inserted in a valid position. Formats without such a prefix are unchanged.

Verified against d3-format v1 directly: +.2f+12345.68, +.0f+12346, -.4f12345.6789, while .2f, s, 0.3s, +13, -13, ($15, #0X, .2% produce identical output to before. Added matching cases in test/jasmine/tests/lib_number_format_test.js.

TemRevil added 4 commits July 10, 2026 16:04
A d3-format specifier beginning with a sign flag (+, -, (, space) was
silently ignored: adjustFormat prepended a trim tilde to the whole
string, producing an invalid spec like ~+.2f. d3.format then threw,
numberFormat fell back to noFormat, and the raw unformatted number was
shown (e.g. %{y:+.2f} rendered 12.3456789 instead of +12.35).

Skip past an optional leading sign flag and symbol ($, #) before the
trim heuristic and reattach it, so the tilde is only added in a valid
position. Formats without such a prefix are unaffected.

Fixes plotly#7897
@camdecoster camdecoster self-assigned this Jul 15, 2026
@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for the PR! I'll take a look and follow up.

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! I'd like to make some changes to avoid some unintended consequences of this update.

Comment thread src/lib/index.js
// 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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove these from the prefix group. Without removing these, "$f" will get changed to "$~f". This means that "1.50" would get changed to "$1.5", which isn't what we want.

Suggested change
var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0];
var prefix = (formatStr.match(/^[+\-( ]/) || [''])[0];

Comment thread src/lib/index.js
var rest = formatStr.slice(prefix.length);

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;
if (!/^[~,.0$#]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;

Comment on lines +56 to +60
// 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'},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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'},
// sign flag with an explicit precision must not be dropped
{ format: '+.2f', number: float, exp: '+12345.68'},
{ format: '+.0f', number: float, exp: '+12346'},
{ format: '-.4f', number: float, exp: '12345.
// symbol-led specs ($, #) are left untrimmed
{ format: '$f', number: 1.5, exp: '$1.500000'},
{ format: '#f', number: 1.5, exp: '1.500000'},
{ format: '$s', number: 1500, exp: '$1.50000k'},

Comment thread draftlogs/7900_fix.md
@@ -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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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)]
- Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` improperly handling d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)]

@TemRevil

Copy link
Copy Markdown
Author

i'm getting some mind melting currently, so i'll review it and send you back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: hovertemplate/texttemplate/tickformat silently ignore a valid d3-format spec that begins with a sign flag (e.g. "+.2f")

2 participants