Example of Commands in ASP.NET MVC Diagram Control
Commands: select shapes (Shift or Ctrl and click, or Ctrl+A), then copy, cut and paste them (Ctrl+C, Ctrl+X, Ctrl+V), line them up with the first selected, space three or more evenly, or give them the first one's size.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Commands: select shapes (Shift or Ctrl and click, or Ctrl+A), then copy, cut and paste them (Ctrl+C, Ctrl+X, Ctrl+V), line them up
with the first selected, space three or more evenly, or give them the first one's size.
</p>
<div class="mb-2" role="toolbar" aria-label="Clipboard" data-dg-target="commandsDiagram">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="copy">Copy</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="cut">Cut</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="paste">Paste</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="selectAll">Select all</button>
</div>
<div class="mb-2" role="toolbar" aria-label="Align" data-dg-target="commandsDiagram">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Left">Align left</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Center">Align center</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Right">Align right</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Top">Align top</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Middle">Align middle</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="align" data-dg-arg="Bottom">Align bottom</button>
</div>
<div class="mb-2" role="toolbar" aria-label="Distribute and size" data-dg-target="commandsDiagram">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="distribute" data-dg-arg="RightToLeft">Space across</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="distribute" data-dg-arg="BottomToTop">Space down</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="sameSize" data-dg-arg="Width">Same width</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="sameSize" data-dg-arg="Height">Same height</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="sameSize" data-dg-arg="Size">Same size</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; }
}
/** OnSelectionChange: what is selected. */
function selected(args) {
status(args.newValue.length ? 'Selected: ' + args.newValue.map(function (o) { return o.id; }).join(', ') : 'Nothing selected.');
}
/** The toolbar's buttons: data-dg-command is the diagram's method, data-dg-arg its argument. */
document.addEventListener('click', function (e) {
var button = e.target.closest ? e.target.closest('[data-dg-command]') : null;
if (!button) { return; }
var bar = button.closest('[data-dg-target]');
var diagram = bar ? Lumen.get(bar.getAttribute('data-dg-target')) : null;
if (!diagram) { return; }
var arg = button.getAttribute('data-dg-arg');
var done = diagram[button.getAttribute('data-dg-command')](arg === null ? undefined : arg);
var nothing = done === false || done === null || done === 0 || (Array.isArray(done) && !done.length);
status(button.textContent + (nothing ? ': nothing to do (select more, or something else).' : ': done.'));
});
return { selected: selected };
})();
</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/Commands.cshtml; it draws what ViewData holds.
public ActionResult Commands()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "commandsDiagram",
Height = "460px",
AriaLabel = "Diagram for the commands",
Nodes = new List<UiDiagramNode>
{
Node("one", 100, 90, "One", UiDiagramShapeType.Basic, "Rectangle", 100, 50, "#e8f0fe"),
Node("two", 260, 160, "Two", UiDiagramShapeType.Basic, "Ellipse", 130, 70, "#e6f4ea"),
Node("three", 470, 110, "Three", UiDiagramShapeType.Basic, "Hexagon", 90, 80, "#fff4e5"),
Node("four", 640, 220, "Four", UiDiagramShapeType.Basic, "Rectangle", 120, 60, "#f3e5f5")
},
OnSelectionChange = "DiagramSamples.selected"
};
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;
}
}
}
@* 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>