Example of BPMN Editor in ASP.NET MVC Diagram Control
BPMN Editor: an order process in BPMN: events, tasks, gateways, a data object and an annotation. Drag shapes and flows from the palette (or press Enter on one) to change it; sequence flows are solid, message flows dashed, associations dotted.
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
BPMN Editor: an order process in BPMN: events, tasks, gateways, a data object and an annotation. Drag shapes and flows from the
palette (or press Enter on one) to change it; sequence flows are solid, message flows dashed, associations dotted.
</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/BpmnEditor.cshtml; it draws what ViewData holds.
public ActionResult BpmnEditor()
{
{
ViewData["palette"] = new UiSymbolPaletteOptions
{
Id = "bpmnPalette", TargetId = "bpmnDiagram", Height = "600px", Width = "100%", AriaLabel = "BPMN shapes",
Palettes = new List<UiSymbolPalette>
{
Group("events", "Events", UiDiagramShapeType.Bpmn, "StartEvent:44:44", "MessageStartEvent:44:44", "TimerStartEvent:44:44", "IntermediateEvent:44:44",
"TimerIntermediateEvent:44:44", "ErrorIntermediateEvent:44:44", "EndEvent:44:44", "MessageEndEvent:44:44", "ErrorEndEvent:44:44", "TerminateEndEvent:44:44"),
Group("activities", "Activities", UiDiagramShapeType.Bpmn, "Task:120:64", "UserTask:120:64", "ServiceTask:120:64", "ScriptTask:120:64",
"SendTask:120:64", "ReceiveTask:120:64", "SubProcess:120:64"),
Group("gateways", "Gateways", UiDiagramShapeType.Bpmn, "ExclusiveGateway:54:54", "ParallelGateway:54:54", "InclusiveGateway:54:54", "EventGateway:54:54"),
Group("artifacts", "Data and artifacts", UiDiagramShapeType.Bpmn, "DataObject:40:54", "DataStore:60:54", "Message:50:36", "TextAnnotation:120:50", "Group:200:140"),
new UiSymbolPalette
{
Id = "flows", Title = "Flows",
ConnectorSymbols = new List<UiDiagramConnector>
{
LinkSymbol("sequenceFlow", Flow("x", null, null)),
LinkSymbol("messageFlow", Flow("x", null, null, null, UiDiagramConnectorType.Orthogonal, "6 4", UiDiagramDecoratorShape.Arrow, true, UiDiagramDecoratorShape.Circle, true)),
LinkSymbol("association", Flow("x", null, null, null, UiDiagramConnectorType.Straight, "2 3", UiDiagramDecoratorShape.None))
}
}
}
};
}
{
UiDiagramNode order = Shape("orderData", 330, 70, UiDiagramShapeType.Bpmn, "DataObject", 40, 54, null);
UiDiagramNode inStock;
order.Annotations.Add(new UiDiagramAnnotation { Content = "Order", Offset = new UiDiagramPoint { X = 0.5, Y = 1.25 } });
inStock = Shape("inStock", 330, 200, UiDiagramShapeType.Bpmn, "ExclusiveGateway", 54, 54, null);
inStock.Annotations.Add(new UiDiagramAnnotation { Content = "In stock?", Offset = new UiDiagramPoint { X = 0.5, Y = -0.3 } });
ViewData["diagram"] = new UiDiagramOptions
{
Id = "bpmnDiagram",
Height = "600px",
AriaLabel = "BPMN process of an order",
Nodes = new List<UiDiagramNode>
{
Shape("received", 60, 200, UiDiagramShapeType.Bpmn, "MessageStartEvent", 44, 44),
Shape("check", 190, 200, UiDiagramShapeType.Bpmn, "Task", 120, 64, "Check stock"),
inStock,
Shape("charge", 480, 200, UiDiagramShapeType.Bpmn, "ServiceTask", 120, 64, "Charge the card"),
Shape("ship", 640, 200, UiDiagramShapeType.Bpmn, "SendTask", 120, 64, "Ship the order"),
Shape("done", 780, 200, UiDiagramShapeType.Bpmn, "EndEvent", 44, 44),
Shape("supplier", 330, 350, UiDiagramShapeType.Bpmn, "UserTask", 130, 64, "Order from supplier"),
Shape("wait", 480, 350, UiDiagramShapeType.Bpmn, "TimerIntermediateEvent", 44, 44),
Shape("declined", 640, 330, UiDiagramShapeType.Bpmn, "ErrorEndEvent", 44, 44),
order,
Shape("note", 760, 330, UiDiagramShapeType.Bpmn, "TextAnnotation", 140, 50, "Shipped within two days")
},
Connectors = new List<UiDiagramConnector>
{
Flow("b1", "received", "check"),
Flow("b2", "check", "inStock"),
Flow("b3", "inStock", "charge", "Yes"),
Flow("b4", "charge", "ship"),
Flow("b5", "ship", "done"),
Flow("b6", "inStock", "supplier", "No"),
Flow("b7", "supplier", "wait"),
Flow("b8", "wait", "charge"),
Flow("b9", "charge", "declined", "Card declined"),
Flow("b10", "orderData", "check", null, UiDiagramConnectorType.Straight, "2 3", UiDiagramDecoratorShape.OpenArrow),
Flow("b11", "note", "ship", null, UiDiagramConnectorType.Straight, "2 3", UiDiagramDecoratorShape.None)
}
};
}
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>