Example of Annotations in ASP.NET MVC Diagram Control
Texts on nodes and connectors: several on one node, placed by an offset (inside or outside the node), styled, wrapped at the node's width, and one that is a link. Double-click a text (or select its node and press F2) to change it.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Texts on nodes and connectors: several on one node, placed by an offset (inside or outside the node), styled, wrapped at the node's
width, and one that is a link. Double-click a text (or select its node and press <kbd>F2</kbd>) to change it.
</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; }
}
/** OnTextEdit: the old and the new text. */
function edited(args) {
status('"' + args.oldValue + '" became "' + args.newValue + '".');
}
return { edited: edited };
})();
</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/Annotations.cshtml; it draws what ViewData holds.
public ActionResult Annotations()
{
var a = Node("note", 160, 120, null, UiDiagramShapeType.Basic, "Rectangle", 220, 110, "#e8f0fe");
a.Annotations.Add(new UiDiagramAnnotation { Content = "Title", Offset = new UiDiagramPoint { X = 0.5, Y = 0.2 }, Style = new UiDiagramStyle { Bold = true, FontSize = 14 } });
a.Annotations.Add(new UiDiagramAnnotation { Content = "A longer description that wraps inside the node.", Offset = new UiDiagramPoint { X = 0.5, Y = 0.62 } });
var b = Node("linked", 480, 120, null, UiDiagramShapeType.Basic, "Ellipse", 160, 90, "#fff4ce");
b.Annotations.Add(new UiDiagramAnnotation { Content = "Read the guide", Hyperlink = "https://example.com/guide" });
var c = Node("below", 160, 330, null, UiDiagramShapeType.Basic, "Rectangle", 140, 60);
c.Annotations.Add(new UiDiagramAnnotation { Content = "Below the node", Offset = new UiDiagramPoint { X = 0.5, Y = 1.35 }, Style = new UiDiagramStyle { Italic = true, Color = "#616161" } });
c.Annotations.Add(new UiDiagramAnnotation { Content = "Inside" });
var link = Link("labelled", "note", "linked", "A connector's label", UiDiagramConnectorType.Straight);
link.Annotations[0].Offset = new UiDiagramPoint { X = 0.5 };
ViewData["diagram"] = new UiDiagramOptions
{
Id = "annotationsDiagram", Height = "460px", Nodes = new List<UiDiagramNode> { a, b, c }, Connectors = new List<UiDiagramConnector> { link },
OnTextEdit = "DiagramSamples.edited"
};
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>