Articles in this section
Category / Section

How to customize each label of the axis in different format?

1 min read

In SfCharts , you can format the axis labels using the LabelFormat property of the respective axis. This will format all the labels in an axis with the same format.

Suppose, if you want to format each label in an axis with different format, this can be done in LabelCreated event. This event will be fired once for each label of that axis. You can get the current label’s content using the LabelContent property in the event argument and format the label and assign the formatted content to the LabelContent property itself.

In the following code example, the first label of each month in the DateTimeAxis is formatted with month format and other labels in the same month are formatted with day format.

C#

chart.PrimaryAxis = new DateTimeAxis() { IntervalType = DateTimeIntervalType.Days, 
Interval = 11}; 
chart.PrimaryAxis.LabelCreated += PrimaryAxis_LabelCreated;

 

XAML

<chart:SfChart.PrimaryAxis>
      <chart:DateTimeAxis IntervalType="Days" LabelCreated = "PrimaryAxis_LabelCreated">
        <chart:DateTimeAxis.Interval>
          <x:Double>10</x:Double>
        </chart:DateTimeAxis.Interval>
      </chart:DateTimeAxis>
</chart:SfChart.PrimaryAxis>

 

 [C#]

int month = 0;
 
void PrimaryAxis_LabelCreated(object sender, ChartAxisLabelEventArgs e)
        {
            DateTime date = DateTime.Parse(e.LabelContent);
 
            if (month != date.Month)
            {
                e.LabelContent = date.ToString("MMM");
                month = date.Month;
            }
            else
                e.LabelContent = date.Day.ToString();
        }

 

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/CustomAxisLabel1047363945.zip  

 

Output:

Format the axis labels in SfCharts

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