-
-
Notifications
You must be signed in to change notification settings - Fork 9k
The AxisValueFormatter interface
Philipp Jahoda edited this page Aug 6, 2016
·
18 revisions
Introduced in release v3.0.0, this interface allows the custom styling of both XAxis
and YAxis
values before drawing.
All that needs to be done to custom-format values on the axis is to create a class that implements the AxisValueFormatter
interface, as shown below. This formatter is used to format the values of an axis to 1 decimal digit always.
public class MyYAxisValueFormatter implements AxisValueFormatter {
private DecimalFormat mFormat;
public MyAxisValueFormatter() {
// format values to 1 decimal digit
mFormat = new DecimalFormat("###,###,###,##0.0");
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// "value" represents the position of the label on the axis (x or y)
return mFormat.format(value) + " $";
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 1; }
}
The example below shows how to plot values from a String[]
array to the axis:
public class MyYAxisValueFormatter implements AxisValueFormatter {
private String[] mValues;
public MyAxisValueFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// "value" represents the position of the label on the axis (x or y)
return mValues[(int) value];
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 0; }
}
After the creation of the formatter, simply set it to your axis of choice:
YAxis left = chart.getAxisLeft();
left.setValueFormatter(new MyYAxisValueFormatter());
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyXAxisValueFormatter());
###Legacy Formatters
Prior to release v3.0.0, there were separate formatters for XAxis
and YAxis
. The documentation for these formatters can be found here: