Hot questions for Value Format in MPAndroidChart

MPAndroid Chart , how do i format values on chart from floats to ints, interface is depricated

Question: I want to format the values from the Line Chart , from the current floats into ints, I found that the library specifies the usage of the ValueFormatter interface , but it is deprecated so i cannot use it anymore , any ideas how I could do it?

This is how it should have looked I suppose:

dataSet.setValueFormatter(new ValueFormatter() {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return super.getFormattedValue(value, axis);
    }
});

Answer: Use getFormattedValue(float value) instead of getFormattedValue(float value, AxisBase axis) either on LineDataSet or LineData like below:

dataSet.setValueFormatter(new ValueFormatter() {
    @Override
    public String getFormattedValue(float value) {
        return String.valueOf((int)value);
    }
});

Show integer values instead of decimal in MPAndroidChart?

Question: I'm using MPAndroidChart for showing hours of work by day.

Everything is working fine, but I can't figure out how to show integer, not float. For example, if I insert 19 hours in chart, it's showing like this at the top of each chart. Can I show it like 19 h?

Answer: Set value Formatter

val xAxis = barChart.xAxis
xAxis.valueFormatter = IAxisValueFormatter { value, axis -> value.roundToInt() }

How do I remove .0 from Float

Question: Here is the code:

private fun setData(playerData: TreeMap<String, String>) {
    val values = ArrayList<Entry>()
    val graphData = java.util.ArrayList<Float>()
    for (value in playerData.values) {
        graphData.add(value.toInt().toFloat()) # <-- converting here
    }
    graphData.reverse()

    for (i in 0 until graphData.size) {

        val itemi = i.toFloat()
        values.add(
            Entry(
                itemi, #<-- entering here directly
                graphData[i],
                resources.getDrawable(R.drawable.ic_cricket)
            )
        )
    }

    val set1: LineDataSet
    set1 = LineDataSet(values, "Player Form Graph")
    val dataSets = ArrayList<ILineDataSet>()
   // Possible Solution?
    val valueFormatter = IValueFormatter { value, _, _, _ ->
        value.toInt().toString()
    }
    set1.valueFormatter = valueFormatter as ValueFormatter?
    dataSets.add(set1)
    val data = LineData(dataSets)
 // set data
    binding.chart1.data = data

}

Error:

java.lang.ClassCastException: <id>DemoFragment$setData$valueFormatter$1 cannot be cast to com.github.mikephil.charting.formatter.ValueFormatter

The thing is some values are working without .0 and some are with .0

Answer: this is what I have used:

set1.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return value.toInt().toString()
            }
        }

In case if there is a chance of value coming in decimal as well, say [1, 1.2, 3, 3.5] is your set, then go for:

set1.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return DecimalFormat("#.#").format(value)
            }
        }

in above case #.# can be replaced with #.## if you want to show UPTO 2 decimal places and so on.