Example of Smart Labels in ASP.NET Core Charts Control
This sample shows the gold medal count scored by each country at the summer olympics 2024 games using smart labels on the chart.
${x}
Gold Medals: ${y}
Gold Medals: ${y}
@Html.UiChart(new UiChartOptions
{
Id = "smartLabelsChart",
Title = "Summer Olympics 2024 - Gold Medals",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipPieSmartLabels" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Country", YName = "Medal", StartAngle = 60, ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside }
}
})
<template id="tipPieSmartLabels"><b>${x}</b><br> Gold Medals: <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "smartLabelsChart",
Title = "Summer Olympics 2024 - Gold Medals",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipPieSmartLabels" },
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Country", YName = "Medal", StartAngle = 60, ShowDataLabel = true, DataLabelTemplate = "${DataLabelMappingName}", DataLabelPosition = UiChartLabelPosition.Outside }
}
})
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/PieSmartLabels.cshtml; it draws what ViewData holds.
public ActionResult PieSmartLabels()
{
List<SmartLabelsChartData> ChartPoints = new List<SmartLabelsChartData>
{
new SmartLabelsChartData { Country = "USA", Medal = 40, DataLabelMappingName = "United States of America: 40" },
new SmartLabelsChartData { Country = "China", Medal = 40, DataLabelMappingName = "China: 40" },
new SmartLabelsChartData { Country = "Japan", Medal = 20, DataLabelMappingName = "Japan: 20" },
new SmartLabelsChartData { Country = "Australia", Medal = 18, DataLabelMappingName = "Australia: 18" },
new SmartLabelsChartData { Country = "France", Medal = 16, DataLabelMappingName = "France: 16" },
new SmartLabelsChartData { Country = "Netherlands", Medal = 15, DataLabelMappingName = "Netherlands: 15" },
new SmartLabelsChartData { Country = "Great Britain", Medal = 14, DataLabelMappingName = "Great Britain: 14" },
new SmartLabelsChartData { Country = "South Korea", Medal = 13, DataLabelMappingName = "South Korea: 13" },
new SmartLabelsChartData { Country = "Germany", Medal = 12, DataLabelMappingName = "Germany: 12" },
new SmartLabelsChartData { Country = "Italy", Medal = 12, DataLabelMappingName = "Italy: 12" },
new SmartLabelsChartData { Country = "NewZealand", Medal = 10, DataLabelMappingName = "New Zealand: 10" },
new SmartLabelsChartData { Country = "Canada", Medal = 9, DataLabelMappingName = "Canada: 9" },
new SmartLabelsChartData { Country = "Uzbekistan", Medal = 8, DataLabelMappingName = "Uzbekistan: 8" },
new SmartLabelsChartData { Country = "Hungary", Medal = 6, DataLabelMappingName = "Hungary: 6" },
new SmartLabelsChartData { Country = "Kenya", Medal = 4, DataLabelMappingName = "Kenya: 4" },
new SmartLabelsChartData { Country = "Georgia", Medal = 3, DataLabelMappingName = "Georgia: 3" },
new SmartLabelsChartData { Country = "North Korea", Medal = 2, DataLabelMappingName = "North Korea: 2" },
new SmartLabelsChartData { Country = "South Africa", Medal = 2, DataLabelMappingName = "South Africa: 2" }
};;
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class SmartLabelsChartData
{
public string Country { get; set; }
public double Medal { 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 the labels can be arranged smartly without overlapping. You can use the EnableSmartLabels property to enable or disable the support.
Tooltip is enabled in this example. To see the tooltip in action, hover a point or tap on a point in touch enabled devices.