Example of Flow Execution in ASP.NET MVC Diagram Control
Flow Execution: run the order's flow chart. Each step lights up in turn and a dot runs along the connector to the next; at the decision the flow goes the way chosen below.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Flow Execution: run the order's flow chart. Each step lights up in turn and a dot runs along the connector to the next; at the
decision the flow goes the way chosen below.
</p>
<div class="d-flex flex-wrap gap-3 align-items-center mb-2">
<fieldset class="d-flex gap-2 align-items-center border-0 p-0 m-0">
<legend class="small mb-0 me-1 float-none w-auto fs-6">In stock?</legend>
<div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="inStock" id="stockYes" value="Yes" checked><label class="form-check-label" for="stockYes">Yes</label></div>
<div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="inStock" id="stockNo" value="No"><label class="form-check-label" for="stockNo">No</label></div>
</fieldset>
<label class="d-flex align-items-center gap-1">Speed
<select class="form-select form-select-sm" id="speed"><option value="1500">Slow</option><option value="800" selected>Normal</option><option value="300">Fast</option></select>
</label>
<button type="button" class="btn btn-sm btn-outline-primary" id="runFlow">Run</button>
</div>
@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; }
}
/** Runs fn once the component has started. */
function whenReady(id, fn) {
var c = window.Lumen && Lumen.get(id);
if (c) { fn(c); } else { window.setTimeout(function () { whenReady(id, fn); }, 50); }
}
var ID = 'executionDiagram';
var running = false;
function diagram() { return Lumen.get(ID); }
function light(lit, now) {
var d = diagram();
d.nodes.forEach(function (n) { n.style.fill = n.id === now ? '#fff59d' : lit[n.id] ? '#c8e6c9' : null; });
d.dataBind();
}
/** Follows the flow from Start: each step lit, a dot along each connector; at the decision, the way chosen. */
function run() {
if (running) { return; }
running = true;
var button = document.getElementById('runFlow');
button.disabled = true;
var d = diagram();
var choice = document.querySelector('input[name="inStock"]:checked').value;
var speed = Number(document.getElementById('speed').value);
var lit = {};
var at = 'begin';
var steps = 0;
var visit = function () {
steps++;
light(lit, at);
var node = d.getNodeObject(at);
status('Step ' + steps + ': ' + node.annotations[0].content + '.');
var outs = d.connectors.filter(function (c) { return c.sourceID === at; });
if (!outs.length) {
lit[at] = true;
light(lit, null);
running = false;
button.disabled = false;
status('Finished in ' + steps + ' steps.');
return;
}
var next = outs.length > 1 ? outs.filter(function (c) { return c.annotations.length && c.annotations[0].content === choice; })[0] || outs[0] : outs[0];
d.animateConnector(next.id, speed).then(function () {
lit[at] = true;
at = next.targetID;
visit();
});
};
visit();
}
document.addEventListener('click', function (e) { if (e.target && e.target.id === 'runFlow') { run(); } });
return { run: run };
})();
</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/FlowExecution.cshtml; it draws what ViewData holds.
public ActionResult FlowExecution()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "executionDiagram",
Height = "620px",
AriaLabel = "Order flow to run",
ReadOnly = true,
Nodes = new List<UiDiagramNode>
{
Node("begin", 300, 50, "Start", UiDiagramShapeType.Flow, "Terminator", 140, 46),
Node("receive", 300, 140, "Receive the order", UiDiagramShapeType.Flow, "Process", 160, 54),
Node("stock", 300, 250, "In stock?", UiDiagramShapeType.Flow, "Decision", 150, 80),
Node("supplier", 530, 250, "Order from supplier", UiDiagramShapeType.Flow, "Process", 160, 54),
Node("pack", 300, 370, "Pack and ship", UiDiagramShapeType.Flow, "Process", 160, 54),
Node("invoice", 300, 460, "Send the invoice", UiDiagramShapeType.Flow, "Document", 160, 60),
Node("end", 300, 540, "End", UiDiagramShapeType.Flow, "Terminator", 140, 46)
},
Connectors = new List<UiDiagramConnector>
{
Link("x1", "begin", "receive"), Link("x2", "receive", "stock"), Link("x3", "stock", "pack", "Yes"), Link("x4", "stock", "supplier", "No"),
Link("x5", "supplier", "pack"), Link("x6", "pack", "invoice"), Link("x7", "invoice", "end")
}
};
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>