Example of Ports in ASP.NET MVC Diagram Control
Ports: the points of a node that connectors attach to. Point at a node to see its ports (the bottom ones are always shown); drag from a port to another node or port to draw a connector. Select a connector and drag an end onto another port to move it there.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Ports: the points of a node that connectors attach to. Point at a node to see its ports (the bottom ones are always shown); drag
from a port to another node or port to draw a connector. Select a connector and drag an end onto another port to move it there.
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** OnCollectionChange: a connector drawn from a port, or something removed. */
function collection(args) {
var c = args.element;
status(args.type === 'Addition' && c.sourceID !== undefined
? 'Drawn: ' + c.id + ' from ' + c.sourceID + (c.sourcePortID ? ' (' + c.sourcePortID + ')' : '') + ' to ' + c.targetID + (c.targetPortID ? ' (' + c.targetPortID + ')' : '') + '.'
: args.type + ': ' + c.id + '.');
}
/** OnConnectionChange: where a connector's end is now. */
function connected(args) {
status(args.connector.id + ' now ends on ' + (args.newValue.nodeId || 'a point') + (args.newValue.portId ? ' (' + args.newValue.portId + ')' : '') + '.');
}
return { collection: collection, connected: connected };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/Ports.cshtml; it draws what ViewData holds.
public ActionResult Ports()
{
System.Func<string, double, double, string, UiDiagramNode> withPorts = (id, x, y, text) =>
{
UiDiagramNode n = Node(id, x, y, text, UiDiagramShapeType.Basic, "Rectangle", 120, 70, "#e8f0fe");
n.Ports.Add(new UiDiagramPort { Id = id + "Left", Offset = new UiDiagramPoint { X = 0, Y = 0.5 }, AllowDraw = true });
n.Ports.Add(new UiDiagramPort { Id = id + "Right", Offset = new UiDiagramPoint { X = 1, Y = 0.5 }, AllowDraw = true });
n.Ports.Add(new UiDiagramPort { Id = id + "Top", Offset = new UiDiagramPoint { X = 0.5, Y = 0 }, AllowDraw = true, Shape = UiDiagramPortShape.Square });
n.Ports.Add(new UiDiagramPort { Id = id + "Bottom", Offset = new UiDiagramPoint { X = 0.5, Y = 1 }, AllowDraw = true, Visibility = UiDiagramPortVisibility.Visible });
return n;
};
ViewData["diagram"] = new UiDiagramOptions
{
Id = "portsDiagram",
Height = "460px",
Nodes = new List<UiDiagramNode> { withPorts("a", 140, 120, "Node A"), withPorts("b", 460, 120, "Node B"), withPorts("c", 300, 330, "Node C") },
Connectors = new List<UiDiagramConnector>
{
new UiDiagramConnector { Id = "ab", SourceID = "a", SourcePortID = "aRight", TargetID = "b", TargetPortID = "bLeft", Type = UiDiagramConnectorType.Orthogonal }
},
OnCollectionChange = "DiagramSamples.collection",
OnConnectionChange = "DiagramSamples.connected"
};
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>