Example of Pareto Chart in ASP.NET Core Charts Control
This Pareto Chart example shows the causes of lateness as columns in falling order, with a line of their running share of the total.
@* 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 = "paretoChart",
Title = "Why staff were late",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pareto, XName = "Cause", YName = "Count", Name = "Late arrivals", ShowMarker = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "paretoChart",
Title = "Why staff were late",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pareto, XName = "Cause", YName = "Count", Name = "Late arrivals", ShowMarker = true }
}
})
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/ParetoChart.cshtml; it draws what ViewData holds.
public ActionResult ParetoChart()
{
ViewData["ChartData"] = LateCauses();
return View();
}
/// <summary>One cause of lateness and how often it happened: the rows of the Pareto sample.</summary>
public class LateCauseData
{
public string Cause { get; set; }
public int Count { get; set; }
}
private static List<LateCauseData> LateCauses()
{
return new List<LateCauseData>
{
new LateCauseData { Cause = "Traffic", Count = 56 },
new LateCauseData { Cause = "Child care", Count = 45 },
new LateCauseData { Cause = "Transit", Count = 27 },
new LateCauseData { Cause = "Weather", Count = 20 },
new LateCauseData { Cause = "Overslept", Count = 7 },
new LateCauseData { Cause = "Emergency", Count = 6 }
};
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Columns from largest down and the running share on a 0-100% axis on the right.