Articles in this section
Category / Section

How to set the count of visible axis labels in Xamarin.Forms Charts

1 min read

Visible axis labels count in Xamarin.Forms Chart has been determined based on the provided data point range. This section explains how to manually fix unique labels count on varying data range along with its available customization supports.


The following image shows how it varies the count of axis visible labels with data range changes.

Default axis labels rendering in Xamarin.Forms Charts
If you want to show only five axis labels with unique intervals for various data range changes as shown in above, then use extended axis to achieve it as shown in the following code sample.

 

[C#]

   public class CustomNumericAxis : NumericalAxis
    {
        protected override void OnCreateLabels()
        {
            base.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()));//Set label format if needed.
                    start += interval;
                }
            }
        }
 
    }

 

[XAML]

…     
<chart:SfChart.SecondaryAxis>
  <local:CustomNumericAxis x:Name="YAxis" EdgeLabelsDrawingMode="Shift" IsVisible="True" ShowMajorGridLines="False" ShowMinorGridLines="False">
                    <chart:NumericalAxis.AxisLineStyle>
                        <chart:ChartLineStyle StrokeColor="White"/>
                    </chart:NumericalAxis.AxisLineStyle>
                    <chart:NumericalAxis.LabelStyle>
                        <chart:ChartAxisLabelStyle TextColor="White"/>
                    </chart:NumericalAxis.LabelStyle>
                </local:CustomNumericAxis>
            </chart:SfChart.SecondaryAxis>
…

Output

Visible axis label with common count

View the complete sample in GitHub

 

See also

How to customize the axis label format based on the culture in Xamarin.Forms Chart

How to replace the axis labels with image in Xamarin.Forms Chart

How to show all the axis labels in Xamarin.Forms Chart

 

 

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied