Example of Container in ASP.NET MVC Diagram Control
Container: each service holds its parts. Move a container and its parts move with it. Drag the Audit log into a container and it joins it; drag a part out and it leaves. The status says where a moved node is.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Container: each service holds its parts. Move a container and its parts move with it. Drag the Audit log into a container and it
joins it; drag a part out and it leaves. The status says where a moved node is.
</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; }
}
function nameOf(o) { return o.annotations && o.annotations.length && o.annotations[0].content ? o.annotations[0].content : o.id; }
/** OnPositionChange: where a moved node is, once the move is finished (it may have joined or left a lane or a container). */
function moved(args) {
if (args.state !== 'Completed') { return; }
var diagram = Lumen.get(document.querySelector('.ui-diagram').id);
window.setTimeout(function () {
var p = diagram.getParent(args.source.id);
status(nameOf(args.source) + (p ? ' is in ' + (p.lane ? 'the ' + p.lane + ' lane' : nameOf(p.node)) : ' is not in a lane or a container') + '.');
}, 0);
}
return { moved: moved };
})();
</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/Container.cshtml; it draws what ViewData holds.
public ActionResult Container()
{
System.Func<string, double, double, string, string[], UiDiagramNode> box = (id, x, y, title, children) =>
{
var n = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = 300, Height = 230,
Shape = new UiDiagramShape { Type = UiDiagramShapeType.Container, HeaderHeight = 32 },
Style = new UiDiagramStyle { Fill = "#fafafa", StrokeColor = "#5c6bc0" },
Children = children.ToList()
};
n.Annotations.Add(new UiDiagramAnnotation { Content = title, Style = new UiDiagramStyle { Bold = true } });
return n;
};
ViewData["diagram"] = new UiDiagramOptions
{
Id = "containerDiagram",
Height = "500px",
AriaLabel = "Services in containers",
Nodes = new List<UiDiagramNode>
{
box("payments", 190, 160, "Payment service", new[] { "api", "ledger" }),
box("shipping", 560, 160, "Shipping service", new[] { "labels" }),
Node("api", 190, 125, "Payments API", UiDiagramShapeType.Basic, "Rectangle", 140, 50, "#e8f0fe"),
Node("ledger", 190, 220, "Ledger", UiDiagramShapeType.Basic, "Cylinder", 100, 60, "#e8f0fe"),
Node("labels", 560, 170, "Label printer", UiDiagramShapeType.Basic, "Rectangle", 140, 50, "#e6f4ea"),
Node("audit", 380, 400, "Audit log", UiDiagramShapeType.Flow, "Document", 130, 60, "#fff4e5")
},
Connectors = new List<UiDiagramConnector> { Link("payToLabels", "api", "labels", "Paid") },
OnPositionChange = "DiagramSamples.moved"
};
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>