Example of StackingColumn and Data Labels in ASP.NET Core Charts Control
This Stacked ColumnChart example visualizes global cotton production trends over the years, with data points enhanced by data labels.
${x}
${series} : ${y}
${series} : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "stackColumnChart",
Title = "Global Cotton Production by Country (2019-2024)",
SubTitle = "Source: fas.usda.gov",
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackingColumn" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, Interval = 1, LabelIntersectAction = UiChartLabelIntersectAction.Rotate45 },
PrimaryYAxis = new UiChartAxis { Title = "Production (60KG Bags)", LabelFormat = "{value}M", Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "India", ColumnWidth = 0.4, ShowDataLabel = true, Data = (IEnumerable<object>)ViewData["ChartPoints"] },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "China", ColumnWidth = 0.4, ShowDataLabel = true, Data = (IEnumerable<object>)ViewData["ChartPoints1"] },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "United States", ColumnWidth = 0.4, ShowDataLabel = true, CornerRadius = 4, Data = (IEnumerable<object>)ViewData["ChartPoints2"] }
}
})
<template id="tipStackingColumn"><b>${x}</b><br>${series} : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "stackColumnChart",
Title = "Global Cotton Production by Country (2019-2024)",
SubTitle = "Source: fas.usda.gov",
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackingColumn" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, Interval = 1, LabelIntersectAction = UiChartLabelIntersectAction.Rotate45 },
PrimaryYAxis = new UiChartAxis { Title = "Production (60KG Bags)", LabelFormat = "{value}M", Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "India", ColumnWidth = 0.4, ShowDataLabel = true, Data = (IEnumerable<object>)ViewData["ChartPoints"] },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "China", ColumnWidth = 0.4, ShowDataLabel = true, Data = (IEnumerable<object>)ViewData["ChartPoints1"] },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "X", YName = "Y", Name = "United States", ColumnWidth = 0.4, ShowDataLabel = true, CornerRadius = 4, Data = (IEnumerable<object>)ViewData["ChartPoints2"] }
}
})
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/StackingColumn.cshtml; it draws what ViewData holds.
public ActionResult StackingColumn()
{
List<StackedColumnChartData> ChartPoints = new List<StackedColumnChartData>
{
new StackedColumnChartData { X = "2019", Y = 28.5 },
new StackedColumnChartData { X = "2020", Y = 27.5 },
new StackedColumnChartData { X = "2021", Y = 24.3 },
new StackedColumnChartData { X = "2022", Y = 26.3 },
new StackedColumnChartData { X = "2023", Y = 25.4 },
new StackedColumnChartData { X = "2024", Y = 25 }
};
List<StackedColumnChartData> ChartPoints1 = new List<StackedColumnChartData>
{
new StackedColumnChartData { X = "2019", Y = 26.9 },
new StackedColumnChartData { X = "2020", Y = 29.3 },
new StackedColumnChartData { X = "2021", Y = 26.7 },
new StackedColumnChartData { X = "2022", Y = 30.8 },
new StackedColumnChartData { X = "2023", Y = 27.4 },
new StackedColumnChartData { X = "2024", Y = 31 }
};
List<StackedColumnChartData> ChartPoints2 = new List<StackedColumnChartData>
{
new StackedColumnChartData { X = "2019", Y = 19.9 },
new StackedColumnChartData { X = "2020", Y = 14.6 },
new StackedColumnChartData { X = "2021", Y = 17.5 },
new StackedColumnChartData { X = "2022", Y = 14.5 },
new StackedColumnChartData { X = "2023", Y = 12.1 },
new StackedColumnChartData { X = "2024", Y = 14.4 }
};;
ViewData["ChartPoints"] = ChartPoints;
ViewData["ChartPoints1"] = ChartPoints1;
ViewData["ChartPoints2"] = ChartPoints2;
return View();
}
public class StackedColumnChartData
{
public string X { get; set; }
public double Y { 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 the stacked column chart. The stacked column chart stacks points in the series vertically. You can also use the StackingGroup property to group stacked collections based on category. This chart displays data labels for individual points and the total value on top of each stack.
Tooltip is enabled in this example. To see the tooltip in action, hover over a point or tap on a point in touch-enabled devices.