How to update datetime axis interval type based on data in Blazor Charts?
Blazor Charts offer a powerful way to visualize data, but sometimes the default settings may not suit your specific needs. One such setting is the IntervalType of the DateTime axis, which can greatly impact how your data is displayed. This article will guide you through the process of dynamically updating the IntervalType based on the datetime values provided for the DataSource. This can be particularly useful when dealing with data that spans across different time scales, such as hours and days.
Dynamically adjusting datetime axis interval type
Blazor Charts provides an option to update the DateTime axis IntervalType based on datetime values provided for DataSource. This can be achieved by changing the IntervalType to Hours for datasource x values in hours difference and IntervalType to Days for datasource x values in days difference.
Index.razor
@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Buttons
<SfButton Content="Days" @onclick="AddDaysData" IsPrimary="true"></SfButton>
<SfButton Content="Hours" @onclick="AddHoursData" IsPrimary="true"></SfButton>
<SfChart>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime" IntervalType="@type">
</ChartPrimaryXAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@WeatherReports" Width=2 XName="XValue" YName="YValue" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Spline">
<ChartMarker Visible="true"></ChartMarker>
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code {
public List<ChartData> WeatherReports = new List<ChartData>();
public Syncfusion.Blazor.Charts.IntervalType type = Syncfusion.Blazor.Charts.IntervalType.Auto;
public void AddDaysData()
{
WeatherReports = new List<ChartData>
{
new ChartData { XValue = new DateTime(2005,01,01), YValue = 15},
new ChartData { XValue = new DateTime(2005,01,02), YValue = 22 },
new ChartData { XValue = new DateTime(2005,01,03), YValue = 32 },
new ChartData { XValue = new DateTime(2005,01,04), YValue = 31 },
new ChartData { XValue = new DateTime(2005,01,05), YValue = 29 },
new ChartData { XValue = new DateTime(2005,01,06), YValue = 24 },
new ChartData { XValue = new DateTime(2005,01,07), YValue = 18}
};
type = Syncfusion.Blazor.Charts.IntervalType.Days;
StateHasChanged();
}
public void AddHoursData()
{
WeatherReports = new List<ChartData>
{
new ChartData { XValue = new DateTime(2005,01,01,01,00,00), YValue = 10},
new ChartData { XValue = new DateTime(2005,01,01,02,00,00), YValue = 18 },
new ChartData { XValue = new DateTime(2005,01,01,03,00,00), YValue = 28 },
new ChartData { XValue = new DateTime(2005,01,01,04,00,00), YValue = 28 },
new ChartData { XValue = new DateTime(2005,01,01,05,00,00), YValue = 26 },
new ChartData { XValue = new DateTime(2005,01,01,06,00,00), YValue = 20 },
new ChartData { XValue = new DateTime(2005,01,01,07,00,00), YValue = 15}
};
type = Syncfusion.Blazor.Charts.IntervalType.Hours;
StateHasChanged();
}
public class ChartData
{
public DateTime XValue { get; set; }
public double YValue { get; set; }
}
}
The following screenshot illustrates the output of the code snippet.
Output
Live Sample for Dynamic IntervalType
Conclusion
I hope you enjoyed learning how to update datetime axis interval type based on data in Blazor Chart Component.
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!