Getting Started with Blazor Diagram in Blazor Web App
This section briefly explains how to include the Blazor Diagram component in your Blazor Web App using Visual Studio, Visual Studio Code, or the .NET CLI.
NOTE
The Blazor Diagram component requires .NET 8.0 or later and a modern browser with ES6 and WebAssembly support. The
Syncfusion.Blazor.Diagrampackage is compatible with theServer,WebAssembly, andAutointeractive render modes.
Ready to streamline your Blazor development?
Discover the full potential of Blazor components with AI Coding Assistants. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, CodeStudio and more. Explore AI Coding Assistants
Prerequisites
- System requirements for Blazor components
- Install the Syncfusion® Blazor Extension for Visual Studio if you plan to use the Syncfusion templates to create the project.
Create a new Blazor Web App in Visual Studio
Create a Blazor Web App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor Web App Getting Started documentation.
NOTE
Configure the appropriate Interactive render mode and Interactivity location while creating the project.
If you use the Syncfusion® Blazor Extension, a sample application can be generated automatically.
Prerequisites
- System requirements for Blazor components
- Install the Syncfusion® Blazor Extension for Visual Studio Code if you plan to use the Syncfusion templates to create the project.
Create a new Blazor Web App in Visual Studio Code
Create a Blazor Web App using Visual Studio Code via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor Web App Getting Started documentation.
NOTE
Configure the appropriate Interactive render mode and Interactivity location while creating the project.
If you use the Syncfusion® Blazor Extension, a sample application can be generated automatically.
Prerequisites
Install the latest version of .NET SDK. If you previously installed the SDK, determine the installed version by executing the following command in a command prompt (Windows) or terminal (macOS) or command shell (Linux).
dotnet --versionCreate a Blazor Web App using .NET CLI
Run the following command to create a new Blazor Web App in a command prompt (Windows) or terminal (macOS) or command shell (Linux). For detailed instructions, refer to the Blazor Web App Getting Started documentation.
For example, in a Blazor Web App with the Auto interactive render mode, use the following commands:
dotnet new blazor -o BlazorWebApp --interactivity Auto -f net8.0
cd BlazorWebAppThis command creates a new Blazor Web App and places it in a new directory called BlazorWebApp inside your current location.
NOTE
See the dotnet new CLI command and interactive render mode documentation topics for more details.
Install required Blazor packages
Install the Syncfusion.Blazor.Diagram and Syncfusion.Blazor.Themes NuGet packages using one of the following methods.
Visual Studio (NuGet Package Manager):
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Syncfusion.Blazor.DiagramandSyncfusion.Blazor.Themes) and install it.
Visual Studio (Package Manager Console): run the following in Tools → NuGet Package Manager → Package Manager Console:
Install-Package Syncfusion.Blazor.Diagram -Version 34.2.2
Install-Package Syncfusion.Blazor.Themes -Version 34.2.2Visual Studio Code or .NET CLI:
Open the terminal or command prompt and run the following commands:
dotnet add package Syncfusion.Blazor.Diagram --version 34.2.2
dotnet add package Syncfusion.Blazor.Themes --version 34.2.2
dotnet restoreNOTE
All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details.
Add import namespaces
After the packages are installed, open the ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.Diagram namespaces.
@using Syncfusion.Blazor
@using Syncfusion.Blazor.DiagramRegister Blazor service
Register the Blazor service in the Program.cs file.
using Syncfusion.Blazor;
....
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSyncfusionBlazor();
....NOTE
If the Interactive Render Mode is set to
WebAssemblyorAuto, register the Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.
NOTE
Register your Syncfusion license key before calling
AddSyncfusionBlazor()by usingSyncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR_LICENSE_KEY"). A missing or invalid license will cause trial license warnings at runtime. See the Syncfusion Licensing FAQ for details.
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 ~/Components/App.razor file.
<head>
...
<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />
</head>
<body>
....
<script src="_content/Syncfusion.Blazor.Diagram/scripts/sf-diagramcomponent.min.js" type="text/javascript"></script>
</body>NOTE
After adding the stylesheet and script references, rebuild the project so that the static web assets from the NuGet packages are restored to the
wwwroot/_content/folder.
NOTE
Check out the Blazor Themes topic to discover various methods (Static Web Assets, CDN, and CRG) for referencing themes in your Blazor application. Also, check out the Adding Script Reference topic to learn different approaches for adding script references in your Blazor application.
Add Blazor Diagram Component
- Open a Razor file located in the ~/Components/Pages (for example, Home.razor) and add the Blazor Diagram component inside the razor file.
- If the interactivity location is set to
Per page/componentin the Blazor Web App, define a render mode at the top of the razor file. (For example,InteractiveServer,InteractiveWebAssemblyorInteractiveAuto).
If your application uses Per page/component interactivity, add the render mode declaration at the top of Pages/Home.razor:
@rendermode InteractiveAuto
@using Syncfusion.Blazor.Diagram
<SfDiagramComponent Width="100%" Height="600px"/>The
@using Syncfusion.Blazor.Diagramdirective can be omitted if it is already declared in~/_Imports.razor(as described in the previous step).
NOTE
The following table shows which render mode to choose for each hosting model:
| Hosting model | Render mode | Use case |
|---|---|---|
| Server | @rendermode InteractiveServer |
Server-side rendering with SignalR. |
| WebAssembly | @rendermode InteractiveWebAssembly |
Client-side rendering, no server required after first load. |
| Auto | @rendermode InteractiveAuto |
WebAssembly on first visit, then Server for subsequent visits. |
Create your first Diagram with nodes and connectors
This section explains how to create a simple flowchart by adding nodes, customizing their appearance, and connecting them using connectors.
The following example creates a flowchart with four nodes: Start, Process, Decision, and End. It also applies common node and connector settings using the NodeCreating and ConnectorCreating properties.
<SfDiagramComponent Width="100%" Height="600px" Nodes="@nodes" Connectors="@connectors" NodeCreating="@NodeCreating" ConnectorCreating="@ConnectorCreating" />
@code {
DiagramObjectCollection<Node> nodes;
DiagramObjectCollection<Connector> connectors;
protected override void OnInitialized()
{
nodes = new DiagramObjectCollection<Node>()
{
new Node()
{
ID = "node1", OffsetX = 300, OffsetY = 100,
Shape = new FlowShape() { Type = NodeShapes.Flow, Shape = NodeFlowShapes.Terminator },
Annotations = new DiagramObjectCollection<ShapeAnnotation>()
{
new ShapeAnnotation(){ Content = "Start" }
}
},
new Node()
{
ID = "node2", OffsetX = 300, OffsetY = 200,
Shape = new FlowShape() { Type = NodeShapes.Flow, Shape = NodeFlowShapes.Process },
Annotations = new DiagramObjectCollection<ShapeAnnotation>()
{
new ShapeAnnotation(){ Content = "Process" }
}
},
new Node()
{
ID = "node3", OffsetX = 300, OffsetY = 300,
Shape = new FlowShape() { Type = NodeShapes.Flow, Shape = NodeFlowShapes.Decision },
Annotations = new DiagramObjectCollection<ShapeAnnotation>()
{
new ShapeAnnotation(){ Content = "Decision?" }
}
},
new Node()
{
ID = "node4", OffsetX = 300, OffsetY = 400,
Shape = new FlowShape() { Type = NodeShapes.Flow, Shape = NodeFlowShapes.Terminator },
Annotations = new DiagramObjectCollection<ShapeAnnotation>()
{
new ShapeAnnotation(){ Content = "End" }
}
}
};
connectors = new DiagramObjectCollection<Connector>()
{
new Connector()
{
ID = "connector1",
SourceID = "node1",
TargetID = "node2",
},
new Connector()
{
ID = "connector2",
SourceID = "node2",
TargetID = "node3",
},
new Connector()
{
ID = "connector3",
SourceID = "node3",
TargetID = "node4",
},
};
}
private void NodeCreating(IDiagramObject obj)
{
Node node = obj as Node;
node.Style = new ShapeStyle() { Fill = "#E8F4FF", StrokeColor = "#357BD2" };
node.Width = 140;
node.Height = 50;
}
private void ConnectorCreating(IDiagramObject obj)
{
(obj as Connector).Type = ConnectorSegmentType.Orthogonal;
}
}A complete working sample can be downloaded from GitHub
In this example:
-
OffsetXandOffsetYdefine the position of each node. -
Shapedefines the node shape configuration, andFlowShape.Shapespecifies flowchart shapes such as Terminator, Process, or Decision. -
ShapeAnnotationadds text inside each node using theContentproperty. -
SourceIDandTargetIDconnect one node to another. -
NodeCreatingapplies common width, height, fill color, and stroke color to all nodes. -
ConnectorCreatingapplies common connector settings, such as orthogonal routing.
Run the application
Visual Studio: Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The Blazor Diagram component will render in your default web browser.
Visual Studio Code or .NET CLI:
- Open the terminal (Visual Studio Code) or command prompt (.NET CLI) and navigate to the
Clientproject folder. -
Run the following command:
dotnet run - The application will start and display in your default web browser at the URL shown in the console output (for example,
https://localhost:7xxxorhttps://localhost:5xxx).
The output will appear as follows:
