Example of BoxAndWhisker in ASP.NET MVC Charts Control
This Box and Whisker Chart example shows how the ages of the employees of each department are spread, with the outliers marked.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "boxChart",
Title = "Employee age by department",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.BoxAndWhisker, XName = "Department", YName = "Ages", Name = "Age" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "boxChart",
Title = "Employee age by department",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.BoxAndWhisker, XName = "Department", YName = "Ages", Name = "Age" }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/BoxAndWhisker.cshtml; it draws what ViewData holds.
public ActionResult BoxAndWhisker()
{
ViewData["ChartData"] = DepartmentAges();
return View();
}
/// <summary>The ages of the employees of one department: the rows of the Box and Whisker sample.</summary>
public class AgeData
{
public string Department { get; set; }
public double[] Ages { get; set; }
}
private static List<AgeData> DepartmentAges()
{
return new List<AgeData>
{
new AgeData { Department = "Development", Ages = new double[] { 22, 22, 23, 25, 25, 25, 26, 27, 27, 28, 28, 29, 30, 32, 34, 32, 34, 36, 35, 38 } },
new AgeData { Department = "Testing", Ages = new double[] { 22, 33, 23, 25, 26, 28, 29, 30, 34, 33, 32, 31, 50 } },
new AgeData { Department = "HR", Ages = new double[] { 22, 24, 25, 30, 32, 34, 36, 38, 39, 41, 35, 36, 40, 56 } },
new AgeData { Department = "Finance", Ages = new double[] { 26, 27, 28, 30, 32, 34, 35, 37, 35, 37, 45 } },
new AgeData { Department = "Sales", Ages = new double[] { 27, 26, 28, 29, 29, 29, 32, 35, 32, 38, 53 } }
};
}
}
}
@* 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>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
YName holds a list of numbers per row. Quartiles by BoxPlotMode; outliers (beyond 1.5 IQR) as dots; the mean as a cross.