Mpandroidchart: Understanding 2 label concepts

Created on 3 Mar 2017  Â·  7Comments  Â·  Source: PhilJay/MPAndroidChart

I am having 2 problems with my MPAndroidChart 3.0.1 graphs and since I can't find the programatically answer I am trying to understand on a concept level how things work so I can try to workout my code. By the way my charts are time series with temperature as Y value and timestamps as X values.

Basically this is problem 1 that I don't understand. When I plot a chart, even if I don't set ANY xAxis value formatter, I get vertical grid lines that are not equally spaced, and are not exclusively marking my Y values. See:
screen shot 2017-03-03 at 15 18 09
So regarding this issue, my goal was to either have these lines equally spaced representing fixed time periods, OR, ONLY to mark my Y values, AND NOT random spots where I don't even have a Y value.

Then my issue number 2 that I would like to understand conceptually. I have a database with 2 columns, timestamps and temperature. When I import these values to the application, they come perfectly as they should be. Example:

I/App: Device 17AB05 | Timestamp 2017-02-27 22:09:55.0 | Temp 23.0
I/App: Device 17AB05 | Timestamp 2017-02-27 22:13:54.0 | Temp 21.0
I/App: Device 17AB05 | Timestamp 2017-02-27 22:17:44.0 | Temp 19.0
I/App: Device 17AB05 | Timestamp 2017-02-27 22:20:44.0 | Temp 24.0
I/App: Device 17AB05 | Timestamp 2017-02-27 22:28:44.0 | Temp 30.0
I/App: Device 17AB05 | Timestamp 2017-02-27 22:33:44.0 | Temp 27.0

Then I add these values as Entry to my LineChart, however, when plotted all the timestamps are messed up. For instance, my first temperature 23 degrees comes in chart with timestamp of 22:09:20 - therefore 33 seconds deviation. I thought that this could be to precision loss when converting timestamp to epoch and then back, however I confirmed it is not the case, my methods convert with precision back and forth. I really don't know why this happens.

If you have any idea of the whys of my issues 1 or 2 I would be really glad with an input.

Thank you!

Most helpful comment

@Jetz72 I just implemented your suggestion for the time offset, and fixed both issues! Thank you so much! Honestly, thanks for replying!

All 7 comments

Issue 2 sounds like it's the conversion to floating point values. They're really not suited for storing dates, which are normally kept as longs. They lose precision at larger values, such as the 1.4 trillion milliseconds and counting since the epoch.

My current workaround is to keep a timestamp long value apart from the graph when I start recording data, and subtract that value from every Date before I convert it to a float for the Entry object. Then in a value formatter for the label, I convert the float back to a long, add the start time back in, and then create a Date and format it. For large windows of time it may also help to cut off the milliseconds entirely. I also keep a HashMap from X values to the original data, so when users tap on a point and the details are displayed, there's no chance of lost information.

@Jetz72 thanks for the suggestion I will implement and let you know how it goes. Do you have any insights for the issue 1?

Thanks!

I believe not setting an xAxis value formatter will leave you stuck with the default, which is just showing it as a big number. You'll probably want a custom value formatter as discussed above that will take the floats in the entries, process them back into dates, and use a DateFormat to display them how you want.

As for the weird spacing, I'm not sure. It may be related to the large floating point precision. I'd start with fixing the formatting. If that doesn't work, maybe try xAxis.setLabelCount(int count).

@Jetz72 I just implemented your suggestion for the time offset, and fixed both issues! Thank you so much! Honestly, thanks for replying!

Hey @fabiolanza . How did you set the labels of only the entries in your dataset? I'm stuck on this since the intervals and the corresponding labels are auto-generated by default.

I would like to have labels only for my entries. Any idea how to achieve this? I have tried playing around with custom axis formatters to no avail.

I am at work now let me try to answer you tonight. Cheers!

Sent from my iPhone

On 19 Apr 2017, at 08:20, rohangoqii notifications@github.com wrote:

Hey @fabiolanza . How did you set the labels of only the entries in your dataset? I'm stuck on this since the intervals and the corresponding labels are auto-generated by default.

I would like to have labels only for my entries. Any idea how to achieve this? I have tried playing around with custom axis formatters to no avail.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Hey @fabiolanza. I found this PR which was really helpful.

https://github.com/PhilJay/MPAndroidChart/pull/2692

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SutharRohit picture SutharRohit  Â·  3Comments

JungYongWook picture JungYongWook  Â·  3Comments

blotfi picture blotfi  Â·  3Comments

DarkHelmet67 picture DarkHelmet67  Â·  3Comments

ChenZeFengHi picture ChenZeFengHi  Â·  3Comments