Skip to content

fix(scale.time): respect per-unit displayFormat in getLabelForValue#12276

Open
xianjianlf2 wants to merge 1 commit into
chartjs:masterfrom
xianjianlf2:fix/time-scale-getlabelforvalue-unit-format-12128
Open

fix(scale.time): respect per-unit displayFormat in getLabelForValue#12276
xianjianlf2 wants to merge 1 commit into
chartjs:masterfrom
xianjianlf2:fix/time-scale-getlabelforvalue-unit-format-12128

Conversation

@xianjianlf2

Copy link
Copy Markdown

Summary

TimeScale#getLabelForValue always formatted the value with the datetime displayFormat, ignoring the displayFormat configured for the currently resolved time unit. As reported in #12128 (with a pointer to scale.time.js:502), a value returned by getLabelForValue therefore did not match the format the user configured for the active unit.

Root cause

return adapter.format(value, timeOpts.displayFormats.datetime);

The datetime format key was hardcoded, so per-unit displayFormats (e.g. day, hour) were never used.

Fix

Format the value with the displayFormat of the currently resolved unit, falling back to datetime when no per-unit format is configured. tooltipFormat keeps precedence, exactly as before.

const displayFormats = timeOpts.displayFormats;
return adapter.format(value, displayFormats[this._unit] || displayFormats.datetime);

Tests

  • Added a unit spec asserting getLabelForValue uses the current unit's displayFormat, and that tooltipFormat still wins.
  • Three existing specs asserted the old, buggy output (the hardcoded datetime format) and were corrected to the format of the active unit. These assertions encoded the exact behaviour reported in getLabelForValue returns the wrong format in time scale #12128.

Fixes #12128

getLabelForValue always formatted values with the `datetime` displayFormat,
ignoring the displayFormat configured for the currently resolved time unit.
Format with `displayFormats[this._unit]` (falling back to `datetime`) so the
returned label respects the configured per-unit format, while `tooltipFormat`
keeps precedence.

Fixes chartjs#12128
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.

getLabelForValue returns the wrong format in time scale

1 participant