Skip to content

Fix/category scale wrong tooltip #12106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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);

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