Example of Hierarchical Layout in ASP.NET MVC Diagram Control
Hierarchical Layout: the categories are made from rows and laid out as a tree. Change the orientation and the spacing.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Hierarchical Layout: the categories are made from rows and laid out as a tree. Change the orientation and the spacing.
</p>
<div class="d-flex flex-wrap gap-3 align-items-center mb-2" role="group" aria-label="Layout" data-dg-target="hierarchyDiagram">
<label class="d-flex align-items-center gap-1">Orientation
<select class="form-select form-select-sm" data-layout="orientation"><option selected>TopToBottom</option><option>BottomToTop</option><option>LeftToRight</option><option>RightToLeft</option></select>
</label>
<label class="d-flex align-items-center gap-1">Across
<input class="form-control form-control-sm" type="number" data-layout="horizontalSpacing" value="24" min="0" max="200" step="4" style="width:6em">
</label>
<label class="d-flex align-items-center gap-1">Down
<input class="form-control form-control-sm" type="number" data-layout="verticalSpacing" value="50" min="0" max="200" step="10" style="width:6em">
</label>
</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; }
}
/** The layout's controls: data-layout is the setting, the diagram is laid out again with it. */
document.addEventListener('change', function (e) {
var control = e.target && e.target.closest ? e.target.closest('[data-layout]') : null;
if (!control) { return; }
var diagram = Lumen.get(control.closest('[data-dg-target]').getAttribute('data-dg-target'));
var setting = {};
setting[control.getAttribute('data-layout')] = control.type === 'number' ? Number(control.value) : control.value;
diagram.doLayout(setting);
var l = diagram.getLayout();
status('Laid out again: ' + l.type + ', ' + l.orientation + (l.type === 'OrganizationalChart' ? ', leaves ' + l.leafLayout : '') + '.');
});
return {};
})();
</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/HierarchicalLayout.cshtml; it draws what ViewData holds.
public ActionResult HierarchicalLayout()
{
var rows = new List<Employee>
{
new Employee { Id = "all", Name = "Products" },
new Employee { Id = "food", ParentId = "all", Name = "Food" },
new Employee { Id = "drink", ParentId = "all", Name = "Drinks" },
new Employee { Id = "home", ParentId = "all", Name = "Home" },
new Employee { Id = "bread", ParentId = "food", Name = "Bread" },
new Employee { Id = "dairy", ParentId = "food", Name = "Dairy" },
new Employee { Id = "fruit", ParentId = "food", Name = "Fruit" },
new Employee { Id = "tea", ParentId = "drink", Name = "Tea" },
new Employee { Id = "coffee", ParentId = "drink", Name = "Coffee" },
new Employee { Id = "clean", ParentId = "home", Name = "Cleaning" },
new Employee { Id = "kitchen", ParentId = "home", Name = "Kitchen" },
new Employee { Id = "cheese", ParentId = "dairy", Name = "Cheese" },
new Employee { Id = "milk", ParentId = "dairy", Name = "Milk" }
};
ViewData["diagram"] = new UiDiagramOptions
{
Id = "hierarchyDiagram",
Height = "520px",
AriaLabel = "Product categories as a tree",
DataSource = new UiDiagramDataSource
{
Data = rows, TextField = "Name",
NodeDefaults = new UiDiagramNode { Width = 100, Height = 40, Shape = new UiDiagramShape { Type = UiDiagramShapeType.Basic, Shape = "Rectangle", CornerRadius = 4 }, Style = new UiDiagramStyle { Fill = "#e8f0fe" } }
},
Layout = new UiDiagramLayout { Type = UiDiagramLayoutType.HierarchicalTree, HorizontalSpacing = 24, VerticalSpacing = 50 }
};
return View();
}
/// <summary>A row of the Local Data sample: an employee and the one they report to.</summary>
public class Employee
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Name { get; set; }
public string Role { get; set; }
}
}
}
@* 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>