Title: [BUG]: hovertemplate/texttemplate/tickformat silently ignore a valid d3-format spec that begins with a sign flag (e.g. "+.2f") · Issue #7897 · plotly/plotly.js · GitHub
Open Graph Title: [BUG]: hovertemplate/texttemplate/tickformat silently ignore a valid d3-format spec that begins with a sign flag (e.g. "+.2f") · Issue #7897 · plotly/plotly.js
X Title: [BUG]: hovertemplate/texttemplate/tickformat silently ignore a valid d3-format spec that begins with a sign flag (e.g. "+.2f") · Issue #7897 · plotly/plotly.js
Description: Description A d3-format specifier that starts with a sign flag — +, -, (, or a space — is silently dropped when used in hovertemplate, texttemplate, tickformat, or hoverformat, and the raw (unformatted) number is shown instead. For examp...
Open Graph Description: Description A d3-format specifier that starts with a sign flag — +, -, (, or a space — is silently dropped when used in hovertemplate, texttemplate, tickformat, or hoverformat, and the raw (unforma...
X Description: Description A d3-format specifier that starts with a sign flag — +, -, (, or a space — is silently dropped when used in hovertemplate, texttemplate, tickformat, or hoverformat, and the raw (unforma...
Opengraph URL: https://github.com/plotly/plotly.js/issues/7897
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: hovertemplate/texttemplate/tickformat silently ignore a valid d3-format spec that begins with a sign flag (e.g. \"+.2f\")","articleBody":"### Description\n\nA d3-format specifier that starts with a **sign flag** — `+`, `-`, `(`, or a space — is silently dropped when used in `hovertemplate`, `texttemplate`, `tickformat`, or `hoverformat`, and the raw (unformatted) number is shown instead.\n\nFor example, `%{y:+.2f}` should render `+12.35`, but it renders the full-precision value `12.3456789`. The sibling spec `%{y:.2f}` (no sign flag) works correctly and renders `12.35`.\n\n`+.2f` is a valid d3-format specifier (grammar: `[[fill]align][sign][symbol][0][width][,][.precision][~][type]`), so it is expected to work.\n\n**Root cause** is in `Lib.adjustFormat` (`src/lib/index.js`). Its \"add a leading tilde to trim trailing zeros\" heuristic does not account for a leading sign flag:\n\n```js\n// src/lib/index.js\nlib.adjustFormat = function adjustFormat(formatStr) {\n if (!formatStr || /^\\d[.]\\df/.test(formatStr) || /[.]\\d%/.test(formatStr)) return formatStr;\n if (formatStr === '0.f') return '~f';\n if (/^\\d%/.test(formatStr)) return '~%';\n if (/^\\ds/.test(formatStr)) return '~s';\n\n // try adding tilde to the start of format in order to trim\n if (!/^[~,.0$]/.test(formatStr) && /[&fps]/.test(formatStr)) return '~' + formatStr;\n\n return formatStr;\n};\n```\n\nFor `formatStr = \"+.2f\"`:\n- `/^[~,.0$]/.test(\"+.2f\")` is `false` (it starts with `+`, which is not in the set), so `!(...)` is `true`;\n- `/[&fps]/.test(\"+.2f\")` is `true` (contains `f`);\n- therefore `adjustFormat` returns **`\"~+.2f\"`**.\n\n`\"~+.2f\"` is an **invalid** d3-format spec (the `~` trim flag must appear immediately before the type, e.g. `+.2~f`; a leading `~` followed by a sign does not parse). `d3Format('~+.2f')` throws, and `Lib.numberFormat` catches it and falls back to `Lib.noFormat`, which returns `String(value)` — the unformatted number:\n\n```js\nlib.numberFormat = function (formatStr) {\n var fn;\n try {\n fn = d3Format(lib.adjustFormat(formatStr));\n } catch (e) {\n lib.warnBadFormat(formatStr); // logs: encountered bad format: \"+.2f\"\n return lib.noFormat; // -> String(value), unformatted\n }\n return fn;\n};\n```\n\nBy contrast, `\".2f\"` starts with `.`, which **is** in `[~,.0$]`, so `adjustFormat` leaves it untouched and it formats correctly.\n\nThis affects every code path that goes through `Lib.numberFormat`: `hovertemplate`, `texttemplate`, `tickformat`, and `hoverformat`.\n\n### Screenshots/Video\n\n_Hovering the bar in the repro below: field A (`:.2f`) shows `12.35`; field B (`:+.2f`) shows `12.3456789` instead of `+12.35`. The browser console also logs `encountered bad format: \"+.2f\"`._\n\n### Steps to reproduce\n\n```html\n\u003c!DOCTYPE html>\n\u003chtml>\n\u003chead>\n \u003cmeta charset=\"utf-8\" />\n \u003cscript src=\"https://cdn.plot.ly/plotly-3.7.0.js\">\u003c/script>\n\u003c/head>\n\u003cbody>\n \u003cdiv id=\"chart\" style=\"width:700px;height:260px;\">\u003c/div>\n \u003cscript>\n var delta = 12.3456789;\n Plotly.newPlot('chart', [{\n type: 'bar', orientation: 'h',\n y: ['example'], x: [delta],\n customdata: [[delta, delta]],\n hovertemplate:\n 'A (:.2f) = %{customdata[0]:.2f}\u003cbr>' +\n 'B (:+.2f) = %{customdata[1]:+.2f}\u003cextra>\u003c/extra>'\n }]);\n // Expected hover: A = 12.35 B = +12.35\n // Actual hover: A = 12.35 B = 12.3456789\n \u003c/script>\n\u003c/body>\n\u003c/html>\n```\n- Open the page in a browser ([JSFiddle](https://jsfiddle.net/j3urLn7o)).\n- Hover over the bar.\n- Note that field **B** (`%{...:+.2f}`) shows `12.3456789` instead of `+12.35`, while field **A** (`%{...:.2f}`) correctly shows `12.35`.\n- Open the developer console and note the warning: `encountered bad format: \"+.2f\"`.\n\nThe same failure occurs with any spec beginning with a sign flag and containing one of `f`/`p`/`s`/`&`, e.g. `-.2f`, `( .2f`, `+,.0f`, `+.1%`, `+.3s`. It also reproduces via `tickformat: '+.2f'` and `hoverformat: '+.2f'`.\n\n### Notes\n\n- Confirmed present on `master` (and released `v3.7.0`) — `Lib.adjustFormat` is unchanged as of commit `227bba91` and still contains the tilde-prepend heuristic above.\n- Suggested fix: in the tilde-prepend branch, do not prepend `~` when the spec begins with a sign flag. For example, exclude a leading `[+\\-( ]` (and any leading fill/align) from that branch, or only insert `~` immediately before the type character rather than at the start of the spec. A regression test covering `+.2f`, `-.2f`, `( .2f`, `+,.0f`, `+.3s`, `+.1%` would guard this.\n- Workaround for users: pre-format the value in the data (pass a pre-rendered string via `customdata`/`text`) or omit the leading `+` sign flag.\n","author":{"url":"https://github.com/omtee","@type":"Person","name":"omtee"},"datePublished":"2026-07-10T12:09:34Z","url":"https://github.com/plotly/plotly.js/issues/7897"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5bef781d-56f3-2231-e90d-f860099b6c8a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D20A:8FAC9:3C38CB:4E6192:6AFF8B06 |
| html-safe-nonce | 308bc5277c012bf1c3ccd46d92fea50ed178c2f0e3b611a393e68978448bd20b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjBBOjhGQUM5OjNDMzhDQjo0RTYxOTI6NkFGRjhCMDYiLCJ2aXNpdG9yX2lkIjoiMzY4MjMwNzYyMDUyMzE4Mjg1NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8bf8bdf8e54c670a1b05983e29bb08f7f962515d06209e29fe02066e5fc17a4e |
| hovercard-subject-tag | issue:4854849021 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/plotly/plotly.js/7897/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec9b333c51e069c3aabebaf50009469d2b87a73dd1fb09c564a9c094159e7f5e/plotly/plotly.js/issues/7897 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec9b333c51e069c3aabebaf50009469d2b87a73dd1fb09c564a9c094159e7f5e/plotly/plotly.js/issues/7897 |
| og:image:alt | Description A d3-format specifier that starts with a sign flag — +, -, (, or a space — is silently dropped when used in hovertemplate, texttemplate, tickformat, or hoverformat, and the raw (unforma... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | omtee |
| hostname | github.com |
| expected-hostname | github.com |
| None | 587ad99a3e443b2e1cb0d3df059ec49ab54389861f13310766dfdeb615fed90d |
| turbo-cache-control | no-preview |
| go-import | github.com/plotly/plotly.js git https://github.com/plotly/plotly.js.git |
| octolytics-dimension-user_id | 5997976 |
| octolytics-dimension-user_login | plotly |
| octolytics-dimension-repository_id | 45646037 |
| octolytics-dimension-repository_nwo | plotly/plotly.js |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 45646037 |
| octolytics-dimension-repository_network_root_nwo | plotly/plotly.js |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5bb0fb68d64881d1344f5863d809515d496801e5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width