Axis Labels - TextFormatting and CursorTextFormatting
All the axis classes obey standard Cocoa formatting strings, calling methods of the Formatter APIs internally. Thus, standard Date Formatters and Number Formatters strings patterns can be applied to format axis labels. There are the setTextFormatting(String textFormatting) and setCursorTextFormatting(String cursorTextFormatting) properties for this purpose.
Note
DateFormat and NumberFormat relies on Unicode Technical Standard #35
See possible string patterns provided by UTS#35
Axis Labels Formatting
Axis API allows to assign a formatting string for axis labels. There is the setTextFormatting(String textFormatting) property for this. Such formatting can be set in code as shown below:
final DateAxis xAxis = new DateAxis(getContext());
xAxis.setTextFormatting("dd.MMM");
final NumericAxis yAxis = new NumericAxis(getContext());
yAxis.setTextFormatting("$0.0");
Note
Axis labels formatting is also applied to AxisMarkerAnnotation labels.
Axis Cursor Text Formatting
Axis API allows to assign a formatting string for axis overlays, such as CursorModifier axis labels. Similarly to axis labels formatting, there is the setCursorTextFormatting(String cursorTextFormatting) property for this. Such formatting can be set in code as shown below:
final DateAxis xAxis = new DateAxis(getContext());
xAxis.setCursorTextFormatting("'X Value:'dd.MM.yyyy");
final NumericAxis yAxis = new NumericAxis(getContext());
yAxis.setCursorTextFormatting("'Price:'###.##' $'");
Numeric Axes and Scientific Notation
It is possible to render axis labels and text in tooltips in a shortened form using scientific notation (standard form). This becomes an issue when working with large numbers. In this case axis size will grow to fit axis labels inside. This is a common issue when working with LogarithmicNumericAxis.
To configure an axis to show numbers in this form, it is necessary that a proper format string is provided and ScientificNotation is set to the desired value. There is a scientificNotation property for this purpose. Possible options are listed below:
- ScientificNotation.None - the default value.
- ScientificNotation.Normalized - assumes 10 as base.
- ScientificNotation.E - assumes the number E as base.
- ScientificNotation.LogarithmicBase - used with LogarithmicNumericAxis. Assumes the logarithmic base of the LogarithmicNumericAxis as base.
An axis can be configured to use ScientificNotation like follows:
LogarithmicNumericAxis xAxis = new LogarithmicNumericAxis(getContext());
xAxis.setLogarithmicBase(10.0);
xAxis.setScientificNotation(ScientificNotation.LogarithmicBase);
xAxis.setTextFormatting("#.#E+0");
xAxis.setCursorTextFormatting("#.#E+0");
Dynamically Changing TextFormatting
For more advanced formatting scenarios, Axis API provides a feature called LabelProviders. It grants full control over text output of every axis label. This can be useful if required to customize textual representation of particular axis labels or replace all of them with other strings based on some logics. Please refer to the LabelProvider API article for further details.