Articles in this section

How to skip dates not specified in a datasource in Blazor Charts?

This article explains how to skip dates that are not specified in a chart’s datasource when using Blazor Charts.

Skipping Unavailable Dates in Datasource

In a typical Blazor Charts component, the DateTime axis is configured to display date and time values at regular intervals by default. This behavior results in axis labels showing dates that may not be present in the datasource.

Blazor Charts allows you to skip datetime values that are not specified in the chart series datasource by utilizing the OnAxisLabelRender event.

Steps:

  • Attach the OnAxisLabelRender event to chart configuration.
  • In the OnAxisLabelRender event, you can check each date provided in the Text property of the event argument against the datasource.
  • If the date from the Text property does not exist in the datasource, set the Text property of the event argument to an empty string. This will effectively hide the label on the axis.

The code example below demonstrates how to skip datetime axis labels that are not available in the datasource.

Index.razor:

@using Syncfusion.Blazor.Charts
<SfChart>
   <ChartEvents OnAxisLabelRender="LabelRenderEvent"></ChartEvents>
   <ChartPrimaryXAxis EdgeLabelPlacement="EdgeLabelPlacement.Shift" ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime" Interval=1 IntervalType="Syncfusion.Blazor.Charts.IntervalType.Days" />
   <ChartSeriesCollection>
       <ChartSeries Opacity=0.5 DataSource="@MedalDetails" XName="X" YName="Y" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Area">
           <ChartMarker Height=10 Width=10 Visible="true"></ChartMarker>
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>
@code {
   public void LabelRenderEvent(Syncfusion.Blazor.Charts.AxisLabelRenderEventArgs args)
   {
       bool exists = false;
       if(args.Axis.Name == "PrimaryXAxis")
       {
           foreach(ChartData d in MedalDetails)
           {
               if ((d.X).ToString("dd/MM/yyyy") == args.Text)
               {
                   exists = true;
                   break;
               }
           };
           args.Text = exists ? args.Text : "";
       }       
   }
   public class ChartData
   {
       public DateTime X { get; set; }
       public double Y { get; set; }
   }
   public List<ChartData> MedalDetails = new List<ChartData>
   {
       new ChartData { X= new DateTime(2005, 01, 01), Y= 39.4 },
       new ChartData { X= new DateTime(2005, 01, 03), Y= 20.4 },
       new ChartData { X= new DateTime(2005, 01, 05), Y= 15.8 }
   };
}

The following screenshot illustrates the output of the code snippet.

Output:

image.png

Live Sample for Skipping AxisLabels

Conclusion:

We hope you enjoyed learning how to skip dates that are not specified in a datasource in the Blazor Chart component.

You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.

If you have any questions 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!

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