Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scales/scale.category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function should be used to use the internal values of the category scale and convert them to the string values.
The internal values are numbers. This feels like a wrong implementation of this function.

The type definition also specifies the input as a number and not a string
https://www.chartjs.org/docs/latest/api/classes/Scale.html#getlabelforvalue

I would find it more logical that there would be a guard against number inputs but that would possible be too breaking since inputting a number as a string will be seen as a number.

So the current implementation is the most correct in my eyes and this is not a bug.

Copy link
Author

Choose a reason for hiding this comment

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

@LeeLenaleee but then the issue is here:

value: vScale ? '' + vScale.getLabelForValue(parsed[vScale.axis]) : ''

  getLabelAndValue(index) {
    const meta = this._cachedMeta;
    const iScale = meta.iScale;
    const vScale = meta.vScale;
    const parsed = this.getParsed(index);
    return {
      label: iScale ? '' + iScale.getLabelForValue(parsed[iScale.axis]) : '',
      value: vScale ? '' + vScale.getLabelForValue(parsed[vScale.axis]) : ''
    };
  }

?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you have a reproducible sample of the issue. Because I don't see the issue.
When I add a label with a number in it it just shows the label on the correct spot, even though indexed it would have picked something else.

https://jsfiddle.net/Lrkc75ao/

Copy link
Author

Choose a reason for hiding this comment

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

Will add one. Currently I see this in my vscode extension but a small example doesn't show the issue. Will keep on analyzing.

Copy link
Author

Choose a reason for hiding this comment

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

@LeeLenaleee
see a minimal example here (was struggling as the code I tried to repro with had my change already.... ;-)
https://jsfiddle.net/9xefs4g1/1/

Copy link
Author

Choose a reason for hiding this comment

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

image

here a pic of the example. Example code:

var options = {
  type: 'line',
  data: {
    labels: ["Jan", "2", "Mar", "Apr", "May", "Jun", "Jul"],
    datasets: [
			{
        type: 'line',
				data: [{x: 0, y: 'ON'},
        {x: 1, y: 'OFF'},
        {x: 2, y: '0'},
        {x: 3, y: 0},
        {x: 4, y: '1'}],
				borderWidth: 1,
        parsing: false
			}
		]
  },
  options: {
    scales: {
      y: {
        type: 'category',
        indexAxis: 'x',
        labels: ['ON', 'OFF', '1', '0'],
      },
    },
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);

Copy link
Collaborator

@LeeLenaleee LeeLenaleee Sep 14, 2025

Choose a reason for hiding this comment

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

I gave this another look and the current implementation seems correct to me.
The reason your code does not work is because you have set the parsing option to false.

In the documentation it is noted that if you do this, you will need to provide the data sorted and in the format that is the internal format. For category scales this is the index of the label.

Either not disabling parsing and use the labels for the Y key or using the index of the label and keep parsing dissabled will give the correct result.
Parsing true (default) and labels for Y keys: https://jsfiddle.net/gd2an916/
Using internal data structure: https://jsfiddle.net/q9mev3fd/

image

return labels[value];
}
return value;
Expand Down
4 changes: 3 additions & 1 deletion test/specs/scale.category.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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() {
Expand Down