Example of Symbol Palette in ASP.NET MVC Diagram Control
Symbol Palette: drag a shape or a connector from the palette onto the diagram. With the keyboard, Tab to a group of symbols, move with the arrow keys and press Enter to add the symbol to the middle of the diagram. Open and close the groups by their headers.
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Symbol Palette: drag a shape or a connector from the palette onto the diagram. With the keyboard, Tab to a group of symbols, move
with the arrow keys and press Enter to add the symbol to the middle of the diagram. Open and close the groups by their headers.
</p>
<div class="d-flex gap-2 align-items-start">
@Html.UiSymbolPalette((UiSymbolPaletteOptions)ViewData["palette"])
<div class="flex-grow-1" style="min-width:0">
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
</div>
</div>
<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; }
}
/** OnDrop: the symbol added from the palette. */
function dropped(args) {
var o = args.element;
status('Added ' + o.id + (o.offsetX !== undefined ? ' at ' + Math.round(o.offsetX) + ', ' + Math.round(o.offsetY) : '') + '.');
}
return { dropped: dropped };
})();
</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/SymbolPalette.cshtml; it draws what ViewData holds.
public ActionResult SymbolPalette()
{
{
System.Func<string, UiDiagramShapeType, string, double, double, UiDiagramNode> symbol = (id, type, shape, w, h) =>
new UiDiagramNode { Id = id, Width = w, Height = h, Shape = new UiDiagramShape { Type = type, Shape = shape } };
var container = new UiDiagramNode { Id = "container", Width = 240, Height = 180, Shape = new UiDiagramShape { Type = UiDiagramShapeType.Container } };
container.Annotations.Add(new UiDiagramAnnotation { Content = "Container" });
ViewData["palette"] = new UiSymbolPaletteOptions
{
Id = "palette",
TargetId = "paletteDiagram",
Height = "560px",
AriaLabel = "Shapes to add to the diagram",
Palettes = new List<UiSymbolPalette>
{
new UiSymbolPalette
{
Id = "flow", Title = "Flow shapes",
Symbols = new[] { "Terminator", "Process", "Decision", "Document", "Data", "PreDefinedProcess", "ManualInput", "Delay" }
.Select(s => symbol(s.ToLowerInvariant(), UiDiagramShapeType.Flow, s, s == "Decision" ? 100 : 120, s == "Decision" ? 80 : 54)).ToList()
},
new UiSymbolPalette
{
Id = "basic", Title = "Basic shapes",
Symbols = new[] { "Rectangle", "Ellipse", "Triangle", "Diamond", "Hexagon", "Star", "Cylinder", "Parallelogram" }
.Select(s => symbol(s.ToLowerInvariant(), UiDiagramShapeType.Basic, s, 100, 70)).ToList()
},
new UiSymbolPalette
{
Id = "connectors", Title = "Connectors",
ConnectorSymbols = new List<UiDiagramConnector>
{
new UiDiagramConnector { Id = "straight", Type = UiDiagramConnectorType.Straight, SourcePoint = new UiDiagramPoint { X = 0, Y = 0 }, TargetPoint = new UiDiagramPoint { X = 120, Y = 60 } },
new UiDiagramConnector { Id = "orthogonal", Type = UiDiagramConnectorType.Orthogonal, SourcePoint = new UiDiagramPoint { X = 0, Y = 0 }, TargetPoint = new UiDiagramPoint { X = 120, Y = 60 } },
new UiDiagramConnector { Id = "bezier", Type = UiDiagramConnectorType.Bezier, SourcePoint = new UiDiagramPoint { X = 0, Y = 0 }, TargetPoint = new UiDiagramPoint { X = 120, Y = 60 } }
}
},
new UiSymbolPalette { Id = "containers", Title = "Containers", Expanded = false, Symbols = new List<UiDiagramNode> { container } }
}
};
}
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "paletteDiagram",
Height = "560px",
AriaLabel = "Diagram to drop symbols on",
Nodes = new List<UiDiagramNode> { Node("begin", 140, 70, "Start", UiDiagramShapeType.Flow, "Terminator") },
OnDrop = "DiagramSamples.dropped"
};
}
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>