Skip to content

fix: give numeric tick labels enough digits to tell ticks apart - #12383

Open
RClaudiuM wants to merge 2 commits into
chartjs:masterfrom
RClaudiuM:fix/12382-numeric-tick-labels
Open

RClaudiuM wants to merge 2 commits into
chartjs:masterfrom
RClaudiuM:fix/12382-numeric-tick-labels

Conversation

@RClaudiuM

Copy link
Copy Markdown

Fixes #12382

Ticks.formatters.numeric picks the number of fraction digits from the tick spacing only. That goes wrong in two cases:

Scientific notation. With notation: 'scientific' the fraction digits apply to the significand, so the digits the spacing needs have to be counted from the tick's exponent. The formatter now adds floor(log10(|tick|)) to the digit count when the notation is scientific (whether picked automatically or set in ticks.format).

Ticks that are not multiples of the spacing. With bounds: 'data' the ticks are min + k * spacing, plus the data max. calculateDelta already handles this for spacings of 1 or more (a tick like 2.5 gets a decimal). For spacings below 1 it now does the same thing: if the tick is off the grid of the shown resolution, it uses the offset from the nearest multiple to pick the digits. A small relative tolerance keeps floating point noise such as 0.6000000000000001 from counting as an offset.

This shows enough digits to tell the ticks apart, not every digit of the data. -0.0936 becomes -0.094 rather than -0.0936, and data like 1/3 gives 0.33, 0.83, ... instead of 16 decimals.

Before / after

Using the script from the issue (Node, BasicPlatform, 400x300 canvas):

y data / options before after
[1e15, 5e15] 1E15, 2E15, 2E15, 3E15, 3E15, ... 1.0E15, 1.5E15, 2.0E15, 2.5E15, ..., 5.0E15
[0, 2e16] 0, 2E15, ..., 1E16, 1E16, 1E16, 2E16, 2E16, 2E16 0, 2E15, ..., 8E15, 1.0E16, 1.2E16, ..., 2.0E16
[0, 5e-5] 0, 1.00000E-5, 2.00000E-5, ... 0, 1E-5, 2E-5, ..., 6E-5
[-0.0936, 0.0917], bounds: 'data' -0.09, -0.07, ..., 0.07, 0.09, 0.09 -0.094, -0.074, ..., 0.066, 0.086, 0.092

Ordinary axes (for example 0, 0.1, ..., 1 or integer ticks) format the same as before.

Tests

Added specs to test/specs/core.ticks.tests.js: formatter-level cases for large and small scientific values and for off-grid ticks, plus a chart-level bounds: 'data' case. All three fail on master and pass with this change.

  • pnpm run lint: passes (no new warnings)
  • karma start ./karma.conf.cjs --single-run --browsers chrome --grep core.ticks: 7 of 7 pass
  • Full karma suite in Chrome and Firefox: the only failures are 7 image fixtures (filler, time and timeseries in both browsers, radialLinear/ticks-below-zero in Chrome, min-max-skip/rotated-case-4 in Firefox). The same 7 fail on unmodified master on my macOS machine, so they come from local rendering, not from this change.

In scientific notation the fraction digits apply to the significand, so
count them from the tick's exponent instead of from the spacing alone.
1.5e15 was shown as 2E15 and 1e-5 as 1.00000E-5.

When a tick is not a multiple of the spacing, as with bounds: 'data'
where ticks are min + k * spacing, show enough digits for its offset
from the nearest multiple. The last two ticks of [-0.0936, 0.0917] were
both shown as 0.09.

@TARUNTEjjjj TARUNTEjjjj left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-assisted review with independent browser validation on the current head. Details and scope are in the inline comment.

Comment thread src/core/core.ticks.js
// The tick might not be a multiple of the spacing, e.g. with `bounds: 'data'` the ticks are
// `min + k * spacing`. Show enough decimal places for its offset from the nearest multiple.
const unit = Math.pow(10, Math.floor(log10(Math.abs(delta))));
const offset = Math.abs(tickValue - Math.round(tickValue / unit) * unit);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I checked a symmetric fractional grid in addition to the offset-bound example: [-0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075] now preserves the quarter-grid values, whereas base labels ±0.075 as ±0.08 and ±0.025 as ±0.03. The same case works with de-DE decimal commas. This would be useful regression coverage because it exercises negative values on both sides of zero with a spacing that is not a power of ten. I also verified distinct labels across [9.8e-9, 9.9e-9, 1e-8, 1.01e-8, 1.02e-8] and that explicit user fraction-digit limits still override the computed precision. The 7 existing tick tests plus these 3 additional tests pass in Chrome Headless 157. I did not run Firefox/Safari or the full repository suite. AI-assisted review and local validation with OpenAI Codex.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for checking these. Added all three as tests in 01cf988:

  • the symmetric grid [-0.075 … 0.075] keeps -0.075/-0.025/0.025/0.075, in en-US and de-DE
  • [9.8e-9, 9.9e-9, 1e-8, 1.01e-8, 1.02e-8] gives 9.8E-9, 9.9E-9, 1.00E-8, 1.01E-8, 1.02E-8 (exact strings, since the base labels were already distinct but padded to 10 digits)
  • explicit minimumFractionDigits/maximumFractionDigits still override the computed precision

The first two fail without the change in core.ticks.js; the third passes on both, as expected.

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.

Numeric tick formatter gives repeated labels in scientific notation and with bounds: 'data'

2 participants