Skip to content
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

x-Axis ticks callback receives wrong value argument #7850

Closed
cnoelle opened this issue Oct 5, 2020 · 2 comments · Fixed by #8798
Closed

x-Axis ticks callback receives wrong value argument #7850

cnoelle opened this issue Oct 5, 2020 · 2 comments · Fixed by #8798

Comments

@cnoelle
Copy link

cnoelle commented Oct 5, 2020

Expected Behavior

We can specify a callback for the ticks of an axis in the form

ticks: {
    callback: function(value, index, values) {
        return value;
    }
}

The callback function should receive the default tick label as first argument and a numeric index as second (https://www.chartjs.org/docs/3.0.0-alpha/axes/labelling.html).

Current Behavior

For the x-Axis the callback function receives the index twice instead of value and index. For the y-Axis it works fine.

Steps to Reproduce

https://jsfiddle.net/fshu1xa8/

Environment

Chart.js version 3.0.0-beta.3 (works fine in 2.9.3, although the syntax needs to be adapted).

@etimberg
Copy link
Member

etimberg commented Oct 6, 2020

This is due to

buildTicks() {
const me = this;
const min = me.min;
const max = me.max;
const offset = me.options.offset;
const ticks = [];
let labels = me.getLabels();
// If we are viewing some subset of labels, slice the original array
labels = (min === 0 && max === labels.length - 1) ? labels : labels.slice(min, max + 1);
me._valueRange = Math.max(labels.length - (offset ? 0 : 1), 1);
me._startValue = me.min - (offset ? 0.5 : 0);
for (let value = min; value <= max; value++) {
ticks.push({value});
}
return ticks;
}
. I believe it's intentional to have common code between the axes so we should document that this is how the category scale works.

@cnoelle
Copy link
Author

cnoelle commented Oct 6, 2020

Ok, thank you for the explanation, @etimberg . Makes sense for categorical labels, even though I find it a bit confusing to have a different behaviour than for linear scale, for instance.

On a related note, it also took me some time to understand that the default axis type for the x-axis is "catetegory" even if the labels are numeric. It could be helpful to have another example under the header "Scale options" for "Numeric X Axis" (there is already "Non Numeric Y Axis"): https://www.chartjs.org/samples/3.0.0-alpha/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants