Example of Pie Chart in ASP.NET Core Charts Control
This Pie Chart example demonstrates a pie chart for global electricity generation. Datalabels show information about the points.
${x}
Percentage: ${y}%
Percentage: ${y}%
@Html.UiChart(new UiChartOptions
{
Id = "pieChart",
Title = "Global Electricity Generation by Source - 2024",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipPieChart" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Browser", YName = "Users", Name = "Browser", ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside, InnerRadius = "0%", CornerRadius = 3 }
}
})
<template id="tipPieChart"><b>${x}</b><br>Percentage: <b>${y}%</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "pieChart",
Title = "Global Electricity Generation by Source - 2024",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipPieChart" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Browser", YName = "Users", Name = "Browser", ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside, InnerRadius = "0%", CornerRadius = 3 }
}
})
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/PieChart.cshtml; it draws what ViewData holds.
public ActionResult PieChart()
{
List<PieChartData> ChartPoints = new List<PieChartData>
{
new PieChartData { Browser= "Coal", Users= 34.4, DataLabelMappingName= "Coal: 34.4%" },
new PieChartData { Browser= "Natural Gas", Users= 22.1, DataLabelMappingName= "Natural Gas: 22.1%" },
new PieChartData { Browser= "Hydro", Users= 14.4, DataLabelMappingName= "Hydro: 14.4%" },
new PieChartData { Browser= "Nuclear", Users= 9.0, DataLabelMappingName= "Nuclear: 9.0%" },
new PieChartData { Browser= "Wind", Users= 8.1, DataLabelMappingName= "Wind: 8.1%" },
new PieChartData { Browser= "Others", Users= 12.0, DataLabelMappingName= "Others: 12.0%" }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class PieChartData
{
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 pie chart. The pie chart is a circular graphic, which is ideal for displaying categories as a proportion or a percentage of the whole. The radius of the pie chart can be customized using the Radius property.
Tooltip is enabled in this example. To see the tooltip in action, hover a point or tap on a point in touch enabled devices.