Example of Network Diagram in ASP.NET MVC Diagram Control
Network Diagram: an office network. Select a device and take it offline: what can no longer reach the internet goes grey and its links red. Rest the pointer on a device (or reach it with Tab) for its address.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Network Diagram: an office network. Select a device and take it offline: what can no longer reach the internet goes grey and its
links red. Rest the pointer on a device (or reach it with Tab) for its address.
</p>
<div class="d-flex flex-wrap gap-2 mb-2" role="group" aria-label="Devices">
<button type="button" class="btn btn-sm btn-outline-primary" id="toggleDevice" disabled>Take offline</button>
<button type="button" class="btn btn-sm btn-outline-secondary" id="allOnline">Bring all online</button>
</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; }
}
/** Runs fn once the component has started. */
function whenReady(id, fn) {
var c = window.Lumen && Lumen.get(id);
if (c) { fn(c); } else { window.setTimeout(function () { whenReady(id, fn); }, 50); }
}
var ID = 'networkDiagram';
var offline = {};
var fills = null;
function diagram() { return Lumen.get(ID); }
/** What can reach the internet through devices that are online; the rest goes grey, and their links red. */
function update() {
var d = diagram();
if (!fills) { fills = {}; d.nodes.forEach(function (n) { fills[n.id] = n.style.fill; }); }
var reach = {};
var queue = offline.internet ? [] : ['internet'];
reach.internet = !offline.internet;
while (queue.length) {
var at = queue.shift();
d.connectors.forEach(function (c) {
var other = c.sourceID === at ? c.targetID : c.targetID === at ? c.sourceID : null;
if (other && !reach[other] && !offline[other]) { reach[other] = true; queue.push(other); }
});
}
var cut = [];
d.nodes.forEach(function (n) {
n.style.fill = reach[n.id] ? fills[n.id] : '#e0e0e0';
n.style.opacity = offline[n.id] ? 0.6 : 1;
if (!reach[n.id]) { cut.push(n.annotations[0].content); }
});
d.connectors.forEach(function (c) {
var broken = !reach[c.sourceID] || !reach[c.targetID];
c.style.strokeColor = broken ? '#c62828' : null;
c.style.strokeDashArray = broken ? '5 4' : null;
});
d.dataBind();
status(cut.length ? cut.length + ' cut off: ' + cut.join(', ') + '.' : 'Everything is online.');
selected();
}
/** OnSelectionChange: the button says what it will do to the selected device. */
function selected() {
var d = diagram();
var n = d ? d.selectedItems.nodes[0] : null;
var b = document.getElementById('toggleDevice');
b.disabled = !n;
b.textContent = n && offline[n.id] ? 'Bring ' + n.annotations[0].content + ' online' : n ? 'Take ' + n.annotations[0].content + ' offline' : 'Take offline';
}
document.addEventListener('click', function (e) {
var id = e.target && e.target.id;
if (id === 'toggleDevice') {
var n = diagram().selectedItems.nodes[0];
if (n) { offline[n.id] = !offline[n.id]; update(); }
} else if (id === 'allOnline') {
offline = {};
update();
}
});
whenReady(ID, update);
return { selected: selected };
})();
</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/NetworkDiagram.cshtml; it draws what ViewData holds.
public ActionResult NetworkDiagram()
{
System.Func<string, double, double, string, string, string, double, double, string, UiDiagramNode> device = (id, x, y, text, kind, shape, w, h, tip) =>
{
UiDiagramNode n = Node(id, x, y, text, UiDiagramShapeType.Basic, shape, w, h, kind);
n.Tooltip = new UiDiagramTooltip { Content = tip };
return n;
};
var nodes = new List<UiDiagramNode>
{
device("internet", 420, 50, "Internet", "#e3f2fd", "Ellipse", 140, 60, "The internet provider's line"),
device("firewall", 420, 150, "Firewall", "#ffcdd2", "Rectangle", 120, 46, "Firewall, 10.0.0.1"),
device("router", 420, 240, "Router", "#fff9c4", "Hexagon", 120, 56, "Core router, 10.0.0.2"),
device("switchA", 230, 340, "Switch A", "#e8f5e9", "Rectangle", 110, 44, "Office floor 1, 10.0.1.1"),
device("switchB", 610, 340, "Switch B", "#e8f5e9", "Rectangle", 110, 44, "Office floor 2, 10.0.2.1"),
device("server", 420, 340, "File server", "#ede7f6", "Cylinder", 90, 70, "File server, 10.0.0.10")
};
var links = new List<UiDiagramConnector>
{
Link("n1", "internet", "firewall", null, UiDiagramConnectorType.Straight), Link("n2", "firewall", "router", null, UiDiagramConnectorType.Straight),
Link("n3", "router", "switchA", null, UiDiagramConnectorType.Straight), Link("n4", "router", "switchB", null, UiDiagramConnectorType.Straight), Link("n5", "router", "server", null, UiDiagramConnectorType.Straight)
};
for (int i = 1; i <= 3; i++)
{
nodes.Add(device("pcA" + i, 110 + (i - 1) * 120, 460, "PC A" + i, "#ffffff", "Rectangle", 90, 40, "Desk A" + i + ", 10.0.1." + (10 + i)));
nodes.Add(device("pcB" + i, 490 + (i - 1) * 120, 460, "PC B" + i, "#ffffff", "Rectangle", 90, 40, "Desk B" + i + ", 10.0.2." + (10 + i)));
links.Add(Link("la" + i, "switchA", "pcA" + i, null, UiDiagramConnectorType.Straight));
links.Add(Link("lb" + i, "switchB", "pcB" + i, null, UiDiagramConnectorType.Straight));
}
foreach (UiDiagramConnector c in links) { c.TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.None }; c.Style = new UiDiagramStyle { StrokeWidth = 2 }; }
ViewData["diagram"] = new UiDiagramOptions { Id = "networkDiagram", Height = "540px", AriaLabel = "Office network", Nodes = nodes, Connectors = links, OnSelectionChange = "DiagramSamples.selected" };
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>