Search code examples
androidkotlinmpandroidchart

MPAndroidChart BarChart - ValueFormatter is not applied


I use MPAndroid to use Group Bar chart.

I create ValueFormatter.

val StepValueFormatter = object : ValueFormatter() {
    override fun getFormattedValue(
        value: Float,
        entry: Entry?,
        dataSetIndex: Int,
        viewPortHandler: ViewPortHandler?
    ): String {
        var oneText = value.toInt() * 1000
        var decimal = DecimalFormat("#,###")
        var twoText = decimal.format(oneText)

        return "${twoText} 걸음"
    }
}

And I applied this ValueFormatter to BarDataSet.

var bds1 = BarDataSet(entryOne, "기준")
bds1.setColor(Color.YELLOW);
bds1.valueTextSize = 16f
bds1.valueTypeface = Typeface.DEFAULT_BOLD
bds1.valueTextColor = Color.BLACK
bds1.valueFormatter = StepValueFormatter

However BarChart graph value format is not changed. What is the problem here? Please help me.

enter image description here


Solution

  • The getFormattedValue overloading you're using is deprecated. Please use the one that accepts a float value:

    val StepValueFormatter = object : ValueFormatter() {
        override fun getFormattedValue(value: Float): String {
            val oneText = value.toInt() * 1000
            val decimal = DecimalFormat("#,###")
            val twoText = decimal.format(oneText)
    
            return "${twoText} 걸음"
        }
    }