Example of Doughnut Chart in ASP.NET Core Charts Control
This donut chart example visualizes mobile browser statistics. Data labels and the center label provide information about the data in the series.
${x}
Browser Share: ${y}%
Browser Share: ${y}%
@Html.UiChart(new UiChartOptions
{
Id = "doughnutChart",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipDoughnutChart" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Browser", YName = "Users", Name = "Project", ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside, InnerRadius = "65%", CornerRadius = 3, StartAngle = 62 }
}
})
<template id="tipDoughnutChart"><b>${x}</b><br>Browser Share: <b>${y}%</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "doughnutChart",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipDoughnutChart" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Doughnut, XName = "Browser", YName = "Users", Name = "Project", ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside, InnerRadius = "65%", CornerRadius = 3, StartAngle = 62 }
}
})
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/DoughnutChart.cshtml; it draws what ViewData holds.
public ActionResult DoughnutChart()
{
List<DoughnutChartData> ChartPoints = new List<DoughnutChartData>
{
new DoughnutChartData { Browser = "Chrome", Users = 63.5, DataLabelMappingName = "Chrome: 63.5%" },
new DoughnutChartData { Browser = "Safari", Users = 25.0, DataLabelMappingName = "Safari: 25.0%" },
new DoughnutChartData { Browser = "Samsung Internet", Users = 6.0, DataLabelMappingName = "Samsung Internet: 6.0%" },
new DoughnutChartData { Browser = "UC Browser", Users = 2.5, DataLabelMappingName = "UC Browser: 2.5%" },
new DoughnutChartData { Browser = "Opera", Users = 1.5, DataLabelMappingName = "Opera: 1.5%" },
new DoughnutChartData { Browser = "Others", Users = 1.5, DataLabelMappingName = "Others: 1.5%" }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class DoughnutChartData
{
public string Browser { get; set; }
public double Users { get; set; }
public string DataLabelMappingName { get; set; }
}
}
}
@* 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. *@
In this example, you can see how to render and configure a donut chart. To create a donut in the pie series, we use the InnerRadius property. The CenterLabel property allows you to specify the default text that will be rendered in the center. You can also customize the text that will render when the mouse pointer is hovered over one of the donut slices using the HoverTextFormat property.