Aleksandr Gorshkov
Aleksandr Gorshkov

Reputation: 493

Display months on XAxis using MPAndroidChart

I'm developing one chart related to months and days. On my XAxis, I have days, but I need to show titles only for the first day of the month. For instance, if my range starts from 23rd November and ends on 12 January, I need to show the titles starting from 1st December, 1st January... Overall, I need to show an interval, not from the beginning of the diagram.

For now, I tried to show all values on the XAxis and format them only if this value is the first day of the month, but it sounds like a workaround.

Don't somebody know how to do that?

Upvotes: 1

Views: 579

Answers (1)

Ilya Mashin
Ilya Mashin

Reputation: 954

You need to create IAxisValueFormatter implement. Inside you need to find the first day of month. For example with JodaTime:

val startOfMonth = DateTime(Date()).withDayOfMonth(1).withTimeAtStartOfDay()

Next get your locale with:

private val locale = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)

And use SimpleDateFormat in getFormattedValue (IAxisValueFormatter implementation):

SimpleDateFormat("d MMM", locale).format(startOfMonth.toDate())

Upvotes: 1

Related Questions