Search results

Port in Blazor Diagram component

Diagram provides support to define custom ports for making connections.

Port

When a connector is connected between two nodes, its end points are automatically docked to the node’s nearest boundary as shown in the following image.

Port to Port

Ports act as the connection points of the node and allows to create connections with only those specific points as shown in the following image.

Port Image

Create port

Add ports when initializing nodes

To add a connection port, define the port object and add it to node’s ports collection. The Offset property of port accepts an object of fractions and used to determine the position of ports. The following code illustrates how to add ports when initializing the node.

@using Syncfusion.Blazor.Diagrams
@using System.Collections.ObjectModel

 <SfDiagram Width="100%" Height="600px" Nodes="@NodeCollection">
 </SfDiagram>

 @code{

    public ObservableCollection<DiagramNode> NodeCollection = new ObservableCollection<DiagramNode>() { };
    protected override void OnInitialized()
    {
        // A node is created and stored in nodes array.
        DiagramNode node1 = new DiagramNode()
        {
            // Position of the node
            OffsetX = 250,
            OffsetY = 250,
            // Size of the node
            Width = 100,
            Height = 100,
            Style = new NodeShapeStyle() { Fill = "#6BA5D7", StrokeColor = "white" },
            // Initialize port collection
            Ports = new ObservableCollection<DiagramPort>()
            {
                // Sets the position for the port
                new DiagramPort(){ Offset= new NodePortOffset(){X=0.5,Y=0.5}, Visibility = PortVisibility.Visible}
            }
        };
        NodeCollection.Add(node1);
    }
}

Add ports at runtime

Add ports at runtime by using the server-side method [Add] in the port collection. The following code illustrates how to add ports to node at runtime.

The port’s Id property is used to define the unique Id for the port and its further used to find the port at runtime. If Id is not set, then default Id is automatically set.

@using Syncfusion.Blazor.Diagrams
@using System.Collections.ObjectModel

 <SfDiagram Width="100%" Height="600px" Nodes="@NodeCollection">
 </SfDiagram>

@code{

    public ObservableCollection<DiagramNode> NodeCollection = new ObservableCollection<DiagramNode>() { };
    protected override void OnInitialized()
    {
        // A node is created and stored in nodes array.
        DiagramNode node1 = new DiagramNode()
        {
            // Position of the node
            OffsetX = 250,
            OffsetY = 250,
            // Size of the node
            Width = 100,
            Height = 100,
            Style = new NodeShapeStyle() { Fill = "#6BA5D7", StrokeColor = "white" },
        };
        NodeCollection.Add(node1);
        // Initialize port collection
        NodeCollection[0].Ports.Add(new DiagramPort() { Id = "port1", Offset = new NodePortOffset() { X = 0, Y = 0.5 }, Visibility = PortVisibility.Visible });
        NodeCollection[0].Ports.Add(new DiagramPort() { Id = "port2", Offset = new NodePortOffset() { X = 1, Y = 0.5 }, Visibility = PortVisibility.Visible });
        NodeCollection[0].Ports.Add(new DiagramPort() { Id = "port3", Offset = new NodePortOffset() { X = 0.5, Y = 0 }, Visibility = PortVisibility.Visible });
        NodeCollection[0].Ports.Add(new DiagramPort() { Id = "port4", Offset = new NodePortOffset() { X = 0.5, Y = 1 }, Visibility = PortVisibility.Visible });
    }
}

Remove ports at runtime

A collection of ports can be removed from the node by using native[RemoveAt] method. Refer to the following example which shows how to remove ports at runtime.

