Example of Connectors in ASP.NET MVC Diagram Control
The three connector types (straight, orthogonal, curved), their decorators (arrow, circle, diamond, open arrow, square), a dashed coloured line, rounded bends, and a connector between two points. Select a connector and drag one of its ends onto another node to connect it there.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
The three connector types (straight, orthogonal, curved), their decorators (arrow, circle, diamond, open arrow, square), a dashed
coloured line, rounded bends, and a connector between two points. Select a connector and drag one of its ends onto another node to
connect 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; }
}
/** OnConnectionChange: where a connector's end is now. */
function connected(args) {
status(args.connector.id + ': ' + (args.connectorEnd === 'ConnectorSourceEnd' ? 'source' : 'target') + ' end on ' + (args.newValue.nodeId || 'a point') + '.');
}
return { 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/Connectors.cshtml; it draws what ViewData holds.
public ActionResult Connectors()
{
var nodes = new List<UiDiagramNode>();
var connectors = new List<UiDiagramConnector>();
string[] types = { "Straight", "Orthogonal", "Bezier" };
for (int i = 0; i < 3; i++)
{
double y = 80 + i * 140;
nodes.Add(Node("from" + i, 100, y, "Source", UiDiagramShapeType.Basic, "Rectangle", 100, 50));
nodes.Add(Node("to" + i, 420, y + 60, "Target", UiDiagramShapeType.Basic, "Ellipse", 100, 50));
var c = Link("link" + i, "from" + i, "to" + i, types[i], (UiDiagramConnectorType)i);
c.Annotations[0].Offset = new UiDiagramPoint { X = 0.5 };
connectors.Add(c);
}
connectors[0].TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.Arrow };
connectors[1].SourceDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.Circle };
connectors[1].TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.Diamond, Width = 14, Height = 10 };
connectors[1].CornerRadius = 8;
connectors[2].Style = new UiDiagramStyle { StrokeColor = "#c4314b", StrokeWidth = 2, StrokeDashArray = "6 3" };
connectors[2].TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.OpenArrow };
connectors.Add(new UiDiagramConnector
{
Id = "free", SourcePoint = new UiDiagramPoint { X = 620, Y = 60 }, TargetPoint = new UiDiagramPoint { X = 760, Y = 160 },
TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.Square },
Annotations = { new UiDiagramAnnotation { Content = "Between two points" } }
});
ViewData["diagram"] = new UiDiagramOptions { Id = "connectorsDiagram", Height = "520px", Nodes = nodes, Connectors = connectors, 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;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
}
}
@* 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>