Example of DataLabel Template in ASP.NET Core Charts Control
This sample illustrates data label template support in charts. Each data label for each point is rendered using the template.
${y}
${series}
${series}
${y}
${series}
${series}
@Html.UiChart(new UiChartOptions
{
Id = "labelTemplateChart",
Title = "Athletes in Popular School",
Data = (IEnumerable<object>)ViewData["PopulationDetails"],
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 70 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Sports", YName = "Boys", Name = "Boys", ColumnWidth = 0.75, ColumnSpacing = 0.5, ShowDataLabel = true, DataLabelTemplate = "#boysLabelTemplate", Marker = new UiChartMarker { Width = 10, Height = 10, Shape = UiChartMarkerShape.Circle } },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Sports", YName = "Girls", Name = "Girls", ColumnWidth = 0.75, ColumnSpacing = 0.5, ShowDataLabel = true, DataLabelTemplate = "#girlsLabelTemplate", Marker = new UiChartMarker { Width = 10, Height = 10, Shape = UiChartMarkerShape.Rectangle } }
}
})
<template id="boysLabelTemplate">
<div style="text-align:center;color:#0076e5"><b>${y}</b><br><span style="font-size:11px">${series}</span></div>
</template>
<template id="girlsLabelTemplate">
<div style="text-align:center;color:#e5468a"><b>${y}</b><br><span style="font-size:11px">${series}</span></div>
</template>
@Html.UiChart(new UiChartOptions
{
Id = "labelTemplateChart",
Title = "Athletes in Popular School",
Data = (IEnumerable<object>)ViewData["PopulationDetails"],
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 70 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Sports", YName = "Boys", Name = "Boys", ColumnWidth = 0.75, ColumnSpacing = 0.5, ShowDataLabel = true, DataLabelTemplate = "#boysLabelTemplate", Marker = new UiChartMarker { Width = 10, Height = 10, Shape = UiChartMarkerShape.Circle } },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Sports", YName = "Girls", Name = "Girls", ColumnWidth = 0.75, ColumnSpacing = 0.5, ShowDataLabel = true, DataLabelTemplate = "#girlsLabelTemplate", Marker = new UiChartMarker { Width = 10, Height = 10, Shape = UiChartMarkerShape.Rectangle } }
}
})
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/DataLabelTemplate.cshtml; it draws what ViewData holds.
public ActionResult DataLabelTemplate()
{
List<TemplateData> PopulationDetails = new List<TemplateData>
{
new TemplateData { Sports= "Tennis", Boys= 50, Girls= 38 },
new TemplateData { Sports= "Badminton", Boys= 30, Girls= 40 },
new TemplateData { Sports= "Cycling", Boys= 37, Girls= 20 },
new TemplateData { Sports= "Football", Boys= 60, Girls= 21 },
new TemplateData { Sports= "Hockey", Boys= 15, Girls= 8 }
};
ViewData["PopulationDetails"] = PopulationDetails;
ViewData["margin"] = new { Right = 30 };
ViewData["margin1"] = new { Right = 15 };
return View();
}
public class TemplateData
{
public string Sports { get; set; }
public double Boys { get; set; }
public double Girls { 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. *@
The Charts data label template feature allows you to change the appearance and behavior of the data label for each data point using the Template property in ChartDataLabel.