How to customize axis visible labels in .NET MAUI SfCartesianChart?
This article discusses how to manually set the number of visible axis labels in .NET MAUI Cartesian Chart. By default, the chart calculates the label count based on the provided data point range. However, this article explains how to customize the visible labels when dealing with varying data ranges.
The image below demonstrates how the count of visible axis labels varies when the data range changes.
To display only five axis labels with unique intervals for different data range variations, you can utilize the extended axis class. This involves overriding the OnCreateLabels() method and configuring the visible label count. You can obtain the minimum and maximum values of the axis range using the VisibleMinimum and VisibleMaximum properties, as demonstrated in the code sample below.
[C#]
public class CustomNumericalAxis : NumericalAxis
{
protected override void OnCreateLabels()
{
if (VisibleLabels != null)
{
VisibleLabels.Clear();
//Considered that we need 5 labels. so divided by 5.
var interval = (VisibleMaximum - VisibleMinimum) / 5;
var start = VisibleMinimum;
while (start <= VisibleMaximum)
{
VisibleLabels.Add(new ChartAxisLabel(start, start.ToString()));
start += interval;
}
}
}
}
[Xaml]
<chart:SfCartesianChart.YAxes>
<model:CustomNumericAxis ShowMajorGridLines="False">
<chart:NumericalAxis.Title>
<chart:ChartAxisTitle Text="Soil Mositure (%)"/>
</chart:NumericalAxis.Title>
<chart:NumericalAxis.LabelStyle>
<chart:ChartAxisLabelStyle LabelFormat="0.'%"/>
</chart:NumericalAxis.LabelStyle>
</model:CustomNumericAxis>
</chart:SfCartesianChart.YAxes>
Output
Conclusion
I hope you enjoyed learning how to customize axis visible labels in .NET MAUI SfCartesianChart.
Refer to our .NET MAUI Chart’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI Chart documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.
Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!