corelibraries-devexpress-dot-xtracharts-dot-axislabel-36ac9080.md
Gets or sets an object that formats axis labels.
Namespace : DevExpress.XtraCharts
Assembly : DevExpress.XtraCharts.v25.2.dll
NuGet Package : DevExpress.Charts
[Browsable(false)]
public IAxisLabelFormatter Formatter { get; set; }
<Browsable(False)>
Public Property Formatter As IAxisLabelFormatter
| Type | Description |
|---|---|
| IAxisLabelFormatter |
An object of a class that implements the IAxisLabelFormatter interface.
|
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.
Perform the steps below to create a custom formatter for axis labels:
AxisLabel.Formatter property.This example shows how to apply a custom format to a numeric axis’s labels.
A formatter applies to axis labels:
A formatter does not apply to axis labels:
private void OnFormLoad(object sender, EventArgs e) {
XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
AxisLabelFormatter formatter = new AxisLabelFormatter();
diagram.AxisX.Label.Formatter = formatter;
diagram.AxisY.Label.Formatter = formatter;
}
public class AxisLabelFormatter : IAxisLabelFormatter {
public string GetAxisLabelText(object axisValue) {
return Convert.ToString((double)axisValue / 1000);
}
}
Private Sub OnFormLoad(ByVal sender As Object, ByVal e As EventArgs)
Dim diagram As XYDiagram = CType(chartControl1.Diagram, XYDiagram)
Dim formatter = New AxisLabelFormatter
diagram.AxisX.Label.Formatter = formatter
diagram.AxisY.Label.Formatter = formatter
End Sub
Public Class AxisLabelFormatter
Inherits IAxisLabelFormatter
Public Function GetAxisLabelText(ByVal axisValue As Object) As String
Return Convert.ToString(CDbl(axisValue) / 1000)
End Function
End Class
This example shows how to format the date-time axis labels as follows:
A formatter applies to the x-axis’s labels:
A formatter does not apply to axis labels:
private void Form1_Load(object sender, EventArgs e) {
XYDiagram diagram = chartControl.Diagram as XYDiagram;
// Configure scale options.
DateTimeScaleOptions dateTimeScaleOptions = diagram.AxisX.DateTimeScaleOptions;
dateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
dateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
dateTimeScaleOptions.GridSpacing = 7;
dateTimeScaleOptions.WorkdaysOnly = true;
// Apply a custom format to axis labels.
AxisLabel axisLabel = diagram.AxisX.Label;
axisLabel.Formatter = new AxisLabelFormatter();
}
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}";
}
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim diagram As XYDiagram = TryCast(chartControl.Diagram, XYDiagram)
' Configure scale options.
Dim dateTimeScaleOptions As DateTimeScaleOptions = diagram.AxisX.DateTimeScaleOptions
dateTimeScaleOptions.ScaleMode = ScaleMode.Continuous
dateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day
dateTimeScaleOptions.GridSpacing = 7
dateTimeScaleOptions.WorkdaysOnly = True
' Apply a custom format to axis labels.
Dim axisLabel As AxisLabel = diagram.AxisX.Label
axisLabel.Formatter = New AxisLabelFormatter
End Sub
Public Class AxisLabelFormatter
Inherits IAxisLabelFormatter
Public Function GetAxisLabelText(ByVal axisValue As Object) As String
Dim value As Date = axisValue
Return If(value.Day >= 1 AndAlso value.Day <= 7, $"{value.ToString("MMM, d")}", $"{value.Day}")
End Function
End Class
See Also