Skip to content

The ValueFormatter interface

Philipp Jahoda edited this page Apr 17, 2015 · 18 revisions

The ValueFormatter interface can be used to create custom-made formatter classes that can allow to format values within the chart (from DataSets) or values from the YAxis.

For using the ValueFormatter, simply create a new class and let it implement the interface and return whatever you want to be displayed from the getFormattedValue(float value) method.

Example of a custom formatter:

public class MyValueFormatter implements ValueFormatter {

    private DecimalFormat mFormat;
    
    public MyValueFormatter() {
        mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
    }
    
    @Override
    public String getFormattedValue(float value) {
        return mFormat.format(value) + " $"; // append a dollar-sign
    }
}

Then, set your formatter to the YAxis, ChartData or DataSet object:

// usage on axis
yAxis.setValueFormatter(new MyValueFormatter());

// usage on whole data object
lineData.setValueFormatter(new MyValueFormatter());

// usage on individual dataset object
lineDataSet.setValueFormatter(new MyValueFormatter());

The documentation has moved.

Clone this wiki locally