I'm trying plot a time series line chart at an annual frequency. Each data point pertains an end-of-year value the x value pertains to the year end date. The x values are formatted as date strings in the "MMM-yy" format.
However, the labels do not align with the data points as shown below.

I've tried setting x-axis granularity to 8766.144 hours (that one year, as unix timestamps are converted to hours before passing to Entry object). This should set apart 1 year between each x label, right? But the chart doesn't seem to respect that either (see chart below).

So, 1. how do I align x labels to the data points on the charts and 2. is it possible to use granularity to get this done?
Got it fixed by passing the index as the x value and looking up and formatting date from a custom value formatter using the index that was passed to the x value.
Facing the same problem.

-Second plot is from Nov 17.
-The last plot supposedly is Jan 05.
Is there a way where we can exactly align the labels of X axis to the plots? Tried using setLabelcount(count,true) or setLabelCount(count) and setGranularity().
@hfahmy you literally cured my headache man <3
@hfahmy , Can you please post some code or explain what you did exactly. If possible, please provide the explanation
@akshaytaru2007 for me, instead of passing the date or time as the X value for each Entry, I just passed the index (0, 1, 2, 3, etc) and then used a value formatter to access the data at that position and display it properly. To be honest with you, I don't really understand why it works, but it does.
Something like this for adding the values:
val data = ArrayList<Entry>()
for (i in 0..23) {
data.add(Entry(i.toFloat(), Math.round(hours[i].temperature.toFloat()).toFloat()))
}
And this is what I'm using for the xAxis value formatter to display a time, in my case:
xAxis.valueFormatter = object : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
val date = Date(hours[value.toInt()].time.toInt() * 1000L)
// format of the date
val jdf = SimpleDateFormat("h a", Locale.getDefault())
jdf.timeZone = TimeZone.getTimeZone(weatherData.timezone)
return jdf.format(date)
}
}
Thanks for replying. You replied very late. But ironically, I did the same thing. I learned this the hard way(by trying and failing).
Sorry my reply was too late for you, I figured that was better than not replying at all, since the original poster wasn't replying. Glad you figured it out.
I'm losing my mind - trying to implement this, inserting values with for loop and the overridden getFormattedValue -function is receving values like: 0.0, 0.3, 0.6, 0.900004, 1.8000001.. etc, even though theres only three elements on the list!
I'm bumping this issue, since I ran across it today. ValueFormatter receives float values between elements, instead of their indexes, which makes displaying label for given points impossible. Also, there is no way to predict which values will be actually passed to getAxisLabel method - I have around 50 entries and I want to label specific ones, but I can't since getAxisLabel receives random float values between my points (exactly what @Broivula said earlier). This makes drawing a chart with date labels impossible - days are skipped, and timeline is not continuous.
Most helpful comment
@akshaytaru2007 for me, instead of passing the date or time as the X value for each Entry, I just passed the index (0, 1, 2, 3, etc) and then used a value formatter to access the data at that position and display it properly. To be honest with you, I don't really understand why it works, but it does.
Something like this for adding the values:
And this is what I'm using for the xAxis value formatter to display a time, in my case: