How can I help you?
Getting Started with Blazor Diagram Component in Web App
2 Jun 202612 minutes to read
This section briefly explains how to include the Syncfusion® Blazor Diagram component in your Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI.
Ready to streamline your Syncfusion® Blazor development?
Discover the full potential of Syncfusion® Blazor components with Syncfusion® 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, Syncfusion® CodeStudio and more. Explore Syncfusion® AI Coding Assistants
Prerequisites
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 a project.
If you use the Syncfusion® Blazor Extension, a sample application can be generated automatically.
Prerequisites
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 a 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 -int Auto
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 Syncfusion® 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 Code or .NET CLI:
Open the terminal or command prompt and run the following commands:
dotnet add package Syncfusion.Blazor.Diagram -v 33.2.3
dotnet add package Syncfusion.Blazor.Themes -v 33.2.3NOTE
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.Diagram;Register Syncfusion® Blazor service
Register the Syncfusion® 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 Syncfusion® Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.
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.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
</body>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 Syncfusion® 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 Web App, define a render mode at the top of the razor file. (For example,InteractiveServer,InteractiveWebAssemblyorInteractiveAuto).
If your create application uses Per page/component interactivity, add this at the top of Pages/Home.razor:
@rendermode InteractiveAuto
@using Syncfusion.Blazor.Diagram
<SfDiagramComponent Width="100%" Height="600px"/>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.
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 = new DiagramObjectCollection<Node>();
DiagramObjectCollection<Connector> connectors = new DiagramObjectCollection<Connector>();
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 = "connector2",
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;
}
}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
- Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application in Visual Studio.
- Run the application using
dotnet runcommand in Command prompt. - This will render the Syncfusion® Blazor Diagram component in the default web browser.
The output will appear as follows:
