Available since v1.6.2 – changed (improved) in v2.1.4

Tutorial Video

In case you want to learn more about formatting values without reading too much, you can check out our tutorial-video explaining the topic of formatting values in the chart in detail.

ValueFormatter

The ValueFormatter class can be used to create custom-made formatter classes that allow to format values within the chart (from DataSets or from the axes) in a specific way before drawing them.

For using the ValueFormatter, simply create a new class that extends it and then override whatever functions you need. The ValueFormatter provides different functions for formatting entries from the individual chart types and axes.

If no function is overridden, the ValueFormatter will return the default formatting which is usually a String representation of the y value of the entry (or axis label).

Creating a ValueFormatter (Charts and YAxis)

The following formatter provides customized formatting for Line- and BarChart entries, as well as for formatting axis (YAxis) values:

class MyValueFormatter : ValueFormatter() {

    private val format = DecimalFormat("###,##0.0")

    // override this for e.g. LineChart or ScatterChart
    override fun getPointLabel(entry: Entry?): String {
        return format.format(entry?.y)
    }

    // override this for BarChart
    override fun getBarLabel(barEntry: BarEntry?): String {
        return format.format(barEntry?.y)
    }

    // override this for custom formatting of XAxis or YAxis labels
    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
        return format.format(value)
    }

    // ... override other methods for the other chart types
}

Creating a ValueFormatter (XAxis)

The following formatter is designed for formatting the values of the XAxis with days of the week. Notice that the axis value is safely casted to integer and used as the array index. Also, you need to make sure the length of the array corresponds to the range of values the chart displays on the XAxis.

class MyXAxisFormatter : ValueFormatter() {

    private val days = arrayOf("Mo", "Tu", "Wed", "Th", "Fr", "Sa", "Su")

    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
        return days.getOrNull(value.toInt()) ?: value.toString()
    }
}

Then, set your formatter to the ChartData or DataSet object for formatting data values:

// usage on whole data object (e.g. LineData)
lineData.setValueFormatter(MyValueFormatter())

// usage on individual dataset object (e.g. LineDataSet)
lineDataSet.valueFormatter = MyValueFormatter()

Or, set your formatter on the XAxis or YAxis for formatting axis values:

// XAxis
chart.xAxis.valueFormatter = MyXAxisFormatter()

// left YAxis
chart.leftAxis.valueFormatter = MyValueFormatter()

Predefined Formatters

  • LargeValueFormatter: Can be used for formatting large values > “1.000”. It will turn values like “1.000” into “1k”, “1.000.000” will be “1m” (million), “1.000.000.000” will be “1b” (billion) and values like one trillion will be e.g. “1t”.
  • PercentFormatter: Used for displaying a “%” sign after each value with 1 decimal digit. Especially useful for the PieChart. 50 -> 50.0 %
  • StackedValueFormatter: A formatter specifically designed to be used with stacked BarChart. It allows to specify whether all stack values should be drawn or just the top value.

Important notice

Since release v3.1.0, there are no longer any separate formatters for axis values and chart data values, they are both now formatted by ValueFormatter.

Close Menu