Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

android - Add x axis as datetime label in MPAndroidChart? (Kotlin)

I believe this question and answer explains how to format time series data into readable date labels in Java. How do you do the same thing in Kotlin?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could create a custom formatter class extending the IAxisValueFormatter:

class MyCustomFormatter() : IAxisValueFormatter 
{
    override fun getFormattedValue(value: Float, axis: AxisBase?): String
    {   
        val dateInMillis = value.toLong()
        val date = Calendar.getInstance().apply {
            timeInMillis = dateInMillis
        }.time

        return SimpleDateFormat("dd MMM", Locale.getDefault()).format(date)
    }
}

Then assign it to your chart with

    chart?.xAxis?.valueFormatter = MyCustomFormatter()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...