From d326efcae3454f04d61edbc6b20bb2ee20000a54 Mon Sep 17 00:00:00 2001 From: Matthias Behr Date: Sat, 2 Aug 2025 10:39:43 +0200 Subject: [PATCH 1/2] Add test for category scale getLabelForValue showing the problem/issue if a label is a string but converts to a number within the labels range. --- test/specs/scale.category.tests.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/specs/scale.category.tests.js b/test/specs/scale.category.tests.js index 70b0bb9a383..03d77f6af8f 100644 --- a/test/specs/scale.category.tests.js +++ b/test/specs/scale.category.tests.js @@ -158,7 +158,7 @@ describe('Category scale tests', function() { yAxisID: 'y', data: [10, 5, 0, 25, 78] }], - labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'] + labels: ['tick1', 'tick2', 'tick3', '1', 'tick5'] }, options: { scales: { @@ -176,6 +176,8 @@ describe('Category scale tests', function() { var scale = chart.scales.x; expect(scale.getLabelForValue(1)).toBe('tick2'); + expect(scale.getLabelForValue('tick3')).toBe('tick3'); + expect(scale.getLabelForValue('1')).toBe('1'); // and not 'tick2' }); it('Should get the correct pixel for a value when horizontal', function() { From 2c2d06c32b3f2b2036fc685502ab1a377e903ee8 Mon Sep 17 00:00:00 2001 From: Matthias Behr Date: Sat, 2 Aug 2025 10:43:22 +0200 Subject: [PATCH 2/2] Fix category scale returning wrong labels for string values If labels are used that convert to numbers between 0..labels.length a wrong value is returned. Changed getLabelForValue to use value as an index only if typeof value is number. --- src/scales/scale.category.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 3d773a8a8c2..5565abc3f72 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -25,7 +25,7 @@ const validIndex = (index, max) => index === null ? null : _limitValue(Math.roun function _getLabelForValue(value) { const labels = this.getLabels(); - if (value >= 0 && value < labels.length) { + if (typeof value === 'number' && value >= 0 && value < labels.length) { return labels[value]; } return value;