Example of UML Class Diagram in ASP.NET MVC Diagram Control
UML Class Diagram: the classes of an order, an interface and an enumeration, with an association, a composition, a realization and a dependency. A class box is as tall as its attributes and methods.
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
UML Class Diagram: the classes of an order, an interface and an enumeration, with an association, a composition, a
realization and a dependency. A class box is as tall as its attributes and methods.
</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/UmlClassDiagram.cshtml; it draws what ViewData holds.
public ActionResult UmlClassDiagram()
{
{
ViewData["palette"] = new UiSymbolPaletteOptions
{
Id = "classPalette", TargetId = "classDiagram", Height = "620px", Width = "100%", AriaLabel = "UML class shapes",
Palettes = new List<UiSymbolPalette>
{
new UiSymbolPalette
{
Id = "classifiers", Title = "Classifiers",
Symbols = new List<UiDiagramNode>
{
ClassBox("class", 0, 0, "Class", "NewClass", new[] { "- field: int" }, new[] { "+ method(): void" }),
ClassBox("interface", 0, 0, "Interface", "INew", new string[0], new[] { "+ method(): void" }),
ClassBox("enumeration", 0, 0, "Enumeration", "NewEnum", new[] { "One", "Two" }, new string[0], 160)
}
},
new UiSymbolPalette
{
Id = "relationships", Title = "Relationships",
ConnectorSymbols = new List<UiDiagramConnector>
{
LinkSymbol("associationLink", Flow("x", null, null, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.OpenArrow)),
LinkSymbol("inheritance", Flow("x", null, null, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.Arrow, true)),
LinkSymbol("realization", Flow("x", null, null, null, UiDiagramConnectorType.Straight, "6 4", UiDiagramDecoratorShape.Arrow, true)),
LinkSymbol("aggregation", Flow("x", null, null, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.None, false, UiDiagramDecoratorShape.Diamond, true)),
LinkSymbol("composition", Flow("x", null, null, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.None, false, UiDiagramDecoratorShape.Diamond)),
LinkSymbol("dependency", Flow("x", null, null, null, UiDiagramConnectorType.Straight, "6 4", UiDiagramDecoratorShape.OpenArrow))
}
}
}
};
}
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "classDiagram",
Height = "620px",
AriaLabel = "UML class diagram of orders",
Nodes = new List<UiDiagramNode>
{
ClassBox("customer", 120, 80, "Class", "Customer", new[] { "- id: int", "- name: string", "- email: string" }, new[] { "+ placeOrder(): Order" }, 190),
ClassBox("order", 400, 80, "Class", "Order", new[] { "- id: int", "- date: DateTime", "- status: OrderStatus" }, new[] { "+ total(): decimal", "+ addLine(p: Product, n: int)" }, 230),
ClassBox("line", 400, 330, "Class", "OrderLine", new[] { "- quantity: int", "- price: decimal" }, new[] { "+ subtotal(): decimal" }),
ClassBox("product", 680, 330, "Class", "Product", new[] { "- id: int", "- name: string", "- price: decimal" }, new string[0], 180),
ClassBox("payable", 680, 70, "Interface", "IPayable", new string[0], new[] { "+ pay(amount: decimal): bool" }, 200),
ClassBox("status", 120, 330, "Enumeration", "OrderStatus", new[] { "New", "Paid", "Shipped", "Cancelled" }, new string[0], 160)
},
Connectors = new List<UiDiagramConnector>
{
Flow("places", "customer", "order", "places", UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.OpenArrow),
Flow("lines", "order", "line", "1..*", UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.None, false, UiDiagramDecoratorShape.Diamond),
Flow("ofProduct", "line", "product", null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.OpenArrow),
Flow("realizes", "order", "payable", null, UiDiagramConnectorType.Straight, "6 4", UiDiagramDecoratorShape.Arrow, true),
Flow("uses", "order", "status", null, UiDiagramConnectorType.Straight, "6 4", UiDiagramDecoratorShape.OpenArrow)
}
};
}
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;
}
private static UiDiagramNode ClassBox(string id, double x, double y, string kind, string name, string[] attributes, string[] methods, double width = 200)
{
UiDiagramNode n = Shape(id, x, y, UiDiagramShapeType.UmlClassifier, kind, width, 100, null, kind == "Class" ? "#e8f0fe" : kind == "Interface" ? "#e6f4ea" : "#fff4e5");
n.Shape.Classifier = new UiDiagramClassifier { Name = name, Attributes = attributes.ToList(), Methods = methods.ToList() };
return n;
}
}
}
@* 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>