-
Notifications
You must be signed in to change notification settings - Fork 12k
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
base: master
Are you sure you want to change the base?
Conversation
showing the problem/issue if a label is a string but converts to a number within the labels range.
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.
I stumbled upon this problem while analyzing mbehr1/dlt-logs#268. |
@@ -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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
Chart.js/src/core/core.datasetController.js
Line 694 in 6372280
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]) : ''
};
}
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

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);
No description provided.