@using Syncfusion.Blazor.Diagrams
@using System.Collections.ObjectModel

 <SfDiagram Width="100%" Height="600px" Nodes="@NodeCollection">
 </SfDiagram>

 @code{

    public ObservableCollection<DiagramNode> NodeCollection = new ObservableCollection<DiagramNode>() { };
    protected override void OnInitialized()
    {
        // A node is created and stored in nodes array.
        NodeCollection = new ObservableCollection<DiagramNode>() { };
        // Initialize port collection
        ObservableCollection<DiagramPort> Ports1 = new ObservableCollection<DiagramPort>() { };
        Ports1.Add(new DiagramPort() { Id = "port1", Offset = new NodePortOffset() { X = 0, Y = 0.5 }, Visibility = PortVisibility.Visible });
        Ports1.Add(new DiagramPort() { Id = "port2", Offset = new NodePortOffset() { X = 1, Y = 0.5 }, Visibility = PortVisibility.Visible });
        Ports1.Add(new DiagramPort() { Id = "port3", Offset = new NodePortOffset() { X = 0.5, Y = 0 }, Visibility = PortVisibility.Visible });
        Ports1.Add(new DiagramPort() { Id = "port4", Offset = new NodePortOffset() { X = 0.5, Y = 1 }, Visibility = PortVisibility.Visible });

        DiagramNode node1 = new DiagramNode()
        {
            // Position of the node
            OffsetX = 250,
            OffsetY = 250,
            // Size of the node
            Width = 100,
            Height = 100,
            Style = new NodeShapeStyle() { Fill = "#6BA5D7", StrokeColor = "white" },
            Ports = Ports1
        };
        NodeCollection.Add(node1);

        //remove ports ar run time
        NodeCollection[0].Ports.RemoveAt(3);
        NodeCollection[0].Ports.RemoveAt(2);
        NodeCollection[0].Ports.RemoveAt(1);
        NodeCollection[0].Ports.RemoveAt(0);
    }
}

Update port at runtime

You can change any port properties at runtime and update it DataBind method.

The following code example illustrates how to change the port properties.

@using Syncfusion.Blazor.Diagrams
@using System.Collections.ObjectModel

 <SfDiagram Width="100%" Height="600px" Nodes="@NodeCollection">
 </SfDiagram>

@code{

    SfDiagram diagram;
    public ObservableCollection<DiagramNode> NodeCollection = new ObservableCollection<DiagramNode>() { };
    protected override void OnInitialized()
    {
        // Initialize port collection
        ObservableCollection<DiagramPort> Ports1 = new ObservableCollection<DiagramPort>() { };
        Ports1.Add(new DiagramPort() { Id = "port1", Offset = new NodePortOffset() { X = 0, Y = 0.5 }, Visibility = PortVisibility.Visible });
        // A node is created and stored in nodes array.
        DiagramNode node1 = new DiagramNode()
        {
            // Position of the node
            OffsetX = 250,
            OffsetY = 250,
            // Size of the node
            Width = 100,
            Height = 100,
            Style = new NodeShapeStyle() { Fill = "#6BA5D7", StrokeColor = "white" },
            Ports = Ports1
        };
        NodeCollection.Add(node1);

        //remove ports ar run time
        NodeCollection[0].BeginUpdate();
        NodeCollection[0].Ports[0].Offset.X = 1;
        NodeCollection[0].Ports[0].Offset.Y = 1;
        NodeCollection[0].EndUpdate();

    }
}

Appearance

  • The shape of port can be changed by using its shape property. To explore the different types of port shapes, refer to Port Shapes. If you need to render a custom shape, then you can set shape as path and define path using path data property of port.

  • The appearance of ports can be customized by using StrokeColor, StrokeWidth and Fill properties of the port.

  • Customize the port size by using the Width and Height properties of port.

  • The ports Visibility property allows you to define, when the port should be visible.

The following code illustrates how to change the appearance of port.

@using Syncfusion.Blazor.Diagrams
@using System.Collections.ObjectModel

 <SfDiagram Width="100%" Height="600px" Nodes="@NodeCollection">
 </SfDiagram>
@code{

    SfDiagram diagram;
    public ObservableCollection<DiagramNode> NodeCollection = new ObservableCollection<DiagramNode>() { };
    protected override void OnInitialized()
    {
        // A node is created and stored in nodes array.
        DiagramNode node1 = new DiagramNode()
        {
            // Position of the node
            OffsetX = 250,
            OffsetY = 250,
            // Size of the node
            Width = 100,
            Height = 100,
            Style = new NodeShapeStyle() { Fill = "#6BA5D7", StrokeColor = "white" },
            // Initialize port collection
            Ports = new ObservableCollection<DiagramPort>() {
            new DiagramPort() {
                Id = "port1",
                Offset = new NodePortOffset() { X = 0, Y = 0.5 },
                Visibility = PortVisibility.Visible,
                //Set the style for the port
                Style= new PortShapeStyle(){ Fill="red", StrokeColor="black", StrokeWidth=2},
                // Sets the shape of the port as Circle
                Width= 12, Height=12, Shape= PortShapes.Circle
            }},
        };
        NodeCollection.Add(node1);
    }
}

Offset

The offset property of port is used to align the port based on fractions. 0 represents top/left corner, 1 represents bottom/right corner, and 0.5 represents half of width/height.

Constraints

The constraints property allows to enable/disable certain behaviors of ports. For more information about port constraints, refer to Port Constraints.