Example of Legend Template in ASP.NET MVC Charts Control
This sample demonstrates customizable legend templates using all-time Summer Olympic medal data. It displays Gold, Silver, and Bronze medal counts for five countries — Argentina, Austria, Ethiopia, Iran, and India. Each legend entry features a medal icon rendered through a custom legend template to enhance visual clarity and data interpretation.
${series} Medals : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "legendTemplateChart",
Title = "All-Time Summer Olympic Medal Count by Country",
SubTitle = "Source: Wikipedia.org",
Data = (IEnumerable<object>)ViewData["MedalData"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#tipLegendTemplate" },
Legend = new UiChartLegend { Visible = true, Position = UiChartLegendPosition.Right, Template = "#medalLegendTemplate" },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 50, Interval = 10, Title = "Medal Count" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Gold", Name = "Gold", ColumnSpacing = 0.1 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Silver", Name = "Silver", ColumnSpacing = 0.1 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Bronze", Name = "Bronze", ColumnSpacing = 0.1 }
}
})
<template id="medalLegendTemplate">
<span style="display:inline-flex;align-items:center;gap:8px;opacity:1">
<span style="width:20px;height:20px;border-radius:50%;background:${color}"></span>
<b style="color:${color}">${name}</b>
</span>
</template>
<template id="tipLegendTemplate"><b>${x}</b><br>${series} Medals : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "legendTemplateChart",
Title = "All-Time Summer Olympic Medal Count by Country",
SubTitle = "Source: Wikipedia.org",
Data = (IEnumerable<object>)ViewData["MedalData"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#tipLegendTemplate" },
Legend = new UiChartLegend { Visible = true, Position = UiChartLegendPosition.Right, Template = "#medalLegendTemplate" },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 50, Interval = 10, Title = "Medal Count" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Gold", Name = "Gold", ColumnSpacing = 0.1 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Silver", Name = "Silver", ColumnSpacing = 0.1 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Bronze", Name = "Bronze", ColumnSpacing = 0.1 }
}
})
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/LegendTemplate.cshtml; it draws what ViewData holds.
public ActionResult LegendTemplate()
{
List<MedalData> medalData = new List<MedalData>
{
new MedalData { Country = "Argentina", Gold = 22, Silver = 27, Bronze = 31 },
new MedalData { Country = "Austria", Gold = 22, Silver = 35, Bronze = 44 },
new MedalData { Country = "Ethiopia", Gold = 24, Silver = 16, Bronze = 22 },
new MedalData { Country = "Iran", Gold = 27, Silver = 29, Bronze = 32 },
new MedalData { Country = "India", Gold = 10, Silver = 10, Bronze = 21 }
};
ViewData["MedalData"] = medalData;
return View();
}
public class MedalData
{
public string Country { get; set; }
public int Gold { get; set; }
public int Silver { get; set; }
public int Bronze { get; set; }
}
}
}
@* 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. *@
This sample shows how to render and configure a custom legend template in a Column chart. The legend items are customized using the template property of LegendSettings, which allows embedding custom HTML content such as medal images and styled text into each legend item.
The legendRender event is used to dynamically update the template content for each series — setting the medal icon image and label text per series.
Tooltip is enabled in this example. To see a tooltip in action, hover over or tap on the chart.