Example of Symmetrical Layout in ASP.NET MVC Diagram Control
Symmetrical Layout: the network is laid out with springs along the connectors while the nodes push each other away. Change the springs' length.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Symmetrical Layout: the network is laid out with springs along the connectors while the nodes push each other away. Change the
springs' length.
</p>
<div class="d-flex flex-wrap gap-3 align-items-center mb-2" role="group" aria-label="Layout" data-dg-target="symmetricDiagram">
<label class="d-flex align-items-center gap-1">Spring length
<input class="form-control form-control-sm" type="number" data-layout="springLength" value="80" min="20" max="400" step="10" style="width:6em">
</label>
</div>
@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; }
}
/** The layout's controls: data-layout is the setting, the diagram is laid out again with it. */
document.addEventListener('change', function (e) {
var control = e.target && e.target.closest ? e.target.closest('[data-layout]') : null;
if (!control) { return; }
var diagram = Lumen.get(control.closest('[data-dg-target]').getAttribute('data-dg-target'));
var setting = {};
setting[control.getAttribute('data-layout')] = control.type === 'number' ? Number(control.value) : control.value;
diagram.doLayout(setting);
var l = diagram.getLayout();
status('Laid out again: ' + l.type + ', ' + l.orientation + (l.type === 'OrganizationalChart' ? ', leaves ' + l.leafLayout : '') + '.');
});
return {};
})();
</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/SymmetricalLayout.cshtml; it draws what ViewData holds.
public ActionResult SymmetricalLayout()
{
var nodes = new List<string> { "server:Server" };
var links = new List<string>();
for (int s = 1; s <= 3; s++)
{
nodes.Add("switch" + s + ":Switch " + s);
links.Add("server>switch" + s);
for (int c = 1; c <= 5; c++)
{
nodes.Add("pc" + s + c + ":PC " + s + "." + c);
links.Add("switch" + s + ">pc" + s + c);
}
}
UiDiagramOptions o = Graph("symmetricDiagram", "A network laid out with springs", string.Join(",", nodes), string.Join(",", links),
new UiDiagramLayout { Type = UiDiagramLayoutType.SymmetricalLayout, SpringLength = 80, SpringFactor = 0.442, MaxIteration = 30 },
UiDiagramShapeType.Basic, "Ellipse", 64, 36, UiDiagramConnectorType.Straight);
foreach (UiDiagramNode n in o.Nodes.Where(n => n.Id.StartsWith("switch") || n.Id == "server")) { n.Style.Fill = n.Id == "server" ? "#ffe0b2" : "#d7ecd9"; n.Width = 80; }
foreach (UiDiagramConnector c in o.Connectors) { c.TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.None }; }
ViewData["diagram"] = o;
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;
}
/// <summary>
/// A diagram of the layouts' samples: nodes written "id:Text" and connectors "from>to"; the layout places them, so they have no
/// position of their own.
/// </summary>
private static UiDiagramOptions Graph(string id, string label, string nodes, string links, UiDiagramLayout layout,
UiDiagramShapeType type = UiDiagramShapeType.Basic, string shape = "Rectangle", double width = 120, double height = 44,
UiDiagramConnectorType connectorType = UiDiagramConnectorType.Orthogonal)
{
var list = nodes.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Select(n =>
{
string[] parts = n.Trim().Split(':');
UiDiagramNode node = Node(parts[0], 0, 0, parts.Length > 1 ? parts[1] : parts[0], type, shape, width, height, "#e8f0fe");
node.Shape.CornerRadius = shape == "Rectangle" ? 4 : 0;
return node;
}).ToList();
var connectors = links.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Select(l =>
{
string[] ends = l.Trim().Split('>');
return Link(ends[0] + "_" + ends[1], ends[0], ends[1], null, connectorType);
}).ToList();
return new UiDiagramOptions { Id = id, Height = "560px", AriaLabel = label, Nodes = list, Connectors = connectors, Layout = layout };
}
}
}
@* 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>