Example of Diagram Binding Treeview in ASP.NET MVC Diagram Control
Diagram Binding Treeview: the team as a tree and as a chart. Select in either and the other follows; collapse a branch in the tree and the chart hides it and is laid out again; rename in the tree (F2) and the chart shows the new name.
- Maria Anders
- Design
- Ana Trujillo
- Thomas Hardy
- Engineering
- Web team
- Hanna Moos
- Martin Sommer
- Apps team
- Yang Wang
- Aria Cruz
@Html.UiTreeView((UiTreeViewOptions)ViewData["tree"])
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Diagram Binding Treeview: the team as a tree and as a chart. Select in either and the other follows; collapse a branch in the tree and
the chart hides it and is laid out again; rename in the tree (F2) and the chart shows the new name.
</p>
<div class="row g-2">
<div class="col-lg-3">
@Html.UiTreeView((UiTreeViewOptions)ViewData["tree"])
</div>
<div class="col-lg-9">
@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; }
}
/** 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 syncing = false;
var collapsed = {};
function diagram() { return Lumen.get('boundDiagram'); }
function tree() { return Lumen.get('teamTree'); }
/** The tree's selection selects the chart's node and brings it to the middle. */
function treeSelected(node) {
if (syncing || !diagram()) { return; }
syncing = true;
diagram().select(node.id);
diagram().bringToCenter(node.id);
syncing = false;
status('Selected in the tree: ' + node.text + '.');
}
/** The chart's selection selects the tree's node (its branch opened). */
function diagramSelected(args) {
var n = args.newValue[0];
if (syncing || !n || !n.visible || !tree()) { return; }
syncing = true;
tree().expandNode(n.id);
tree().selectNode(n.id);
syncing = false;
status('Selected in the chart: ' + n.annotations[0].content + '.');
}
/** A collapsed branch is hidden in the chart, which is laid out again. */
function show() {
var d = diagram();
var parent = {};
d.connectors.forEach(function (c) { parent[c.targetID] = c.sourceID; });
var hidden = function (id) { for (var p = parent[id]; p; p = parent[p]) { if (collapsed[p]) { return true; } } return false; };
syncing = true;
d.nodes.forEach(function (n) { n.visible = !hidden(n.id); });
d.connectors.forEach(function (c) { c.visible = !hidden(c.targetID); });
d.dataBind();
d.doLayout();
syncing = false;
}
function treeCollapsed(node) { collapsed[node.id] = true; show(); status(node.text + "'s branch is hidden."); }
function treeExpanded(node) { if (collapsed[node.id]) { delete collapsed[node.id]; show(); status(node.text + "'s branch is shown."); } }
/** A name changed in the tree is the node's text in the chart. */
function treeEdited(node) {
var n = diagram().getNodeObject(node.id);
if (!n) { return; }
n.annotations[0].content = node.text;
diagram().dataBind();
status('Renamed: ' + node.oldText + ' is now ' + node.text + '.');
}
/** Names are changed in the tree (it keeps the two the same). */
function diagramEdited(args) {
args.cancel = true;
status('Rename in the tree (select the name and press F2).');
}
return { treeSelected: treeSelected, diagramSelected: diagramSelected, treeCollapsed: treeCollapsed, treeExpanded: treeExpanded, treeEdited: treeEdited, diagramEdited: diagramEdited };
})();
</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/DiagramBindingTreeview.cshtml; it draws what ViewData holds.
public ActionResult DiagramBindingTreeview()
{
{
System.Func<string, UiTreeNode> make = null;
make = id =>
{
Employee row = TeamRows.First(r => r.Id == id);
var node = new UiTreeNode { Id = row.Id, Text = row.Name, Expanded = true };
foreach (Employee kid in TeamRows.Where(r => r.ParentId == id)) { node.Children.Add(make(kid.Id)); }
return node;
};
ViewData["tree"] = new UiTreeViewOptions
{
Id = "teamTree", AriaLabel = "The team", Height = "520px", AllowEditing = true, Nodes = new List<UiTreeNode> { make("lead") },
OnNodeSelected = "DiagramSamples.treeSelected", OnNodeExpanded = "DiagramSamples.treeExpanded", OnNodeCollapsed = "DiagramSamples.treeCollapsed",
OnNodeEdited = "DiagramSamples.treeEdited"
};
}
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "boundDiagram",
Height = "520px",
AriaLabel = "The team as a chart",
DataSource = new UiDiagramDataSource
{
Data = TeamRows, TextField = "Name",
NodeDefaults = new UiDiagramNode { Width = 110, Height = 44, Shape = new UiDiagramShape { Type = UiDiagramShapeType.Basic, Shape = "Rectangle", CornerRadius = 4 }, Style = new UiDiagramStyle { Fill = "#e8f0fe" } },
ConnectorDefaults = new UiDiagramConnector { Type = UiDiagramConnectorType.Orthogonal, CornerRadius = 3 }
},
Layout = new UiDiagramLayout { Type = UiDiagramLayoutType.HierarchicalTree, HorizontalSpacing = 14, VerticalSpacing = 50 },
OnSelectionChange = "DiagramSamples.diagramSelected",
OnTextEdit = "DiagramSamples.diagramEdited"
};
}
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; }
}
private static readonly Employee[] TeamRows =
{
new Employee { Id = "lead", Name = "Maria Anders", Role = "Executive" },
new Employee { Id = "design", ParentId = "lead", Name = "Design", Role = "Director" },
new Employee { Id = "build", ParentId = "lead", Name = "Engineering", Role = "Director" },
new Employee { Id = "ux", ParentId = "design", Name = "Ana Trujillo", Role = "Staff" },
new Employee { Id = "visual", ParentId = "design", Name = "Thomas Hardy", Role = "Staff" },
new Employee { Id = "web", ParentId = "build", Name = "Web team", Role = "Manager" },
new Employee { Id = "apps", ParentId = "build", Name = "Apps team", Role = "Manager" },
new Employee { Id = "w1", ParentId = "web", Name = "Hanna Moos", Role = "Staff" },
new Employee { Id = "w2", ParentId = "web", Name = "Martin Sommer", Role = "Staff" },
new Employee { Id = "a1", ParentId = "apps", Name = "Yang Wang", Role = "Staff" },
new Employee { Id = "a2", ParentId = "apps", Name = "Aria Cruz", Role = "Staff" }
};
}
}
@* 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>