Getting Started with Blazor Stock Chart in Blazor Web App

3 Jul 20268 minutes to read

This section briefly explains how to include Blazor Stock Chart component in your Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI.

Create a new Blazor Web App

Create a Blazor Web App using Visual Studio via Microsoft Templates or the Blazor Extension. For detailed instructions, refer to the Blazor Web App Getting Started documentation.

Run the following command to create a new Blazor Web App.

dotnet new blazor -o BlazorWebApp --interactivity Auto
cd BlazorWebApp
cd BlazorWebApp.Client

Alternatively, create a Blazor Web App using Visual Studio Code via Microsoft Templates, the Blazor Extension, or the C# Dev Kit extension.

Run the following command to create a new Blazor Web App.

dotnet new blazor -o BlazorWebApp --interactivity Auto
cd BlazorWebApp
cd BlazorWebApp.Client

NOTE

Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Web App. For detailed information, refer to the interactive render mode documentation.

Install the required Blazor packages

Install the Syncfusion.Blazor.StockChart and the Syncfusion.Blazor.Themes NuGet packages. All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details. If using the WebAssembly or Auto render modes in the Blazor Web App, install these packages in the .Client project.

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Syncfusion.Blazor.StockChart and Syncfusion.Blazor.Themes) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.Blazor.StockChart -Version 34.1.29
Install-Package Syncfusion.Blazor.Themes -Version 34.1.29

Open the terminal and run the following commands.

dotnet add package Syncfusion.Blazor.StockChart -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29

Open the command prompt and run the following commands.

dotnet add package Syncfusion.Blazor.StockChart -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29

Add import namespaces

After the packages are installed, open the ~/_Imports.razor file in the .Client project and import the Syncfusion.Blazor and Syncfusion.Blazor.Charts namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Charts

Register the Blazor service

Open the Program.cs file in Blazor Web App and register the Blazor service. If the Interactive Render Mode is set to WebAssembly or Auto, register the Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.

....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet and script references in the App.razor file.

...
<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />
...
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>

Add Blazor Stock Chart component

Open a Razor file located in the ~/Pages/*.razor (for example, Home.razor) and add the Blazor Stock Chart component inside the razor file.

NOTE

If the interactivity location is set to Per page/component in the Web App, define a render mode at the top of the razor file. (For example, InteractiveServer, InteractiveWebAssembly or InteractiveAuto). If the Interactivity is set to Global with Auto or WebAssembly, the render mode is automatically configured in the App.razor file by default.

@rendermode InteractiveAuto

@using Syncfusion.Blazor.Charts

<SfStockChart>

</SfStockChart>

Populate Blazor Stock Chart with Data

To bind data for the Stock Chart component, assign a IEnumerable object to the DataSource property. It can also be provided as an instance of the DataManager.

@using Syncfusion.Blazor.Charts

<SfStockChart>
    <StockChartSeriesCollection>
        <StockChartSeries DataSource="@StockDetails" Type="ChartSeriesType.Candle" XName="Date" High="High" Low="Low" Open="Open" Close="Close" Volume="Volume"></StockChartSeries>
    </StockChartSeriesCollection>
</SfStockChart>

@code {
    public class StockChartData
    {
        public DateTime Date { get; set; }
        public Double Open { get; set; }
        public Double Low { get; set; }
        public Double Close { get; set; }
        public Double High { get; set; }
        public Double Volume { get; set; }
    }

    public List<StockChartData> StockDetails = new List<StockChartData>
    {
        new StockChartData { Date = new DateTime(2012, 04, 02), Open = 85.9757, High = 90.6657, Low = 85.7685, Close = 90.5257, Volume = 660187068 },
        new StockChartData { Date = new DateTime(2012, 04, 09), Open = 89.4471, High = 92, Low = 86.2157, Close = 86.4614, Volume = 912634864 },
        new StockChartData { Date = new DateTime(2012, 04, 16), Open = 87.1514, High = 88.6071, Low = 81.4885, Close = 81.8543, Volume = 1221746066 },
        new StockChartData { Date = new DateTime(2012, 04, 23), Open = 81.5157, High = 88.2857, Low = 79.2857, Close = 86.1428, Volume = 965935749 },
        new StockChartData { Date = new DateTime(2012, 04, 30), Open = 85.4, High =  85.4857, Low = 80.7385, Close = 80.75, Volume = 615249365 },
        new StockChartData { Date = new DateTime(2012, 05, 07), Open = 80.2143, High = 82.2685, Low = 79.8185, Close = 80.9585, Volume = 541742692 },
        new StockChartData { Date = new DateTime(2012, 05, 14), Open = 80.3671, High = 81.0728, Low = 74.5971, Close = 75.7685, Volume = 708126233 },
        new StockChartData { Date = new DateTime(2012, 05, 21), Open = 76.3571, High = 82.3571, Low = 76.2928, Close = 80.3271, Volume = 682076215 },
        new StockChartData { Date = new DateTime(2012, 05, 28), Open = 81.5571, High = 83.0714, Low = 80.0743, Close = 80.1414, Volume = 480059584 }
    };
}

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The Blazor Stock Chart component will render in your default web browser.

Open the terminal and navigate to the main project folder (for example, BlazorWebApp) and run the following command.

cd ..
cd BlazorWebApp
dotnet run

Open the command prompt and navigate to the main project folder (for example, BlazorWebApp) and run the following command.

cd ..
cd BlazorWebApp
dotnet run
Blazor Stock Chart Component

NOTE

View Sample in GitHub.

See also

  1. Getting Started with Blazor in Blazor WebAssembly App
  2. Getting Started with Blazor in Blazor Web App