Example of Local Data in ASP.NET MVC Diagram Control
Local Data: the organization chart is made from rows (each with an id and the id of the one it reports to) and laid out as a tree. The colour comes from each row's role; select a node to see its row.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Local Data: the organization chart is made from rows (each with an id and the id of the one it reports to) and laid out as a tree.
The colour comes from each row's role; select a node to see its row.
</p>
@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; }
}
var colours = { Executive: '#c5cae9', Manager: '#d7ecd9', Staff: '#fff3e0' };
/** DoBinding: each node's colour and tooltip from its row. */
function bind(node, row) {
node.style = node.style || {};
node.style.fill = colours[row.Role] || null;
node.tooltip = { content: row.Name + ', ' + row.Role };
}
/** OnSelectionChange: the selected node's row. */
function selected(args) {
var d = Lumen.get('dataDiagram');
var row = args.newValue.length ? d.getNodeData(args.newValue[0].id) : null;
status(row ? 'Selected: ' + row.Name + ', ' + row.Role + (row.ParentId ? ', reports to ' + d.getNodeData(row.ParentId).Name : '') + '.' : 'Nothing selected.');
}
return { bind: bind, 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/LocalData.cshtml; it draws what ViewData holds.
public ActionResult LocalData()
{
var rows = new List<Employee>
{
new Employee { Id = "e1", Name = "Maria Anders", Role = "Executive" },
new Employee { Id = "e2", ParentId = "e1", Name = "Ana Trujillo", Role = "Manager" },
new Employee { Id = "e3", ParentId = "e1", Name = "Thomas Hardy", Role = "Manager" },
new Employee { Id = "e4", ParentId = "e1", Name = "Hanna Moos", Role = "Manager" },
new Employee { Id = "e5", ParentId = "e2", Name = "Antonio Moreno", Role = "Staff" },
new Employee { Id = "e6", ParentId = "e2", Name = "Christina Berglund", Role = "Staff" },
new Employee { Id = "e7", ParentId = "e3", Name = "Frederique Citeaux", Role = "Staff" },
new Employee { Id = "e8", ParentId = "e4", Name = "Martin Sommer", Role = "Staff" },
new Employee { Id = "e9", ParentId = "e4", Name = "Laurence Lebihan", Role = "Staff" },
new Employee { Id = "e10", ParentId = "e4", Name = "Elizabeth Lincoln", Role = "Staff" }
};
ViewData["diagram"] = new UiDiagramOptions
{
Id = "dataDiagram",
Height = "460px",
AriaLabel = "Organization chart made from rows",
DataSource = new UiDiagramDataSource
{
Data = rows, IdField = "Id", ParentIdField = "ParentId", TextField = "Name",
NodeDefaults = new UiDiagramNode { Width = 150, Height = 50, Shape = new UiDiagramShape { Type = UiDiagramShapeType.Basic, Shape = "Rectangle", CornerRadius = 6 } },
ConnectorDefaults = new UiDiagramConnector { Type = UiDiagramConnectorType.Orthogonal, CornerRadius = 4 },
DoBinding = "DiagramSamples.bind"
},
Layout = new UiDiagramLayout { Type = UiDiagramLayoutType.HierarchicalTree, HorizontalSpacing = 24, VerticalSpacing = 60 },
OnSelectionChange = "DiagramSamples.selected"
};
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>