Example of UML Sequence Diagram in ASP.NET MVC Diagram Control
UML Sequence Diagram: signing in. Each participant is a lifeline; the calls are solid arrows, the replies dashed; the alt fragment shows the two outcomes.
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
UML Sequence Diagram: signing in. Each participant is a lifeline; the calls are solid arrows, the replies dashed; the alt
fragment shows the two outcomes.
</p>
<div class="row g-2">
<div class="col-lg-3">
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
</div>
<div class="col-lg-9">
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
</div>
</div>
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/UmlSequenceDiagram.cshtml; it draws what ViewData holds.
public ActionResult UmlSequenceDiagram()
{
{
UiDiagramNode lifeline = Shape("lifeline", 0, 0, UiDiagramShapeType.UmlSequence, "Lifeline", 120, 300, "Object");
lifeline.Shape.HeaderHeight = 40;
ViewData["palette"] = new UiSymbolPaletteOptions
{
Id = "sequencePalette", TargetId = "sequenceDiagram", Height = "560px", Width = "100%", AriaLabel = "UML sequence shapes",
Palettes = new List<UiSymbolPalette>
{
new UiSymbolPalette
{
Id = "participants", Title = "Participants",
Symbols = new List<UiDiagramNode> { lifeline, Shape("activation", 0, 0, UiDiagramShapeType.UmlSequence, "Activation", 12, 80), Shape("fragment", 0, 0, UiDiagramShapeType.UmlSequence, "Fragment", 240, 120, "loop") }
},
new UiSymbolPalette
{
Id = "messages", Title = "Messages",
ConnectorSymbols = new List<UiDiagramConnector>
{
LinkSymbol("call", Flow("x", null, null, null, UiDiagramConnectorType.Straight)),
LinkSymbol("reply", Flow("x", null, null, null, UiDiagramConnectorType.Straight, "5 4", UiDiagramDecoratorShape.OpenArrow))
}
}
}
};
}
{
const double top = 30, length = 470, head = 40;
string[] names = { "User", "Web page", "Server", "Database" };
var nodes = new List<UiDiagramNode>();
for (int i = 0; i < names.Length; i++)
{
UiDiagramNode l = Shape("life" + i, 110 + i * 200, top + length / 2, UiDiagramShapeType.UmlSequence, "Lifeline", 120, length, names[i], "#e8f0fe");
l.Shape.HeaderHeight = head;
nodes.Add(l);
}
// The messages: from, to, the height (from the top of the lifelines), the text, a reply?
var messages = new[]
{
new { From = 0, To = 1, At = 80.0, Text = "Sign in", Reply = false },
new { From = 1, To = 2, At = 120.0, Text = "POST /login", Reply = false },
new { From = 2, To = 3, At = 160.0, Text = "Find user", Reply = false },
new { From = 3, To = 2, At = 200.0, Text = "User", Reply = true },
new { From = 2, To = 1, At = 260.0, Text = "200 OK", Reply = true },
new { From = 2, To = 1, At = 330.0, Text = "401 Unauthorized", Reply = true },
new { From = 1, To = 0, At = 400.0, Text = "Show the result", Reply = true }
};
var connectors = new List<UiDiagramConnector>();
for (int m = 0; m < messages.Length; m++)
{
var msg = messages[m];
string port = "m" + m;
nodes[msg.From].Ports.Add(new UiDiagramPort { Id = port + "s", Offset = new UiDiagramPoint { X = 0.5, Y = msg.At / length }, Visibility = UiDiagramPortVisibility.Hidden });
nodes[msg.To].Ports.Add(new UiDiagramPort { Id = port + "t", Offset = new UiDiagramPoint { X = 0.5, Y = msg.At / length }, Visibility = UiDiagramPortVisibility.Hidden });
UiDiagramConnector c = Flow("msg" + m, "life" + msg.From, "life" + msg.To, msg.Text, UiDiagramConnectorType.Straight, msg.Reply ? "5 4" : null,
msg.Reply ? UiDiagramDecoratorShape.OpenArrow : UiDiagramDecoratorShape.Arrow);
c.SourcePortID = port + "s";
c.TargetPortID = port + "t";
c.Annotations[0].Offset = new UiDiagramPoint { X = 0.5, Y = 0 };
connectors.Add(c);
}
nodes.Add(Shape("act1", 310, top + 190, UiDiagramShapeType.UmlSequence, "Activation", 12, 260, null, "#ffffff"));
nodes.Add(Shape("act2", 510, top + 180, UiDiagramShapeType.UmlSequence, "Activation", 12, 40, null, "#ffffff"));
UiDiagramNode alt = Shape("alt", 410, top + 295, UiDiagramShapeType.UmlSequence, "Fragment", 300, 130, "alt");
alt.Annotations.Add(new UiDiagramAnnotation { Content = "[password right]", Offset = new UiDiagramPoint { X = 0.3, Y = 0.3 } });
alt.Annotations.Add(new UiDiagramAnnotation { Content = "[else]", Offset = new UiDiagramPoint { X = 0.15, Y = 0.72 } });
nodes.Add(alt);
ViewData["diagram"] = new UiDiagramOptions { Id = "sequenceDiagram", Height = "560px", AriaLabel = "UML sequence diagram of signing in", Nodes = nodes, Connectors = connectors };
}
return View();
}
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;
}
private static UiDiagramNode Shape(string id, double x, double y, UiDiagramShapeType type, string shape, double width, double height, string text = null, 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;
}
/// <summary>A connector of a given look: its line's dashes, its ends' decorators (a hollow end has a transparent fill).</summary>
private static UiDiagramConnector Flow(string id, string from, string to, string label = null, UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal,
string dashes = null, UiDiagramDecoratorShape target = UiDiagramDecoratorShape.Arrow, bool hollowTarget = false,
UiDiagramDecoratorShape source = UiDiagramDecoratorShape.None, bool hollowSource = false)
{
UiDiagramConnector c = Link(id, from, to, label, type);
c.Style = new UiDiagramStyle { StrokeDashArray = dashes };
c.TargetDecorator = new UiDiagramDecorator { Shape = target, Width = 12, Height = 10, Style = new UiDiagramStyle { Fill = hollowTarget ? "transparent" : null } };
c.SourceDecorator = new UiDiagramDecorator { Shape = source, Width = 14, Height = 10, Style = new UiDiagramStyle { Fill = hollowSource ? "transparent" : null } };
return c;
}
/// <summary>A connector symbol for a palette: a sample of the line, not attached to anything.</summary>
private static UiDiagramConnector LinkSymbol(string id, UiDiagramConnector look)
{
look.Id = id;
look.SourceID = null;
look.TargetID = null;
look.SourcePoint = new UiDiagramPoint { X = 0, Y = 0 };
look.TargetPoint = new UiDiagramPoint { X = 120, Y = 60 };
look.Annotations.Clear();
return look;
}
}
}
@* 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>