Example of Pie with Legend Template in ASP.NET Core Charts Control
This sample demonstrates custom legend templates in a pie chart visualizing the world's top 5 oil-producing countries — United States, Saudi Arabia, Russia, Canada, and China. Each pie segment reflects the percentage share of production, and the legend displays country flags and daily output in millions of barrels.
${name}${value} (${percent})
${x}
Production: ${tooltip} barrels/day
Production: ${tooltip} barrels/day
@Html.UiChart(new UiChartOptions
{
Id = "pieLegendTemplateChart",
Data = (IEnumerable<object>)ViewData["PieChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#tipPieLegendTemplate" },
Legend = new UiChartLegend { Visible = true, Position = UiChartLegendPosition.Right, Template = "#pieLegendMarkup" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "X", YName = "Y", InnerRadius = "0%", ShowDataLabel = true, DataLabelTemplate = "${Text}" }
}
})
<template id="pieLegendMarkup">
<span style="display:inline-flex;align-items:center;gap:8px">
<span style="width:12px;height:12px;border-radius:3px;background:${color}"></span>
<span>${name}</span><span style="opacity:.7">${value} (${percent})</span>
</span>
</template>
<template id="tipPieLegendTemplate"><b>${x}</b><br>Production: <b>${tooltip}</b> barrels/day</template>
@Html.UiChart(new UiChartOptions
{
Id = "pieLegendTemplateChart",
Data = (IEnumerable<object>)ViewData["PieChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#tipPieLegendTemplate" },
Legend = new UiChartLegend { Visible = true, Position = UiChartLegendPosition.Right, Template = "#pieLegendMarkup" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "X", YName = "Y", InnerRadius = "0%", ShowDataLabel = true, DataLabelTemplate = "${Text}" }
}
})
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/PieLegendTemplate.cshtml; it draws what ViewData holds.
public ActionResult PieLegendTemplate()
{
var pieChartPoints = new List<PieLegendTemplateData>
{
new PieLegendTemplateData { X = "United States", Y = 29.55, Text = "United States: 29.55%", Description = "13.4M barrels per day", Tooltip = "13.4M" },
new PieLegendTemplateData { X = "Saudi Arabia", Y = 23.83, Text = "Saudi Arabia: 23.83%", Description = "10.8M barrels per day", Tooltip = "10.8M" },
new PieLegendTemplateData { X = "Russia", Y = 23.69, Text = "Russia: 23.69%", Description = "10.8M barrels per day", Tooltip = "10.8M" },
new PieLegendTemplateData { X = "Canada", Y = 12.12, Text = "Canada: 12.12%", Description = "5.5M barrels per day", Tooltip = "5.5M" },
new PieLegendTemplateData { X = "China", Y = 10.83, Text = "China: 10.83%", Description = "4.9M barrels per day", Tooltip = "4.9M" }
};
ViewBag.TitleText = "Top 5 Oil Producing Countries (2023)";
ViewBag.SubTitle = "Source: Wikipedia.org";
ViewData["PieChartPoints"] = pieChartPoints;
return View();
}
public class PieLegendTemplateData
{
public string X { get; set; }
public double Y { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public string Tooltip { 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. *@
This sample uses the template property of LegendSettings to fully customize legend items. The legendRender event dynamically injects country-specific data — including flag images, color-matched labels, and production volumes — into the legend template at render time.