Example of Nested Donut in ASP.NET MVC Charts Control
This Nested Doughnut Chart example compares the sales of two quarters of the regions as rings inside each other.
@* 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 = "nestedDonutChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "420px",
Title = "Sales by region: Q4 (outer) and Q1 (inner)",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Right },
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Region", YName = "Q4", Name = "Q4", ShowDataLabel = true },
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Region", YName = "Q1", Name = "Q1", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "nestedDonutChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "420px",
Title = "Sales by region: Q4 (outer) and Q1 (inner)",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Right },
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Region", YName = "Q4", Name = "Q4", ShowDataLabel = true },
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Region", YName = "Q1", Name = "Q1", ShowDataLabel = 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/NestedDonut.cshtml; it draws what ViewData holds.
public ActionResult NestedDonut()
{
ViewData["ChartData"] = RegionSalesData();
return View();
}
/// <summary>The sales of one region per quarter: the rows of the Bar, Stacked Column and Bar Gradient samples.</summary>
public class RegionData
{
public string Region { get; set; }
public double Q1 { get; set; }
public double Q2 { get; set; }
public double Q3 { get; set; }
public double Q4 { get; set; }
}
private static List<RegionData> RegionSalesData()
{
return new List<RegionData>
{
new RegionData { Region = "Luzon", Q1 = 42000, Q2 = 48500, Q3 = 51200, Q4 = 60300 },
new RegionData { Region = "Visayas", Q1 = 27500, Q2 = 29800, Q3 = 33100, Q4 = 36900 },
new RegionData { Region = "Mindanao", Q1 = 21300, Q2 = 24100, Q3 = 23800, Q4 = 28700 },
new RegionData { Region = "Metro Manila", Q1 = 55800, Q2 = 58200, Q3 = 61900, Q4 = 70400 }
};
}
}
}
@* 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. *@
Several doughnut series in one chart are drawn as rings inside each other (a nested doughnut): the first series is the outer ring. Here the outer ring is the sales of the fourth quarter and the inner ring those of the first.