How to plot date-time values in vertical axes in Blazor Chart?
This article explains how to plot date time values in vertical axis of Blazor Chart Component.
Plotting date time values in vertical axis of Blazor Chart
To plot date-time value in y-axis, convert the date-time values to double while populating the data source for Blazor Chart and reverse the conversion(double to date-time) in the OnAxisLabelRender event.
public void OnLabelRender(AxisLabelRenderEventArgs args)
{
if(args.Axis.Name == "PrimaryYAxis")
{
double value = Convert.ToDouble(args.Text);
//Converting corresponding double value to data time value.
DateTime date = (new DateTime(1970, 1, 1).AddMilliseconds(value));
args.Text = String.Format("{0:HH:mm}", date);
}
}
By using using OnAxisLabelRender event with above convertion, you can convert the axis label text into date time and replace that converted date time value into label text to plot date time value in vertical axis.
The following code example illustrates this.
Index.razor
@using Syncfusion.Blazor.Charts
<SfChart>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime"></ChartPrimaryXAxis>
<ChartEvents OnAxisLabelRender="@OnLabelRender"></ChartEvents>
<ChartSeriesCollection>
<ChartSeries DataSource="@WeatherReports" XName="XValue" YName="Y" Type="ChartSeriesType.Line">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code {
DateTime start = new DateTime(1970, 1, 1);
public class ChartData
{
public DateTime XValue { get; set; }
public DateTime YValue { get; set; }
public double Y
{
get{ return (YValue - new DateTime(1970, 1, 1)).TotalMilliseconds; }
}
}
public List<ChartData> WeatherReports = new List<ChartData>
{
new ChartData { XValue = new DateTime(2010, 1, 1), YValue = new DateTime(2010, 1, 1) },
new ChartData { XValue = new DateTime(2010, 2, 1), YValue = new DateTime(2010, 1, 2) },
new ChartData { XValue = new DateTime(2010, 3, 1), YValue = new DateTime(2010, 1, 3) },
new ChartData { XValue = new DateTime(2010, 4, 1), YValue = new DateTime(2010, 1, 4) },
new ChartData { XValue = new DateTime(2010, 5, 1), YValue = new DateTime(2010, 1, 5) }
};
public void OnLabelRender(AxisLabelRenderEventArgs args)
{
if(args.Axis.Name == "PrimaryYAxis")
{
double value = Convert.ToDouble(args.Text);
//Converting corresponding double value to data time value.
DateTime date = (new DateTime(1970, 1, 1).AddMilliseconds(value));
args.Text = String.Format("{0:HH:mm}", date);
}
}
}
The following screenshot illustrates the output of the above code snippet.
Output:
Conclusion
I hope you enjoyed learning how to plot date-time values in y-axis 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!