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

Wish to have yaxis 0 tick always showing, but forceNiceScale does not seem to work properly with min/max values -1 < value < 1 #4283

Open
ehbats opened this issue Feb 29, 2024 · 4 comments

Comments

@ehbats
Copy link

ehbats commented Feb 29, 2024

Description

Hello guys,

I am currently facing a problem where I can't force my chart to always have the 0 tick on the y-axis. I can't set my min to 0 because my series has negative values. I tried using forceNiceScale but it does not seem to solve my problem when i pass custom min/max values. With custom min/max values (that are -1 < value < 1) the 0 tick behavior looks random and I haven't found a way to consistenly make the 0 tick appear. I tried taking a look at #1771 but using annotation does not really solve my problem, and my problem is not solved by the OP's solution probably because of my very small values series.

Steps to Reproduce

  1. Create a Bar Chart with a series that includes values that are -1 < value < 1 (having negative values is important).
  2. Use custom dataLabels formatting that shows the labels above the bar as this is the desired appearance for the user
  3. Use forceNiceScale in attempt to always show the 0 tick on the yaxis
  4. Pass custom min/max values to better show the dataLabels
  5. Notice how yaxis 0 tick seems to only randomly appear depending on the values of min/max when these values are very small.
  6. See codepen links at the end for more detailed reproduction

Expected Behavior

Consistent yaxis always showing the 0 tick when passing min/max values and forceNiceScale

Actual Behavior

Yaxis seems to randomly show the 0 tick when I pass min/max values that are 0 < value < 1

Screenshots

Yaxis 0 tick not showing when i pass small min/max values:
image

Without the min/max values the 0 tick shows up, but my datalabels formatting on the -0.08% series value gets messy:
image

Reproduction Link

This codepen shows how the chart does not properly show the y-axis 0 tick when I pass min/max values that are 0 < value < 1:
https://codepen.io/Eduardo-Batistella/pen/xxexqOQ
You may change the values of min/max and see how the yaxis ticks behavior looks a bit random with such small values.

This other codepen shows how the chart (apparently) correctly formats the y axis with the 0 tick when i do not pass min/max values:
https://codepen.io/Eduardo-Batistella/pen/xxexqYe
However, this is not good enough for me as it messes with my dataLabels formatting on the -0.08% value. The bar hits the limit of the y-axis and my label undesirely goes inside the bar.

Can you guys help me fix this problem please? I've been trying to fix it for a while now and haven't found a solution

@ehbats ehbats added the bug Something isn't working label Feb 29, 2024
@rosco54
Copy link
Contributor

rosco54 commented Feb 29, 2024

By setting max: 0.11 and min: -0.1, you set the range (0.21), and this must be divided into even spacings. So you get either 7 divisions of 0.03 (most likely) or 3 divisions of 0.07 (unlikely), and neither of those land on zero.

You could try the following:

max: 0.12,
min: -0.1
which is guaranteed to produce divisions that land on zero (range 0.22 = 11 divisions of 0.02 spacing, or

max: 0.12,
min: -0.12,
which is almost guaranteed to land on zero.

PS. This is not a bug. The documentation needs to be updated but even in it's current form doesn't imply that it will guarantee a zero tick. As of 3.46.0, user options are honored if self-consistent but an option will be tweaked or ignored if it's in conflict with other user-defined options. Min and/or max are always honored, then stepSize and tickAmount, in roughly that order of priority. For example, setting min: 0, max: 10 and stepSize: 3 is in conflict, so stepSize would be tweaked to fit. ForceNiceScale is used to choose between various alternative outcomes under certain conditions.

@ehbats
Copy link
Author

ehbats commented Feb 29, 2024

@rosco54 Thanks a lot for your quick answer! These infos will sure help me out a lot trying to figure this out. I tried tweaking my code a little bit more and noticed that even when nor min or max are passed, some of my charts with small values still don't land on 0. Do you think it would be a nice feature to have some way of forcing the 0 to show up? I think it would make some charts a lot easier to read. In this case i misstagged my issue and I apologize, it should have been a feature request. But still, do you think that would be possible?

I think it would be a lot more friendly if that existed instead of having to add extra complexity on my code to do these kinds of maths to have a chance that it might land on 0

@rosco54
Copy link
Contributor

rosco54 commented Mar 1, 2024

Yes, it would be nice, and the unrestricted auto scaling generally does that. Indirectly though, as a side effect of how it does the scaling, so it's not 100% guaranteed even when no user-defined options are present. The solution in that case, may be to increase the margin space on the chart area so that you don't have to try to make allowance for it using min or max settings.

@rosco54
Copy link
Contributor

rosco54 commented Mar 1, 2024

@ehbats Would you be able to add one of those "charts with small values [that] still don't land on 0" to this issue? One of those that don't include min or max. I would like to see how that is happening and I can't seem to conjure up an example of my own that doesn't land on zero.

@junedchhipa junedchhipa removed the bug Something isn't working label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants