Back to Devexpress

AxisLabel.Formatter Property

wpf-devexpress-dot-xpf-dot-charts-dot-axislabel-38498dab.md

latest5.5 KB
Original Source

AxisLabel.Formatter Property

Gets or sets an object that formats axis labels.

Namespace : DevExpress.Xpf.Charts

Assembly : DevExpress.Xpf.Charts.v25.2.dll

NuGet Package : DevExpress.Wpf.Charts

Declaration

csharp
[Browsable(false)]
public IAxisLabelFormatter Formatter { get; set; }
vb
<Browsable(False)>
Public Property Formatter As IAxisLabelFormatter

Property Value

TypeDescription
IAxisLabelFormatter

An object of a class that implements the IAxisLabelFormatter interface.

|

Remarks

Use the Formatter property if you need to generate text strings for axis labels based on a custom condition or change an order of magnitude of axis label values.

Formatter has higher priority than the AxisLabel.TextPattern property value. For this reason, the AxisLabel.TextPattern value is ignored if Formatter is specified.

Example

Perform the steps below to create a formatter:

  • Create a class that implements the IAxisLabelFormatter interface and uses the interface’s GetAxisLabelText method.
  • Assign the newly created class’s instance to the AxisLabel.Formatter property.

How to: Format Numeric Axis Labels

This example applies a custom format to the numeric axis’s labels.

A formatter applies to axis labels:

A formatter does not apply to axis labels:

Markup:

xaml
<dxc:XYDiagram2D.AxisX>
    <dxc:AxisX2D>
        <dxc:AxisX2D.Label>
            <dxc:AxisLabel>
                <dxc:AxisLabel.Formatter>
                    <local:AxisLabelFormatter/>
                </dxc:AxisLabel.Formatter>
            </dxc:AxisLabel>
        </dxc:AxisX2D.Label>
    </dxc:AxisX2D>
</dxc:XYDiagram2D.AxisX>
<dxc:XYDiagram2D.AxisY>
    <dxc:AxisY2D>
            <dxc:AxisLabel>
                <dxc:AxisLabel.Formatter>
                    <local:AxisLabelFormatter/>
                </dxc:AxisLabel.Formatter>
            </dxc:AxisLabel>
        </dxc:AxisY2D.Label>
    </dxc:AxisY2D>
</dxc:XYDiagram2D.AxisY>

Code-Behind:

csharp
public class AxisLabelFormatter : IAxisLabelFormatter {
    public string GetAxisLabelText(object axisValue) {
        return Convert.ToString((double)axisValue / 1000);
    }
}
vb
Public Class AxisLabelFormatter
    Implements IAxisLabelFormatter
    Public Function GetAxisLabelText(ByVal axisValue As Object) As String
        Return Convert.ToString(CDbl(axisValue) / 1000)
    End Function
End Class

How to: Format Date-Time Axis Labels

This example formats date-time axis labels as follows:

A formatter applies to axis labels:

A formatter does not apply to axis labels:

Markup:

xaml
<dxc:XYDiagram2D.AxisX>
    <dxc:AxisX2D>
        <dxc:AxisX2D.DateTimeScaleOptions>
            <dxc:ManualDateTimeScaleOptions AutoGrid="False" 
                                            GridAlignment="Day" 
                                            GridSpacing="7"/>
        </dxc:AxisX2D.DateTimeScaleOptions>
        <dxc:AxisX2D.Label>
            <dxc:AxisLabel>
                <dxc:Axis2D.ResolveOverlappingOptions>
                    <dxc:AxisLabelResolveOverlappingOptions AllowHide="False"/>
                </dxc:Axis2D.ResolveOverlappingOptions>
                <dxc:AxisLabel.Formatter>
                    <local:AxisLabelFormatter/>
                </dxc:AxisLabel.Formatter>
            </dxc:AxisLabel>
        </dxc:AxisX2D.Label>
    </dxc:AxisX2D>
</dxc:XYDiagram2D.AxisX>

Code-Behind:

csharp
public class AxisLabelFormatter : IAxisLabelFormatter {
    public string GetAxisLabelText(object axisValue) {
        DateTime value = (DateTime)axisValue;
        return ((value.Day >= 1) && (value.Day <= 7)) ? $"{value.ToString("MMM, d")}" : $"{value.Day}";
    }
}
vb
Public Class AxisLabelFormatter
    Implements IAxisLabelFormatter

    Public Function GetAxisLabelText(ByVal axisValue As Object) As String
        Dim value As DateTime = CType(axisValue, DateTime)
        Return If(((value.Day >= 1) AndAlso (value.Day <= 7)), $"{value.ToString("MMM, d")}", $"{value.Day}")
    End Function
End Class

See Also

AxisLabel.TextPattern

AxisLabel Class

AxisLabel Members

DevExpress.Xpf.Charts Namespace