Example of Line Routing in ASP.NET MVC Diagram Control
This sample visualizes the connectors that are automatically re-routing or moving away if any shape is found on the connectors' path.
Drag a shape near a connector and the connector goes round it. This is the LineRouting option of the diagram.
@using System.Collections.Generic
@Html.UiDiagram(new UiDiagramOptions
{
Id = "routingDiagram",
Height = "600px",
AriaLabel = "Connectors that route around shapes",
LineRouting = true,
OnCreated = "DiagramSamples.created",
Nodes = (List<UiDiagramNode>)ViewData["nodes"],
Connectors = (List<UiDiagramConnector>)ViewData["connectors"]
})
@using System.Collections.Generic
<p>
This sample visualizes the connectors that are automatically re-routing or moving away if any shape is found on the connectors' path.
Drag a shape near a connector and the connector goes round it. This is the <code>LineRouting</code> option of the diagram.
</p>
@Html.UiDiagram(new UiDiagramOptions
{
Id = "routingDiagram",
Height = "600px",
AriaLabel = "Connectors that route around shapes",
LineRouting = true,
OnCreated = "DiagramSamples.created",
Nodes = (List<UiDiagramNode>)ViewData["nodes"],
Connectors = (List<UiDiagramConnector>)ViewData["connectors"]
})
<script>
window.DiagramSamples = (function () {
'use strict';
/** OnCreated: the whole diagram fits in the view. */
function created() {
window.setTimeout(function () { window.Lumen.get('routingDiagram').fitToPage(); }, 100);
}
return { created: created };
})();
</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/LineRouting.cshtml; it draws what ViewData holds.
public ActionResult LineRouting()
{
// A flow shape 120 wide (a decision is 70 high, the others 50) with white text; its X and Y are its centre, in pixels.
Func<string, string, string, string, double, double, UiDiagramNode> shape = (id, text, flowShape, fill, x, y) => new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = 120, Height = flowShape == "Decision" ? 70 : 50,
Shape = new UiDiagramShape { Type = UiDiagramShapeType.Flow, Shape = flowShape },
Style = new UiDiagramStyle { Fill = fill, StrokeWidth = 0 },
Annotations = { new UiDiagramAnnotation { Content = text, Style = new UiDiagramStyle { Color = "#ffffff" } } }
};
// A point on a node's edge that a connector can use: (0.5, 0) is the middle of the top, (1, 0.5) the middle of the right side.
Func<string, double, double, UiDiagramPort> port = (id, x, y) => new UiDiagramPort
{
Id = id, Offset = new UiDiagramPoint { X = x, Y = y }, Visibility = UiDiagramPortVisibility.Hidden
};
UiDiagramNode start = shape("start", "Start", "Terminator", "#D5535D", 25, 110);
start.Ports.Add(port("port1", 0.5, 0));
UiDiagramNode document = shape("document", "Document", "Document", "#5BA5F0", 25, 400);
document.Ports.Add(port("port1", 0, 0.5));
UiDiagramNode card = shape("card", "Card", "Card", "#76C3F0", 525, 255);
card.Ports.Add(port("port1", 1, 0.5));
card.Ports.Add(port("port2", 0.5, 1));
var nodes = new List<UiDiagramNode>
{
start,
shape("process", "Process", "Process", "#65B091", 25, 255),
document,
shape("decision", "Decision", "Decision", "#9A8AF7", 275, 110),
shape("document2", "Document", "Document", "#5BA5F0", 275, 255),
shape("end", "End", "Terminator", "#D5535D", 275, 400),
shape("process2", "Process", "Process", "#65B091", 525, 110),
card
};
// Orthogonal connectors with a grey line and arrow; a port id makes a connector use that point.
Func<string, string, string, string, string, UiDiagramConnector> connect = (id, from, to, fromPort, toPort) => new UiDiagramConnector
{
Id = id, SourceID = from, TargetID = to, SourcePortID = fromPort, TargetPortID = toPort, Type = UiDiagramConnectorType.Orthogonal,
Style = new UiDiagramStyle { StrokeColor = "#707070", StrokeWidth = 1.25 },
TargetDecorator = new UiDiagramDecorator { Style = new UiDiagramStyle { Fill = "#707070", StrokeColor = "#707070" } }
};
var connectors = new List<UiDiagramConnector>
{
connect("connector1", "start", "process", null, null),
connect("connector2", "process", "document", null, null),
connect("connector3", "document", "end", null, null),
connect("connector4", "start", "decision", null, null),
connect("connector5", "decision", "process2", null, null),
connect("connector6", "process2", "card", null, null),
connect("connector7", "process", "document2", null, null),
connect("connector8", "document2", "card", null, null),
connect("connector9", "start", "card", "port1", "port1"),
connect("connector10", "card", "document", "port2", "port1")
};
ViewData["nodes"] = nodes;
ViewData["connectors"] = connectors;
return View();
}
}
}
@* 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>