Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

logarithmic scale: first tick repeated, or axis starts below the data, from floating-point error in generateTicks

Open solquin opened this issue 3 weeks ago • 1 comments

Expected behavior

tick values on a logarithmic axis go strictly up, and scaling the data by a power of ten scales the ticks the same way.

  • min: 0.3 with data [0.3, 10] gives ticks starting 0.3, 0.4, 0.5, …
  • data [7, 10] (no min) gives an axis starting at 7, like [70, 100] starts at 70

Current behavior

generateTicks in src/scales/scale.logarithmic.js floors values that came out a hair below an integer (6.999999999999999, an offset of 6.000000000000001, -8.9e-15), so significand ends up one step too low.

config ticks / scale.min
min: 0.3, data [0.3, 10] [0.3, 0.3, 0.4, 0.5, …], label "0.3" drawn twice at the same pixel
min: 7, data [7, 10] [7, 7, 7.1, …]
data [7, 10] min 6.9, [6.9, 7, 7.5, 8, 9, 10]
data [70, 100] min 70, [70, 71, 72, …]
data [8, 9] min 7.8
data [80, 90] min 80

a sweep of min from 1.1 to 9.9 in every decade from 0.01 to 1000 hit the duplicated first tick in 273 of about 1,600 cases. with the default options autoSkip hides the duplicate on a short axis, but on a 500px tall chart both "0.3" labels are drawn on top of each other.

Reproducible sample

https://gistpreview.github.io/?c4001951dc4d49d70c297648af0b91cb (source: https://gist.github.com/solquin/c4001951dc4d49d70c297648af0b91cb). three 500px tall charts with default options, the first labels and scale.min are printed under them.

Optional extra steps/info to reproduce

same in node against dist/chart.js on master (71d4421):

const c = new Chart(ctx, {
  type: 'line',
  data: { labels: [0, 1], datasets: [{ data: [0.3, 10] }] },
  options: { scales: { y: { type: 'logarithmic', min: 0.3 } } },
});
c.scales.y.ticks.map(t => t.value); // [0.3, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, ...]

Possible solution

round the quotient before flooring, e.g. Math.floor(Math.round(q * 1e9) / 1e9), or keep offset and significand in integer arithmetic.

Context

No response

chart.js version

4.6.1 (also on master)

Browser name and version (if applicable)

No response

Link to your project

No response

solquin avatar Nov 08 '26 09:11 solquin

Reproduced on Chart.js 4.6.1 (cdn.jsdelivr.net/npm/[email protected]), Chromium 151, macOS, 2026-11-08.

Steps to reproduce:

  1. Create a line chart with type: 'logarithmic' on the y axis and default options.
  2. Use datasets: [{ data: [7, 10] }].
  3. After rendering, read chart.scales.y.min and chart.scales.y.ticks[0].label.

Actual: scale.min = 6.9. The first entry in .ticks has an empty label (the phantom 6.9 step sits below the data minimum); the first labelled tick is "7.0". The data line does not start at the bottom of the axis.

Expected: scale.min = 7 — the axis starts at the data minimum, matching the control case below.

Control: datasets: [{ data: [70, 100] }] gives scale.min = 70 correctly.

The asymmetry is in generateTicks: the intermediate offset for 7 comes out as 6.999999999999999, which floors to 6, so significand is set one step too low. For 70 the same computation stays integer-exact.

Recording (shows both cases side by side, with the console output): https://scenerecap.com/s/lsv0t6catc

scenerecap avatar Nov 08 '26 12:11 scenerecap