Example of Entity Relationship Diagram in ASP.NET MVC Diagram Control
Entity Relationship Diagram: customers, orders and products in Chen's notation: entities (a weak entity has a double border), relationships (diamonds) and attributes (a key is underlined, a list has a double border, a derived one is dashed).
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Entity Relationship Diagram: customers, orders and products in Chen's notation: entities (a weak entity has a double
border), relationships (diamonds) and attributes (a key is underlined, a list has a double border, a derived one is dashed).
</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/EntityRelationshipDiagram.cshtml; it draws what ViewData holds.
public ActionResult EntityRelationshipDiagram()
{
{
ViewData["palette"] = new UiSymbolPaletteOptions
{
Id = "erPalette", TargetId = "erDiagram", Height = "560px", Width = "100%", AriaLabel = "Entity relationship shapes",
Palettes = new List<UiSymbolPalette>
{
Group("entities", "Entities and relationships", UiDiagramShapeType.Er, "Entity:120:50", "WeakEntity:120:50", "Relationship:110:70", "IdentifyingRelationship:110:70"),
Group("attributes", "Attributes", UiDiagramShapeType.Er, "Attribute:100:46", "KeyAttribute:100:46", "MultivaluedAttribute:100:46", "DerivedAttribute:100:46"),
new UiSymbolPalette { Id = "lines", Title = "Lines", ConnectorSymbols = new List<UiDiagramConnector> { LinkSymbol("erLine", Flow("x", null, null, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.None)) } }
}
};
}
{
Func<string, string, string, UiDiagramConnector> line = (id, from, to) => Flow(id, from, to, null, UiDiagramConnectorType.Straight, null, UiDiagramDecoratorShape.None);
Func<string, string, string, string, UiDiagramConnector> card = (id, from, to, text) =>
{
UiDiagramConnector c = line(id, from, to);
c.Annotations.Add(new UiDiagramAnnotation { Content = text, Offset = new UiDiagramPoint { X = 0.8, Y = 0 } });
return c;
};
ViewData["diagram"] = new UiDiagramOptions
{
Id = "erDiagram",
Height = "560px",
AriaLabel = "Entity relationship diagram of orders",
Nodes = new List<UiDiagramNode>
{
Shape("customer", 120, 260, UiDiagramShapeType.Er, "Entity", 120, 50, "Customer", "#e8f0fe"),
Shape("places", 285, 260, UiDiagramShapeType.Er, "Relationship", 110, 70, "Places", "#fff4e5"),
Shape("order", 450, 260, UiDiagramShapeType.Er, "Entity", 120, 50, "Order", "#e8f0fe"),
Shape("contains", 615, 260, UiDiagramShapeType.Er, "Relationship", 110, 70, "Contains", "#fff4e5"),
Shape("product", 780, 260, UiDiagramShapeType.Er, "Entity", 120, 50, "Product", "#e8f0fe"),
Shape("line", 615, 420, UiDiagramShapeType.Er, "WeakEntity", 120, 50, "Order line", "#e8f0fe"),
Shape("has", 615, 520, UiDiagramShapeType.Er, "IdentifyingRelationship", 110, 70, "Has", "#fff4e5"),
Shape("customerId", 60, 140, UiDiagramShapeType.Er, "KeyAttribute", 110, 46, "CustomerId", "#e6f4ea"),
Shape("name", 190, 130, UiDiagramShapeType.Er, "Attribute", 100, 46, "Name", "#e6f4ea"),
Shape("phones", 120, 390, UiDiagramShapeType.Er, "MultivaluedAttribute", 110, 46, "Phones", "#e6f4ea"),
Shape("orderId", 400, 130, UiDiagramShapeType.Er, "KeyAttribute", 100, 46, "OrderId", "#e6f4ea"),
Shape("total", 520, 130, UiDiagramShapeType.Er, "DerivedAttribute", 100, 46, "Total", "#e6f4ea"),
Shape("productId", 780, 140, UiDiagramShapeType.Er, "KeyAttribute", 110, 46, "ProductId", "#e6f4ea")
},
Connectors = new List<UiDiagramConnector>
{
card("e1", "places", "customer", "1"), card("e2", "places", "order", "N"), card("e3", "contains", "order", "1"), card("e4", "contains", "product", "N"),
line("e5", "customer", "customerId"), line("e6", "customer", "name"), line("e7", "customer", "phones"), line("e8", "order", "orderId"),
line("e9", "order", "total"), line("e10", "product", "productId"), line("e11", "contains", "line"), line("e12", "line", "has")
}
};
}
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 UiSymbolPalette Group(string id, string title, UiDiagramShapeType type, params string[] shapes)
{
return new UiSymbolPalette
{
Id = id, Title = title,
Symbols = shapes.Select(sh =>
{
string[] parts = sh.Split(':');
double w = parts.Length > 1 ? double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture) : 100;
double h = parts.Length > 2 ? double.Parse(parts[2], System.Globalization.CultureInfo.InvariantCulture) : 60;
return Shape(parts[0].ToLowerInvariant(), 0, 0, type, parts[0], w, h);
}).ToList()
};
}
}
}
@* 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>