How to render x-axis labels with sorting in Blazor Charts?
This article explains how to render x-axis labels with sorting in Blazor Charts.
Render X-Axis Labels with Sorting
Blazor Charts provides an option to render x-axis labels with sorting.
To render x-axis labels with sorting, you need to utilize the OnAxisLabelRender event and use the X property for the X-axis. This event can change the axis label based on the X property value, allowing it to render the x-axis labels with sorting.
The below code example demonstrates how to render x-axis labels with sorting
Index.razor
@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Buttons
<SfChart>
<ChartEvents OnAxisLabelRender="AxisLabelEvent" />
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Double" />
<ChartSeriesCollection>
<ChartSeries DataSource="@Data1" XName="X" YName="Y" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Line" />
</ChartSeriesCollection>
</SfChart>
@code {
public void AxisLabelEvent(Syncfusion.Blazor.Charts.AxisLabelRenderEventArgs args)
{
if(args.Axis.Name == "PrimaryXAxis")
{
args.Text = Data1[int.Parse(args.Text)].XLabel;
}
}
public class ChartData
{
public double X { get; set; }
public string XLabel { get; set; }
public double Y { get; set; }
}
public List<ChartData> Data1 = new List<ChartData>
{
new ChartData { X=0, XLabel= "11/20/2013", Y= 30},
new ChartData { X=1, XLabel= "02/27/2014", Y= 23.6 },
new ChartData { X=2, XLabel= "03/11/2014", Y= 34 },
new ChartData { X=3, XLabel= "04/14/2014", Y= 18 },
new ChartData { X=4, XLabel= "05/06/2014", Y= 10 },
new ChartData { X=5, XLabel= "05/06/2014", Y= 15 },
new ChartData { X=6, XLabel= "05/14/2015", Y= 40 },
new ChartData { X=7, XLabel= "05/10/2016", Y= 41 },
new ChartData { X=8, XLabel= "09/29/2016", Y= 0 },
new ChartData { X=9, XLabel= "10/05/2016", Y= 0 },
new ChartData { X=10, XLabel= "10/05/2016", Y= 2 },
new ChartData { X=11, XLabel= "11/03/2016", Y= 88 },
new ChartData { X=12, XLabel= "03/20/2017", Y= 70 },
new ChartData { X=13, XLabel= "04/10/2017", Y= 70 },
new ChartData { X=14, XLabel= "05/15/2017", Y= 80 },
new ChartData { X=15, XLabel= "09/11/2017", Y= 80 },
new ChartData { X=16, XLabel= "02/21/2019", Y= 80 },
new ChartData { X=17, XLabel= "01/15/2020", Y= 52 },
new ChartData { X=18, XLabel= "08/31/2021", Y= 70 }
};
}
The following screenshot illustrates the output of the code snippet.
Live sample for render x-axis labels with sorting
Conclusion
I hope you enjoyed learning how to render x-axis labels with sorting in Blazor Charts.
You can refer to our Blazor Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!