How to Pass a JSON Array to a Blazor Chart?
This article explains how to bind the values from JSON array into Blazor Chart.
Creating Blazor RangeStepArea chart using JSON array
JSON data cannot be bound directly into Blazor Charts, Therefore, it is necessary to convert the JSON data into a format that can be easily bound.
The following steps explain how to pass JSON data to Blazor Charts.
Step 1: Import and inject the following namespace in the razor file and store the JSON file inside wwwroot folder.
@inject NavigationManager NavigationManager
@inject HttpClient Http
@using System.Net.Http.Json
Step 2: Create a Class and Array to store the JSON data as below.
public class ChartData
{
public string X { get; set; }
public double High { get; set; }
public double Low { get; set; }
}
public ChartData[] ChartPoints { get; set; }
Step 3: Get data from JSON file and get stored in Array using GetFromJsonAsync method.
protected async override Task OnInitializedAsync()
{
ChartPoints = await Http.GetFromJsonAsync<chartdata[]>(NavigationManager.BaseUri + "./range-data.json");
}
Step 4: Bind the JSON data stored in ChartPoints to the chart series.
<SfChart>
<ChartSeriesCollection>
<ChartSeries DataSource="@ChartPoints" XName="X" High="High" Low="Low" Opacity="0.5" Type="ChartSeriesType.RangeStepArea">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
Blazor Chart with JSON data:
Index.razor
@using Syncfusion.Blazor.Charts
@inject NavigationManager NavigationManager
@inject HttpClient Http
@using System.Net.Http.Json
<SfChart>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime" Format="dd MMM">
<ChartAxisMajorGridLines Width="0"/>
</ChartPrimaryXAxis>
<ChartPrimaryYAxis LabelFormat="{value}˚C" Interval="5" Minimum="-5" Maximum="25">
<ChartAxisLineStyle Width="0"></ChartAxisLineStyle>
<ChartAxisMajorTickLines Width="0"/>
</ChartPrimaryYAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@ChartPoints" XName="X" High="High" Low="Low" Opacity="0.5" Type="ChartSeriesType.RangeStepArea">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code{
public string BorderColor { get; set; }
public ChartData[] ChartPoints { get; set; }
protected async override Task OnInitializedAsync()
{
ChartPoints = await Http.GetFromJsonAsync<ChartData[]>(NavigationManager.BaseUri + "./range-data.json");
}
public class ChartData
{
public string X { get; set; }
public double High { get; set; }
public double Low { get; set; }
}
}
The following screenshot illustrates the result of RangeStepArea chart by using JSON data.
Output:
Conclusion
I hope you enjoyed learning how to pass JSON array 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!
</chartdata[]></chartdata[]>