Example of Mind Map Tree in ASP.NET MVC Diagram Control
Mind Map Tree: the plan in the middle, its topics to the right and the left (the side with less gets the next one). Select a topic and add one under it; the map is laid out again.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Mind Map Tree: the plan in the middle, its topics to the right and the left (the side with less gets the next one). Select a topic and
add one under it; the map is laid out again.
</p>
<div class="mb-2" role="toolbar" aria-label="Mind map">
<button type="button" class="btn btn-sm btn-outline-primary" id="addTopic">Add a topic</button>
<button type="button" class="btn btn-sm btn-outline-primary" id="relayout">Lay out again</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; }
}
var count = 0;
function diagram() { return Lumen.get('mindMapDiagram'); }
/** OnSelectionChange: what is selected. */
function selected(args) {
status(args.newValue.length ? 'Selected: ' + args.newValue.map(function (o) { return o.id; }).join(', ') : 'Nothing selected.');
}
document.addEventListener('click', function (e) {
if (!e.target || !e.target.id) { return; }
var d = diagram();
if (e.target.id === 'addTopic') {
var parent = d.selectedItems.nodes[0];
if (!parent) { status('Select a topic first.'); return; }
count++;
var made = d.addNode({ width: 110, height: 36, shape: { type: 'Basic', shape: 'Rectangle', cornerRadius: 4 }, style: { fill: '#e8f0fe' }, annotations: [{ content: 'New topic ' + count }] });
d.addConnector({ sourceID: parent.id, targetID: made.id, type: 'Bezier', targetDecorator: { shape: 'None' } });
d.doLayout();
status('Added ' + made.id + ' under ' + parent.id + '.');
} else if (e.target.id === 'relayout') {
d.doLayout();
status('Laid out again.');
}
});
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/MindMapTree.cshtml; it draws what ViewData holds.
public ActionResult MindMapTree()
{
UiDiagramOptions o = Graph("mindMapDiagram", "Mind map of a business plan",
"plan:Business plan,goals:Goals,g1:Grow revenue,g2:New markets,g3:Hire,market:Market,m1:Customers,m2:Competitors," +
"product:Product,p1:Features,p2:Pricing,p3:Roadmap,finance:Finance,f1:Budget,f2:Funding,team:Team,t1:Roles,t2:Culture",
"plan>goals,goals>g1,goals>g2,goals>g3,plan>market,market>m1,market>m2,plan>product,product>p1,product>p2,product>p3," +
"plan>finance,finance>f1,finance>f2,plan>team,team>t1,team>t2",
new UiDiagramLayout { Type = UiDiagramLayoutType.MindMap, HorizontalSpacing = 16, VerticalSpacing = 50 },
UiDiagramShapeType.Basic, "Rectangle", 110, 36, UiDiagramConnectorType.Bezier);
UiDiagramNode root = o.Nodes[0];
root.Shape.Shape = "Ellipse";
root.Width = 150;
root.Height = 70;
root.Style.Fill = "#ffe0b2";
foreach (UiDiagramConnector c in o.Connectors) { c.TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.None }; }
o.OnSelectionChange = "DiagramSamples.selected";
ViewData["diagram"] = o;
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;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
/// <summary>
/// A diagram of the layouts' samples: nodes written "id:Text" and connectors "from>to"; the layout places them, so they have no
/// position of their own.
/// </summary>
private static UiDiagramOptions Graph(string id, string label, string nodes, string links, UiDiagramLayout layout,
UiDiagramShapeType type = UiDiagramShapeType.Basic, string shape = "Rectangle", double width = 120, double height = 44,
UiDiagramConnectorType connectorType = UiDiagramConnectorType.Orthogonal)
{
var list = nodes.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Select(n =>
{
string[] parts = n.Trim().Split(':');
UiDiagramNode node = Node(parts[0], 0, 0, parts.Length > 1 ? parts[1] : parts[0], type, shape, width, height, "#e8f0fe");
node.Shape.CornerRadius = shape == "Rectangle" ? 4 : 0;
return node;
}).ToList();
var connectors = links.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Select(l =>
{
string[] ends = l.Trim().Split('>');
return Link(ends[0] + "_" + ends[1], ends[0], ends[1], null, connectorType);
}).ToList();
return new UiDiagramOptions { Id = id, Height = "560px", AriaLabel = label, Nodes = list, Connectors = connectors, Layout = layout };
}
}
}
@* 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>