New to Telerik UI for WinForms? Download free 30-day trial

Custom labels text

RadChartView allows you to easily change the axes labels text by using a custom format provider class. This class must implement the IFormatProvider and ICustomFormatter interfaces. The key point in this class is that the Format method is called for each label and its "arg" parameter contains the current label text. The returned value will represent the new label.

Example 1: Changing the labels' texts to more human readable ones

Label Format

public class MyFormatProvider : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        return this;
    }
    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        string s = arg.ToString();
        switch (s)
        {
            case "0":
                return "0 seconds";
            case "30":
                return "1/2 min";
            case "60":
                return "1 min";
            case "90":
                return "90 seconds";
        }
        return null;
    }
}

Class MyFormatProvider
    Implements IFormatProvider
    Implements ICustomFormatter
    Public Function GetFormat(formatType As Type) As Object Implements IFormatProvider.GetFormat
        Return Me
    End Function
    Public Function Format(format__1 As String, arg As Object, formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
        Dim s As String = arg.ToString()
        Select Case s
            Case "0"
                Return "0 seconds"
            Case "30"
                Return "1/2 min"
            Case "60"
                Return "1 min"
            Case "90"
                Return "90 seconds"
        End Select
        Return Nothing
    End Function
End Class

Then you can just change the horizontal axis LabelFormatProvider by using the corresponding property.

Assign Format Provider

LinearAxis horizontalAxis = radChartView1.Axes.Get<LinearAxis>(0);
horizontalAxis.LabelFormatProvider = new MyFormatProvider();

Dim horizontalAxis As LinearAxis = RadChartView1.Axes.[Get](Of LinearAxis)(0)
horizontalAxis.LabelFormatProvider = New MyFormatProvider()

Figure 1: Format Provider

WinForms RadChartView Format Provider

Example 2: Showing the date part of a label only on day changes

DateTime Format Provider

public class DateTimeFormatProvider : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        return this;
    }
    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        DateTime val = (DateTime)arg;
        if (val.Hour == 0)
        {
            return val.ToShortDateString();
        }
        else
        {
            return val.ToString("H\\h");
        }
    }
}

Class DateTimeFormatProvider
    Implements IFormatProvider
    Implements ICustomFormatter
    Public Function GetFormat(formatType As Type) As Object Implements IFormatProvider.GetFormat
        Return Me
    End Function
    Public Function Format(format__1 As String, arg As Object, formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
        Dim val As DateTime = DirectCast(arg, DateTime)
        If val.Hour = 0 Then
            Return val.ToShortDateString()
        Else
            Return val.ToString("H\h")
        End If
    End Function
End Class

Again you can just change the horizontal axis LabelFormatProvider by using the corresponding property.

Assign DateTime Format Provider

DateTimeContinuousAxis dateTimeAxis = new DateTimeContinuousAxis();
dateTimeAxis.LabelFormatProvider = new DateTimeFormatProvider();

Dim dateTimeAxis As New DateTimeContinuousAxis()
dateTimeAxis.LabelFormatProvider = New DateTimeFormatProvider()

Figure 2: DateTime Format Provider

WinForms RadChartView DateTime Format Provider

The above provider implementation is applicable only to axes working with DateTime objects ( DateTimeContinuousAxis and DateTimeCategoricalAxis ).

See Also

In this article