How to update chart based on grid data in Blazor Charts?
This article explains how to update chart based on grid data in Blazor Charts.
Update Chart Based on Grid Data
Blazor Charts provides an option to update chart based on grid data. To update the chart based on grid data, use the grid component’s GetCurrentViewRecordsAsync
public method to retrieve the grid data, and then bind this data to the chart.
In this code snippet, the Grid component displays a list of statistics, including browser names and the number of users. When the “Update” button is clicked, the UpdateChart
method retrieves the current records from the grid using the GetCurrentViewRecordsAsync
public method. These records are then assigned to the chart’s data source, named StatisticsDetails1, and the chart is refreshed using the chart component’s RefreshAsync
public method to display the updated data.
Index.razor
@page "/"
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Buttons
<div class="row">
<div class="col-md-6">
<SfChart @ref="chartobj">
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category">
</ChartPrimaryXAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@StatisticsDetails1" XName="Browser" YName="Users" Type="ChartSeriesType.Column">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
</div>
<div class="col-md-6">
<SfButton IsPrimary="true" IsToggle="true" OnClick="UpdateChart">Update</SfButton><br />
<SfGrid @ref="grid" DataSource="@StatisticsDetails">
<GridColumns>
<GridColumn Field=@nameof(Statistics.Browser) HeaderText="Browser" Width="120"></GridColumn>
<GridColumn Field=@nameof(Statistics.Users) HeaderText="No of Users" Width="150"></GridColumn>
</GridColumns>
</SfGrid>
</div>
</div>
@code {
SfGrid<Statistics>? grid;
SfChart? chartobj;
List<Statistics> StatisticsDetails1 = new List<Statistics> { };
public void UpdateChart(MouseEventArgs args)
{
var newData = grid.GetCurrentViewRecordsAsync();
StatisticsDetails1 = newData.Result;
chartobj.RefreshAsync();
}
public class Statistics
{
public string? Browser { get; set; }
public double? Users { get; set; }
}
public List<Statistics> StatisticsDetails = new List<Statistics>
{
new Statistics { Browser = "Chrome", Users = 37 },
new Statistics { Browser = "UC Browser", Users = 17 },
new Statistics { Browser = "iPhone", Users = 19 },
new Statistics { Browser = "Others", Users = 4 },
new Statistics { Browser = "Opera", Users = 11 },
new Statistics { Browser = "Android", Users = 12 },
};
}
The following screenshot illustrates the output of the code snippet.
Live Sample for Updating Chart Based on Grid Data
Conclusion
I hope you enjoyed learning how to update chart based on grid data 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!