How to refresh the chart when the layout is resized in Blazor Charts?
This article explains how to refresh the chart when the layout is resized in Blazor Charts.
Refresh Chart Using the Layout OnResizeStop Event
Blazor Charts provides an option to refresh chart using layout OnResizeStop event. This can be achieved by utilizing the OnResizeStop event to call the RefreshAsync method, which refreshes the chart when resizing the layout panel.
In this code snippet, the OnResizeStop event of the DashboardLayout is used to call the RefreshAsync method on the Chart component. This ensures that the chart is refreshed whenever the layout panel is resized. This approach helps in maintaining the chart’s appearance and accuracy within the resized panel.
Index.razor
@using Syncfusion.Blazor.Layouts
@using Syncfusion.Blazor.Charts
<SfDashboardLayout CellSpacing="@(new double[]{20 ,20 })" AllowResizing="true" Columns="3">
<DashboardLayoutEvents OnResizeStop="ResizeStop"></DashboardLayoutEvents>
<DashboardLayoutPanels>
<DashboardLayoutPanel Id="Panel1">
<ContentTemplate>
<div style="height:100%; width:100%;">
<SfChart ID="chart1" @ref="chartObj1">
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
</div>
</ContentTemplate>
</DashboardLayoutPanel>
</DashboardLayoutPanels>
</SfDashboardLayout>
@code {
SfChart chartObj1;
public void ResizeStop(Syncfusion.Blazor.Layouts.ResizeArgs args)
{
this.chartObj1.RefreshAsync();
}
public class SalesInfo
{
public string Month { get; set; }
public double SalesValue { get; set; }
}
public List<SalesInfo> Sales { get; set; } = new List<SalesInfo>();
protected override void OnInitialized()
{
Sales = new List<SalesInfo>
{
new SalesInfo { Month = "Jan", SalesValue = 35 },
new SalesInfo { Month = "Feb", SalesValue = 28 },
new SalesInfo { Month = "Mar", SalesValue = 34 },
new SalesInfo { Month = "Apr", SalesValue = 32 },
new SalesInfo { Month = "May", SalesValue = 40 },
new SalesInfo { Month = "Jun", SalesValue = 32 },
new SalesInfo { Month = "Jul", SalesValue = 35 }
};
}
}
The following GIF illustrates the output of the code snippet.
Live Sample for Refreshing the Chart When the Layout is Resized
Conclusion
I hope you enjoyed learning how to refresh chart using layout OnResizeStop event 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!