From 0e3a3bd2c722ae1aea3fd106e23b07bd0bcba929 Mon Sep 17 00:00:00 2001 From: AKIBUZZAMAN AKIB Date: Thu, 30 Apr 2026 23:03:44 +0000 Subject: [PATCH] fix(time-scale): getLabelForValue uses current unit displayFormat Previously, getLabelForValue() always fell back to the 'datetime' displayFormat regardless of the current time unit. This caused inconsistency when a custom displayFormat was set for the active unit (e.g. unit: 'month', displayFormats: { month: 'MMM YYYY' }), because the tooltip label would show the full datetime format instead. The format() method on the same scale correctly uses this._unit to pick the right displayFormat, so getLabelForValue() should do the same. Fix by using displayFormats[this._unit] with a fallback to 'datetime' when no unit-specific format is defined. Fixes #12128 --- src/scales/scale.time.js | 2 +- test/specs/scale.time.tests.js | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index f82d43ad72d..55823186f3e 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -499,7 +499,7 @@ export default class TimeScale extends Scale { if (timeOpts.tooltipFormat) { return adapter.format(value, timeOpts.tooltipFormat); } - return adapter.format(value, timeOpts.displayFormats.datetime); + return adapter.format(value, timeOpts.displayFormats[this._unit] || timeOpts.displayFormats.datetime); } /** diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 42817ae15c9..f566a9c8fcf 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -456,6 +456,42 @@ describe('Time scale tests', function() { } }); + it('getLabelForValue should use the current unit displayFormat, not datetime (issue #12128)', function() { + var chart = window.acquireChart({ + type: 'line', + data: { + datasets: [{ + xAxisID: 'x', + data: [ + {x: '2015-01-01', y: 10}, + {x: '2015-02-01', y: 20}, + {x: '2015-03-01', y: 30} + ] + }] + }, + options: { + scales: { + x: { + type: 'time', + time: { + unit: 'month', + displayFormats: { + month: 'MMM YYYY' + } + } + } + } + } + }); + + var xScale = chart.scales.x; + var controller = chart.getDatasetMeta(0).controller; + var value = controller.getParsed(0)[xScale.id]; + + // Should use 'month' displayFormat ('MMM YYYY'), not 'datetime' + expect(xScale.getLabelForValue(value)).toBe('Jan 2015'); + }); + it('should round to isoWeekday', function() { var chart = window.acquireChart({ type: 'line',