If you develop some trading application, most likely, your chart will display some OHLC prices and indicators along with the corresponding date labels on X-Axis. In SciChart Axis Labels - LabelProvider API helps you to present these labels in the desired format. Quite a popular scenario in trading charts is when date labels formatting changes depending on the current zoom level, so the deeper zoom level is, the more detailed dates appear.
NOTE: Example of the trading charts label formatter usage can be found in the “Multi-Panel Stock Chart” example in the SciChart iOS Examples Suite as well as on GitHub:
The most suitable type of X-Axis for trading charts is SCICategoryDateAxis. It uses SCITradeChartAxisLabelProvider to dynamically change its Text and Cursor Labels depending on Data-value and current zoom.
Each of them formats dates depending on the current granularity using -[ISCICalendarUnitDateFormatter formatDate:withCalendarUnit:] method. By default, there are few predefined formatters, so you will get dynamically changing axis labels out-of-the-box. In case, the default one doesn’t meet your requirements, you can provide your own and customize your label provider. Please, continue reading to find out, how to do it.
Custom Trade Chart Label Provider
As an example of a Trade Chart Label Provider customization, let’s create a custom one with some different formatters.
First, we need to subclass SCICalendarUnitDateFormatter and implement a protected method -[SCICalendarUnitDateFormatter createFormatterForCalendarUnit:] which will return a formatter with the desired date format depending on the current calendar unit. Here is, how it might look in code:
import SciChart.Protected.SCICalendarUnitDateFormatter
class CustomCalendarUnitDateFormatter: SCICalendarUnitDateFormatter {
override func createFormatter(for calendarUnit: NSCalendar.Unit) -> DateFormatter {
var dateFormat = “”
if calendarUnit.contains(.year) {
dateFormat.append(“ yyyy”)
}
if calendarUnit.contains(.month) {
dateFormat.append(“ MMMM”)
}
if calendarUnit.contains(.day) {
dateFormat.append(“ EEEE, dd MMM”)
}
let formatter = DateFormatter()
formatter.dateFormat = dateFormat
return formatter
}
}
public class CustomCalendarUnitDateFormatter : SCICalendarUnitDateFormatter
{
protected override NSDateFormatter CreateFormatterForCalendarUnit(NSCalendarUnit calendarUnit)
{
var dateFormat = “”;
if ((calendarUnit & NSCalendarUnit.Year) > 0)
{
dateFormat += “ yyyy”;
}
if ((calendarUnit & NSCalendarUnit.Month) > 0)
{
dateFormat += “ MMMM”;
}
if ((calendarUnit & NSCalendarUnit.Day) > 0)
{
dateFormat += “ EEEE, dd MMM”;
}
var dateFormatter = new NSDateFormatter()
{
DateFormat = dateFormat
};
return dateFormatter;
}
}
NOTE: In case using createFormatterForCalendarUnit method to return your custom formatter doesn’t fit your needs and you want to have full control on formatting your axis labels, use -[ISCICalendarUnitDateFormatter formatDate:withCalendarUnit:] method. Please, continue reading to see how you can use this method.
Let’s also create some custom SCICursorModifier axis label formatter which will format cursor axis label depending on current granularity. So, for example, by default we want our Cursor to show a date in a format, like “2018 Jun 17” but when we zoom close enough - it should become something like “Monday, 10 June”.
public class CustomTradeChartLabelFormatter : SCITradeChartAxisLabelFormatter
{
public CustomTradeChartLabelFormatter() : base(new CustomCalendarUnitDateFormatter(), new CustomCursorCalendarUnitDateFormatter())
{
}
}
class CustomTradeChartLabelProvider: SCITradeChartAxisLabelProvider {
init() {
super.init(labelFormatter: CustomTradeChartLabelFormatter())
}
}
…
let xAxis = SCICategoryDateAxis()
xAxis.labelProvider = CustomTradeChartLabelProvider()
let pinchZoomModifier = SCIPinchZoomModifier()
pinchZoomModifier.direction = .xDirection
let cursorModifier = SCICursorModifier()
cursorModifier.receiveHandledEvents = true
surface.chartModifiers.add(items: pinchZoomModifier, cursorModifier)
public class CustomTradeChartLabelProvider : SCITradeChartAxisLabelProvider
{
public CustomTradeChartLabelProvider() : base(new CustomTradeChartLabelFormatter())
{
}
}
…
var xAxis = new SCICategoryDateAxis {
LabelProvider = new CustomTradeChartLabelProvider()
}
surface.ChartModifiers = new SCIChartModifierCollection
{
new SCIPinchZoomModifier { Direction = SCIDirection2D.XDirection },
new SCICursorModifier { ReceiveHandledEvents = true }
};
Here are the results in the normal and slightly zoomed states:
In case such a customization doesn’t fit your needs and you need some completely different Label Provider you can always create your own. You will find more details in Axis Labels - LabelProvider API article.