Example of Pert Chart in ASP.NET MVC Diagram Control
Pert Chart: a project's tasks from left to right, each after the tasks it waits for, with its duration and its earliest start and end (in days from the start).
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Pert Chart: a project's tasks from left to right, each after the tasks it waits for, with its duration and its earliest start and
end (in days from the start).
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
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/PertChart.cshtml; it draws what ViewData holds.
public ActionResult PertChart()
{
var tasks = new List<PertTask>
{
new PertTask { Id = "start", Name = "Start", Days = 0, After = new string[0] },
new PertTask { Id = "spec", Name = "Specification", Days = 5, After = new[] { "start" } },
new PertTask { Id = "design", Name = "Design", Days = 8, After = new[] { "spec" } },
new PertTask { Id = "hardware", Name = "Order hardware", Days = 10, After = new[] { "spec" } },
new PertTask { Id = "code", Name = "Programming", Days = 15, After = new[] { "design" } },
new PertTask { Id = "setup", Name = "Set up servers", Days = 4, After = new[] { "hardware" } },
new PertTask { Id = "manual", Name = "Write manual", Days = 6, After = new[] { "design" } },
new PertTask { Id = "test", Name = "Testing", Days = 7, After = new[] { "code", "setup" } },
new PertTask { Id = "train", Name = "Training", Days = 3, After = new[] { "manual", "test" } },
new PertTask { Id = "finish", Name = "Go live", Days = 0, After = new[] { "train" } }
};
var end = new Dictionary<string, int>();
var nodes = new List<UiDiagramNode>();
var links = new List<UiDiagramConnector>();
foreach (PertTask t in tasks)
{
int begin = t.After.Length == 0 ? 0 : t.After.Max(a => end[a]);
end[t.Id] = begin + t.Days;
var node = new UiDiagramNode
{
Id = t.Id, Width = 150, Height = 76,
Shape = new UiDiagramShape { Type = UiDiagramShapeType.Basic, Shape = "Rectangle", CornerRadius = 4 },
Style = new UiDiagramStyle { Fill = t.Days == 0 ? "#ffe0b2" : "#e8f0fe" }
};
node.Annotations.Add(new UiDiagramAnnotation { Content = t.Name, Offset = new UiDiagramPoint { X = 0.5, Y = 0.22 }, Style = new UiDiagramStyle { Bold = true } });
node.Annotations.Add(new UiDiagramAnnotation { Content = t.Days + " days", Offset = new UiDiagramPoint { X = 0.5, Y = 0.52 } });
node.Annotations.Add(new UiDiagramAnnotation { Content = "Day " + begin + " to " + end[t.Id], Offset = new UiDiagramPoint { X = 0.5, Y = 0.8 }, Style = new UiDiagramStyle { FontSize = 11 } });
nodes.Add(node);
links.AddRange(t.After.Select(a => Link(a + "_" + t.Id, a, t.Id)));
}
ViewData["diagram"] = new UiDiagramOptions
{
Id = "pertDiagram",
Height = "520px",
AriaLabel = "Pert chart of a project",
Nodes = nodes,
Connectors = links,
Layout = new UiDiagramLayout { Type = UiDiagramLayoutType.ComplexHierarchicalTree, Orientation = UiDiagramLayoutOrientation.LeftToRight, HorizontalSpacing = 30, VerticalSpacing = 60 }
};
return View();
}
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;
}
private class PertTask
{
public string Id;
public string Name;
public int Days;
public string[] After;
}
}
}
@* 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>