SciChart Android 2D Charts API > Axis APIs > Axis Labels - TextFormatting and CursorTextFormatting
Axis Labels - TextFormatting and CursorTextFormatting

Axis Labels Formatting

All the axis classes obey standard Java formatting strings, calling methods of the Java Format API internally. Thus, standard Date and Time and Numeric pattern strings can be applied to format axis labels. There is the setTextFormatting() method declared in IAxisCore for this purpose. Also, axis axis labels formatting is applied to AxisMarkerAnnotation labels.

There are some examples of possible formatting strings for NumericAxis: "%+,8f", "#,##0.###", "$###,###.###", "0.#####E0".

And the following ones are examples of valid formatting strings for DateAxis: "dd.MM.yy", "EEE, MMM d,' 'yy", "K:mm a,z", "h:mm a".

Axis Labels Formatting can be set in code as shown below:

// Create an axis and set a formatting string
IAxis xAxis = new DateAxis(getActivity());
 xAxis.setTextFormatting("dd.MMM");

// Create an axis and set a formatting string
IAxis yAxis = new NumericAxis(getActivity());
 yAxis.setTextFormatting("$0.0000");

The code above results in axis labels being formatted as shown below:

 

Axis Cursor Text Formatting

Axis API allows to assign a formatting string for axis overlays, such as Cursor axis labels. Similarly to axis labels formatting, there is the setCursorTextFormatting() method for this. Likewise, it uses the same Java Format API internally, so everything aforementioned applies to the cursor labels as well.

Axis Overlays Formatting can be set in code as shown below:

// Create an axis and set a formatting string for modifiers' axis labels
IAxis xAxis = new DateAxis(getActivity());
xAxis.setCursorTextFormatting("'X Value:'dd.MM.yyyy");

// Create an axis and set a formatting string for modifiers' axis labels
IAxis yAxis = new NumericAxis(getActivity());
 yAxis.setCursorTextFormatting("'Price:'###.##' $'");

 The code above results in cursor labels being formatted as shown below:

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 method called setScientificNotation() for this purpose, which accepts a ScientificNotation enum member. Possible options are listed below:

An axis can be configured to use ScientificNotation like follows:

LogarithmicNumericAxis axis = new LogarithmicNumericAxis(getActivity());

axis.setLogarithmicBase(10d);
 axis.setScientificNotation(ScientificNotation.LogarithmicBase);

axis.setTextFormatting("#.#E+0");
 axis.setCursorTextFormatting("#.#E+0");

 The code above will result in cursor labels and labels on axes being formatted as shown below:

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.

See Also