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

Docs/clarify usage of ticks callback (#9991) #9994

Merged
merged 2 commits into from Dec 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/axes/labelling.md
Expand Up @@ -45,7 +45,7 @@ const chart = new Chart(ctx, {
y: {
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
callback: function(value, index, ticks) {
return '$' + value;
}
}
Expand All @@ -55,6 +55,13 @@ const chart = new Chart(ctx, {
});
```

Keep in mind that overriding `ticks.callback` means that you are responsible for all formatting of the label. Depending on your use case, you may want to call the default formatter and then modify its output. In the example above, that would look like:

```javascript
// call the default formatter, forwarding `this`
Copy link
Member

Choose a reason for hiding this comment

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

It looks like there's some leading whitespace here which could be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept the leading whitespace the same as in the example above, to emphasize that the snippet would substitute for return '$' + value;.

return '$' + Chart.Ticks.formatters.numeric.apply(this, [value, index, ticks]);
```

Related samples:

* [Tick configuration sample](../samples/scale-options/ticks)