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

Using tick callbacks can add extra digits to tick labels #9991

Closed
doug-a-brunner opened this issue Dec 15, 2021 · 6 comments
Closed

Using tick callbacks can add extra digits to tick labels #9991

doug-a-brunner opened this issue Dec 15, 2021 · 6 comments

Comments

@doug-a-brunner
Copy link
Contributor

doug-a-brunner commented Dec 15, 2021

Expected Behavior

Null Y axis tick callback ((value) => value) or simple callback (`(value) => value + 'psi') should not affect value precision.

Current Behavior

For some values of data and suggested axis limits, the Y axis ticks are rendered with extraneous digits (e.g. "78.95000000000002" instead of "78.95") if a callback is present, even if that callback does not change the value it's passed.

Possible Solution

Might need to add an argument to the callback API that gives the proposed/default string rendering of the number value.

Steps to Reproduce

Set suggestedMin, suggestedMax, and data as shown in example. Set a null callback for the Y axis ticks, and note extra digits in Y axis tick labels. Remove callback, and note change in tick labels.

Context

Our project plots values in engineering units (psi, kPa, V, °C, etc.) logged from our controllers. We use a tick callback to add those units to the Y axis tick labels.

We do not know the range of the data in an individual log in advance, so in order to maximize the dynamic range of the plot, we compute suggestedMin and suggestedMax from the data. In addition, the data may be converted to localized units (e.g. showing °F for US users), which introduces small errors due to FP precision limitations.

Environment

@kurkle
Copy link
Member

kurkle commented Dec 15, 2021

The number formatting is done in the callback. If you replace the callback, its up to you to make it produce nice ticks.

You can call the default formatter from the callback:
https://codepen.io/kurkle/pen/bGoqYEK

@doug-a-brunner
Copy link
Contributor Author

Thanks for the clarification, didn't know that was possible. Looking at the current docs, it would be helpful to add some clarification. How would you feel about adding the following?

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:

                       // call the default formatter, forwarding `this`
                       return '$' + Chart.Ticks.formatters.numeric.apply(this, [value, index, ticks]);

@kurkle
Copy link
Member

kurkle commented Dec 15, 2021

That could be a good addition to the documentation.

Further, you can also use the ticks.format to pass options to the default formatter: https://codepen.io/kurkle/pen/gOGmXzK

https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#linear-axis-specific-tick-options

@doug-a-brunner
Copy link
Contributor Author

Cool, I'll submit a PR.

doug-a-brunner pushed a commit to hgmelectronics/Chart.js that referenced this issue Dec 15, 2021
doug-a-brunner pushed a commit to hgmelectronics/Chart.js that referenced this issue Dec 16, 2021
etimberg pushed a commit that referenced this issue Dec 16, 2021
* Docs/clarify usage of ticks callback (#9991)

* Docs/Fixed inconsistent variable names (#9991)

Co-authored-by: Doug Brunner <doug@epicycloidengr.com>
@LeeLenaleee
Copy link
Collaborator

LeeLenaleee commented Dec 16, 2021

Fixed with #9994

@digiage
Copy link

digiage commented Apr 29, 2022

That could be a good addition to the documentation.

Further, you can also use the ticks.format to pass options to the default formatter: https://codepen.io/kurkle/pen/gOGmXzK

https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#linear-axis-specific-tick-options

I am trying to change decimal separator (it is not dependent on language, but on app settings). I have tried the suggested format, but I am not sure how can I add there lang option (from Intl.NumberFormat) to simulate different decimal separator (for example 'cs' and 'en'). This is obviously wrong:

format: ('en',{style: 'decimal'})

https://codepen.io/koubin/pen/bGLbreg

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

No branches or pull requests

4 participants