Example of Fishbone Diagram in ASP.NET MVC Diagram Control
Fishbone Diagram: the causes of late deliveries, grouped into six kinds along the spine that leads to the problem.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Fishbone Diagram: the causes of late deliveries, grouped into six kinds along the spine that leads to the problem.
</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/FishboneDiagram.cshtml; it draws what ViewData holds.
public ActionResult FishboneDiagram()
{
const double spineY = 300, spineEnd = 900;
var nodes = new List<UiDiagramNode>
{
Node("problem", 990, spineY, "Late deliveries", UiDiagramShapeType.Basic, "Rectangle", 150, 70, "#ffcdd2")
};
nodes[0].Annotations[0].Style = new UiDiagramStyle { Bold = true, FontSize = 14 };
var spine = new UiDiagramConnector { Id = "spine", SourcePoint = new UiDiagramPoint { X = 40, Y = spineY }, TargetID = "problem", Type = UiDiagramConnectorType.Straight, Style = new UiDiagramStyle { StrokeWidth = 3 } };
var links = new List<UiDiagramConnector> { spine };
var groups = new[]
{
new { Id = "people", Name = "People", X = 200.0, Top = true, Causes = new[] { "Too few drivers", "New staff not trained" } },
new { Id = "process", Name = "Process", X = 440.0, Top = true, Causes = new[] { "Orders checked by hand", "No priority rules" } },
new { Id = "equipment", Name = "Equipment", X = 680.0, Top = true, Causes = new[] { "Old vans break down", "Scanners fail" } },
new { Id = "materials", Name = "Materials", X = 200.0, Top = false, Causes = new[] { "Boxes out of stock", "Wrong labels" } },
new { Id = "environment", Name = "Environment", X = 440.0, Top = false, Causes = new[] { "Traffic in the city", "Winter weather" } },
new { Id = "management", Name = "Management", X = 680.0, Top = false, Causes = new[] { "Targets not shared", "Late schedules" } }
};
foreach (var g in groups)
{
double y = g.Top ? 80 : 520;
UiDiagramNode head = Node(g.Id, g.X, y, g.Name, UiDiagramShapeType.Basic, "Rectangle", 130, 40, "#e3f2fd");
nodes.Add(head);
double sx = g.X + 170, sy = spineY;
links.Add(new UiDiagramConnector { Id = g.Id + "Bone", SourceID = g.Id, TargetPoint = new UiDiagramPoint { X = sx, Y = sy }, Type = UiDiagramConnectorType.Straight, Style = new UiDiagramStyle { StrokeWidth = 2 } });
for (int k = 0; k < g.Causes.Length; k++)
{
double t = (k + 1.0) / (g.Causes.Length + 1);
double px = g.X + (sx - g.X) * t, py = y + (g.Top ? 20 : -20) + (sy - y - (g.Top ? 20 : -20)) * t;
string cid = g.Id + "Cause" + (k + 1);
nodes.Add(new UiDiagramNode
{
Id = cid, OffsetX = px - 105, OffsetY = py, Width = 150, Height = 30,
Shape = new UiDiagramShape { Type = UiDiagramShapeType.Text, Content = g.Causes[k] }, Style = new UiDiagramStyle { FontSize = 12 }
});
var c = new UiDiagramConnector { Id = cid + "Line", SourceID = cid, TargetPoint = new UiDiagramPoint { X = px, Y = py }, Type = UiDiagramConnectorType.Straight };
c.TargetDecorator = new UiDiagramDecorator { Shape = UiDiagramDecoratorShape.Arrow, Width = 8, Height = 6 };
links.Add(c);
}
}
ViewData["diagram"] = new UiDiagramOptions { Id = "fishboneDiagram", Height = "620px", AriaLabel = "Fishbone diagram of the causes of late deliveries", Nodes = nodes, Connectors = links };
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;
}
}
}
@* 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>