How to position node’s port
12 Nov 202413 minutes to read
Diagram allows you to customize the position and appearance of the port efficiently. Ports can be aligned relative to the node boundaries. It has Margin, Offset, Horizontal, and Vertical alignment settings. It is quite tricky when all four alignments are used together but gives more control over alignments properties of the PointPort class. Ports of a node can be positioned using the following properties of PointPort
.
- Offset
- HorizontalAlignment
- VerticalAlignment
- Margin
How to change offset at runtime
The Offset property is used to align the ports based on fractions. 0 represents top/left corner, 1 represents bottom/right corner, and 0.5 represents half of width/height.
@using Syncfusion.Blazor.Diagram
<SfDiagramComponent Height="600px" Nodes="@nodes"/>
@code
{
DiagramObjectCollection<Node> nodes;
protected override void OnInitialized()
{
nodes = new DiagramObjectCollection<Node>();
// A node is created and stored in nodes collection.
Node node = new Node()
{
// Position of the node.
OffsetX = 250,
OffsetY = 250,
// Size of the node.
Width = 100,
Height = 100,
Style = new ShapeStyle() { Fill = "#6495ED", StrokeColor = "white" },
// Initialize port collection.
Ports = new DiagramObjectCollection<PointPort>()
{
new PointPort()
{
ID = "port1",
// Sets the offset for the port.
Offset = new DiagramPoint() { X = 0, Y = 0.5 },
Visibility = PortVisibility.Visible,
//Set the style for the port.
Style= new ShapeStyle() { Fill = "gray", StrokeColor = "black" },
Width = 12,
Height = 12,
// Sets the shape of the port as Square .
Shape = PortShapes.Square
}
}
};
nodes.Add(node);
}
}
You can download a complete working sample from GitHub
The following table shows the relationship between the shape port position and path port offset (fraction values).
Offset values | Output |
---|---|
(0,0) | |
(0,0.5) | |
(0,1) | |
(0.5,0) | |
(0.5,0.5) | |
(0.5,1) | |
(1,0) | |
(1,0.5) | |
(1,1) |
How to change Horizontal and Vertical alignment
The HorizontalAlignment property of the port is used to set how the port is horizontally aligned at the port position determined from the fraction values. The VerticalAlignment property is used to set how the port is vertically aligned at the port position.
The following table shows all the possible alignments visually with offset (0, 0)
.
Horizontal Alignment | Vertical Alignment | Output with Offset(0,0) |
---|---|---|
Left | Top | |
Center | Top | |
Right | Top | |
Left | Center | |
Center | Center | |
Right | Center | |
Left | Bottom | |
Center | Bottom | |
Right | Bottom |
The following code explains how to align ports.
@using Syncfusion.Blazor.Diagram
<SfDiagramComponent Height="600px" Nodes="@nodes"/>
@code
{
DiagramObjectCollection<Node> nodes;
protected override void OnInitialized()
{
nodes = new DiagramObjectCollection<Node>();
// A node is created and stored in nodes array.
Node node = new Node()
{
// Position of the node.
OffsetX = 250,
OffsetY = 250,
// Size of the node.
Width = 100,
Height = 100,
Style = new ShapeStyle() { Fill = "#6495ED", StrokeColor = "white" },
// Initialize port collection.
Ports = new DiagramObjectCollection<PointPort>()
{
new PointPort()
{
ID = "port1",
Offset = new DiagramPoint() { X = 0, Y = 0 },
Visibility = PortVisibility.Visible,
//Set the style for the port.
Style = new ShapeStyle(){ Fill="gray", StrokeColor="black"},
Width = 12,
Height = 12,
// Sets the shape of the port as Square.
Shape = PortShapes.Square,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
}
}
};
nodes.Add(node);
}
}
You can download a complete working sample from GitHub
NOTE
By default, the value of the
HorizontalAlignment
andVerticalAlignment
isCenter
. The alignment is positioned based on the offset value.
How to update margin for port
Margin is an absolute value used to add some blank space to any one of its four sides. The ports can be displaced with the margin property. The following code example explains how to align a port based on its Offset, HorizontalAlignment, VerticalAlignment, and Margin values.
@using Syncfusion.Blazor.Diagram
<SfDiagramComponent Height="600px" Nodes="@nodes"/>
@code
{
DiagramObjectCollection<Node> nodes;
protected override void OnInitialized()
{
nodes = new DiagramObjectCollection<Node>();
// A node is created and stored in nodes array.
Node node = new Node()
{
// Position of the node.
OffsetX = 250,
OffsetY = 250,
// Size of the node.
Width = 100,
Height = 100,
Style = new ShapeStyle() { Fill = "#6495ED", StrokeColor = "white" },
// Initialize port collection.
Ports = new DiagramObjectCollection<PointPort>()
{
new PointPort()
{
ID = "port1",
Offset = new DiagramPoint() { X = 0.5, Y = 1 },
Visibility = PortVisibility.Visible,
//Set the style for the port.
Style= new ShapeStyle() { Fill = "gray", StrokeColor = "black" },
Width = 12,
Height = 12,
// Sets the shape of the port as Square.
Shape = PortShapes.Square,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new DiagramThickness() { Top = 20 }
}
}
};
nodes.Add(node);
}
}
You can download a complete working sample from GitHub