Example of Avoid Connector Overlap in ASP.NET MVC Diagram Control
This sample visualizes the connectors that automatically adjust to minimize visual overlap, ensuring clear and distinct
representations of connections in a circuit diagram. With AvoidConnectorOverlap on, the wires between the same blocks
run side by side instead of on top of each other. Drag the canvas to move the view.
@using System.Collections.Generic
@Html.UiDiagram(new UiDiagramOptions
{
Id = "overlapDiagram",
Height = "600px",
AriaLabel = "Circuit diagram",
LineRouting = true,
AvoidConnectorOverlap = true,
Tool = UiDiagramTool.ZoomPan,
OnCreated = "DiagramSamples.created",
Nodes = (List<UiDiagramNode>)ViewData["nodes"],
Connectors = (List<UiDiagramConnector>)ViewData["connectors"]
})
@using System.Collections.Generic
<p>
This sample visualizes the connectors that automatically adjust to minimize visual overlap, ensuring clear and distinct
representations of connections in a circuit diagram. With <code>AvoidConnectorOverlap</code> on, the wires between the same blocks
run side by side instead of on top of each other. Drag the canvas to move the view.
</p>
@Html.UiDiagram(new UiDiagramOptions
{
Id = "overlapDiagram",
Height = "600px",
AriaLabel = "Circuit diagram",
LineRouting = true,
AvoidConnectorOverlap = true,
Tool = UiDiagramTool.ZoomPan,
OnCreated = "DiagramSamples.created",
Nodes = (List<UiDiagramNode>)ViewData["nodes"],
Connectors = (List<UiDiagramConnector>)ViewData["connectors"]
})
<script>
window.DiagramSamples = (function () {
'use strict';
/** OnCreated: the whole circuit fits in the view. */
function created() {
window.setTimeout(function () { window.Lumen.get('overlapDiagram').fitToPage(); }, 200);
}
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/AvoidConnectorOverlap.cshtml; it draws what ViewData holds.
public ActionResult AvoidConnectorOverlap()
{
// The outline of each kind of block, drawn into the block's size.
string wide = "M 55 0 L 55 7.5 L 80 7.5 L 55 7.5 L 55 22.5 L 80 22.5 L 55 22.5 L 55 37.5 L 80 37.5 L 55 37.5 L 55 52.5 L 80 52.5 L 55 52.5 L 55 67.5 L 80 67.5 L 55 67.5 L 55 82.5 L 80 82.5 L 55 82.5 L 55 97.5 L 80 97.5 L 55 97.5 L 55 112.5 L 80 112.5 L 55 112.5 L 55 127.5 L 80 127.5 L 55 127.5 L 55 142.5 L 80 142.5 L 55 142.5 L 55 157.5 L 80 157.5 L 55 157.5 L 55 172.5 L 80 172.5 L 55 172.5 L 55 187.5 L 80 187.5 L 55 187.5 L 55 202.5 L 80 202.5 L 55 202.5 L 55 217.5 L 80 217.5 L 55 217.5 L 55 232.5 L 80 232.5 L 55 232.5 L 55 240 L 25 240 L 25 120 L 0 120 L 25 120 L 25 0 Z";
string adder = "M 75 0 L 75 7.5 L 100 7.5 L 75 7.5 L 75 22.5 L 100 22.5 L 75 22.5 L 75 37.5 L 100 37.5 L 75 37.5 L 75 52.5 L 100 52.5 L 75 52.5 L 75 67.5 L 100 67.5 L 75 67.5 L 75 135 L 25 135 L 25 127.5 L 0 127.5 L 25 127.5 L 25 112.5 L 0 112.5 L 25 112.5 L 25 97.5 L 0 97.5 L 25 97.5 L 25 82.5 L 0 82.5 L 25 82.5 L 25 67.5 L 0 67.5 L 25 67.5 L 25 52.5 L 0 52.5 L 25 52.5 L 25 37.5 L 0 37.5 L 25 37.5 L 25 22.5 L 0 22.5 L 25 22.5 L 25 7.5 L 0 7.5 L 25 7.5 L 25 0 Z";
string merge = "M 55 0 L 55 120 L 80 120 L 55 120 L 55 240 L 25 240 L 25 232.5 L 0 232.5 L 25 232.5 L 25 217.5 L 0 217.5 L 25 217.5 L 25 202.5 L 0 202.5 L 25 202.5 L 25 187.5 L 0 187.5 L 25 187.5 L 25 172.5 L 0 172.5 L 25 172.5 L 25 157.5 L 0 157.5 L 25 157.5 L 25 142.5 L 0 142.5 L 25 142.5 L 25 127.5 L 0 127.5 L 25 127.5 L 25 112.5 L 0 112.5 L 25 112.5 L 25 97.5 L 0 97.5 L 25 97.5 L 25 82.5 L 0 82.5 L 25 82.5 L 25 67.5 L 0 67.5 L 25 67.5 L 25 52.5 L 0 52.5 L 25 52.5 L 25 37.5 L 0 37.5 L 25 37.5 L 25 22.5 L 0 22.5 L 25 22.5 L 25 7.5 L 0 7.5 L 25 7.5 L 25 0 Z";
string input = "M 35 0 L 35 15 L 60 15 L 35 15 L 35 30 L 0 30 L 0 0 Z";
string output = "M 60 0 L 60 30 L 25 30 L 25 15 L 0 15 L 25 15 L 25 0 Z";
var nodes = new List<UiDiagramNode>();
// A block: a path with a black outline, and an optional text in the middle.
Func<string, string, double, double, double, double, string, UiDiagramNode> block = (id, path, x, y, width, height, text) =>
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = UiDiagramShapeType.Path, Data = path },
Style = new UiDiagramStyle { StrokeColor = "#000000", StrokeWidth = 2 }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text, Style = new UiDiagramStyle { FontSize = 14 } }); }
nodes.Add(node);
return node;
};
// Round black ports down one side of a block, evenly spaced: "in" on the left, "out" on the right (factor is how many the side is divided into).
Action<UiDiagramNode, int, string, int> addPorts = (node, count, side, factor) =>
{
for (int i = 1; i <= count; i++)
{
node.Ports.Add(new UiDiagramPort
{
Id = node.Id + "_" + side + "_" + (i - 1),
Offset = new UiDiagramPoint { X = side == "out" ? 1 : 0, Y = count > 1 ? i / (double)factor - 1 / (2.0 * factor) : 0.5 },
Visibility = UiDiagramPortVisibility.Visible, Shape = UiDiagramPortShape.Circle, Width = 8, Height = 8,
Style = new UiDiagramStyle { Fill = "#000000" }
});
}
};
// A small text by each port: its number, or the given names.
Action<UiDiagramNode, int, string, string[], int> addLabels = (node, count, side, names, factor) =>
{
for (int i = 1; i <= count; i++)
{
node.Annotations.Add(new UiDiagramAnnotation
{
Content = names != null ? names[i - 1] : (i - 1).ToString(),
Offset = new UiDiagramPoint { X = side == "out" ? (node.Width - 12.5) / node.Width : 12.5 / node.Width, Y = i / (double)factor - 1 / (2.0 * factor) },
Style = new UiDiagramStyle { FontSize = 7 }, VerticalAlignment = UiDiagramVerticalAlignment.Bottom, Margin = new UiDiagramMargin { Bottom = 2 }
});
}
};
// The two 16-wire blocks on the left, the four 4-bit adders, the 16-wire block on the right, then the inputs and outputs.
UiDiagramNode a = block("node1", wide, 205, 180, 80, 240, null);
addPorts(a, 1, "in", 1); addPorts(a, 16, "out", 16); addLabels(a, 16, "out", null, 16);
UiDiagramNode b = block("node2", wide, 205, 427.5, 80, 240, null);
addPorts(b, 1, "in", 1); addPorts(b, 16, "out", 16); addLabels(b, 16, "out", null, 16);
string[] leftNames = { "A_0", "A_1", "A_2", "A_3", "B_0", "B_1", "B_2", "B_3", "Cin" };
string[] rightNames = { "S_0", "S_1", "S_2", "S_3", "Cout" };
double[,] adders = { { 415, 127.5 }, { 415, 367.5 }, { 615, 127.5 }, { 615, 367.5 } };
for (int i = 0; i < 4; i++)
{
UiDiagramNode adderNode = block("node" + (3 + i), adder, adders[i, 0], adders[i, 1], 100, 135, "4 Bit\nRCA");
addPorts(adderNode, 9, "in", 9); addPorts(adderNode, 5, "out", 9);
addLabels(adderNode, 9, "in", leftNames, 9); addLabels(adderNode, 5, "out", rightNames, 9);
}
UiDiagramNode sum = block("node7", merge, 820, 240, 80, 240, null);
addPorts(sum, 16, "in", 16); addPorts(sum, 1, "out", 1); addLabels(sum, 16, "in", null, 16);
// The inputs and outputs: the text sits clear of the port.
Action<string, double, double, string, bool> terminal = (id, x, y, text, isInput) =>
{
UiDiagramNode node = block(id, isInput ? input : output, x, y, 80, 30, text);
addPorts(node, 1, isInput ? "out" : "in", 1);
double middle = (80 - 25) / (2 * 80.0);
node.Annotations[0].Offset = new UiDiagramPoint { X = isInput ? middle : 1 - middle, Y = 0.5 };
};
terminal("node8", 70, 40, "Cin", true);
terminal("node9", 70, 180, "A", true);
terminal("node10", 70, 427.5, "B", true);
terminal("node11", 950, 240, "S", false);
terminal("node12", 950, 367.5, "Cout", false);
// A wire: from an out port of one block to an in port of another, a right angle at a time, with rounded corners.
string dark = "#005100", light = "#1AD81A", orange = "#FFA500";
var connectors = new List<UiDiagramConnector>();
Action<string, string, string, int, int, string> wire = (id, from, to, outPort, inPort, color) => connectors.Add(new UiDiagramConnector
{
Id = id, SourceID = from, TargetID = to, SourcePortID = from + "_out_" + outPort, TargetPortID = to + "_in_" + inPort,
Type = UiDiagramConnectorType.Orthogonal, CornerRadius = 5,
Style = new UiDiagramStyle { StrokeColor = color, StrokeWidth = 2 },
TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.None }
});
wire("connector01", "node8", "node3", 0, 8, light);
wire("connector02", "node9", "node1", 0, 0, orange);
wire("connector03", "node10", "node2", 0, 0, orange);
wire("connector04", "node7", "node11", 0, 0, orange);
wire("connector05", "node6", "node12", 4, 0, dark);
wire("connector06", "node3", "node5", 4, 8, dark);
wire("connector07", "node5", "node4", 4, 8, light);
wire("connector08", "node4", "node6", 4, 8, dark);
// Each 16-wire block (node1, node2) sends four wires to each adder, and each adder sends four to the block on the right (node7).
// The light green ones are marked so a few can be followed.
var lightWires = new HashSet<int> { 5, 8, 9, 10, 12, 14, 15, 17, 18, 21, 22, 23, 24, 26, 29, 35, 40, 43, 48 };
string[] adderNames = { "node3", "node5", "node4", "node6" };
int number = 1;
for (int source = 0; source < 2; source++)
{
for (int target = 0; target < 4; target++)
{
for (int k = 0; k < 4; k++)
{
wire("connector" + number, source == 0 ? "node1" : "node2", adderNames[target], target * 4 + k, source * 4 + k, lightWires.Contains(number) ? light : dark);
number++;
}
}
}
for (int adderIndex = 0; adderIndex < 4; adderIndex++)
{
for (int k = 0; k < 4; k++)
{
wire("connector" + number, adderNames[adderIndex], "node7", k, adderIndex * 4 + k, lightWires.Contains(number) ? light : dark);
number++;
}
}
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>