-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Charts 3.0.1 - Align x labels (dates) with plots #2094
Comments
I ran in the same issue using timestamps before and it is caused by what is being rendered on the x-axis is an interpolated timestamp. This basically means that the timestamps resulting in the labels using a dateFormatter are not the timestamps for your scores, causing the labels to not match up. For example, let's assume you have x values [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The x-axis renderer might actually print 2.5 as a label trying to evenly distribute the labels on the x-axis, even though that particular x-value is not in your dataset. If you then consider you are using timestamps this means the dateFormatter may format some intermediate timestamp as a date. As you format by day this results in your labels not matching up with your scores even though the label is correct (as you basically round it off by day). If you were to format by day hour:minute you would see the label is indeed an interpolated date. I solved the issue on my end by resorting to using array indexes instead of using timestamps for the x-axis and using a custom X-axis formatter:
|
Thanks a lot for your answer! Thanks! |
@4np I guess that you then have to define your array of ChartDataEntry with xValues as indices too, instead of time intervals, right ?
|
@4np OK, that worked! My chart is now the same as in version 2. But, I had to add 2 more things to my xAxis:
Thanks for your help. If you have a SO account and want to copy / paste your answer there, I'd be glad to accept your answer there too. Thanks |
Also, I left this issue open until the Charts devs have a chance to look at it, and maybe introduce a way to choose how the labels should be aligned. Because I now have to choose between 2 poor solutions:
Please let us know what you think. |
I managed to solve the problem (TimeInSeconds - miniDate) / (3600.0 * 24.0) And all the trick is in the formatter |
@thierryH91200 Thanks Thierry ! |
It worked! Fantastic! |
Cool @thierryH91200, that does indeed seem the way to go forward 👍 |
This does not work for me |
@frederic-adda did you find the solution for your issue?? Here is my code for setting ChartDataEntry's X-value as TimeInterval
|
Trying to get the above solutions working when using multiple LineChartDataSets on the one graph, has anyone been able to get it successfully working? I'm having problems finding a solution when one dataset might differ in size compared to the other.
|
I am having a hard time to migrate Charts from version 2 (Swift 2.3) to 3 (Swift 3).
Basically, I can't have the x labels (dates) aligned correctly with the corresponding plots.
This is what I had before in version 2:
https://i.stack.imgur.com/wOL3t.png
In version 2, I had values for days 7, 8, 10 and 11. So I was missing a day in the middle, but the labels were correctly alined with the plots.
And here is what I have in version 3:
https://i.stack.imgur.com/arIL2.png
In version 3, the "labels" in the x axis have now been replaced by double (for dates, it's a timeInterval since 1970), and formatted via a formatter. So, indeniably, the graph is more "correct" now, since the chart correctly extrapolates the value for the 9th, but I can't find how to put the labels under the corresponding plots.
This is my code for the x axis:
Here is the ChartXAxisFormatter class I created:
So, the values here are correct, the formatting is correct, the shape of the chart is correct, but the alignment of the labels with the corresponding plots is not good.
Thanks for your help
The text was updated successfully, but these errors were encountered: