Example of 100% StackingColumn in ASP.NET Core Charts Control
This 100% Stacked Column Chart example visualizes motor vehicle production by manufacturer with a default stacked column series. The legend in the sample shows the information about those series.
${x}
@Html.UiChart(new UiChartOptions
{
Id = "percentChart",
SubTitle = "Source: fas.usda.gov",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackingColumn100" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, Interval = 1, LabelIntersectAction = UiChartLabelIntersectAction.Rotate45 },
PrimaryYAxis = new UiChartAxis { Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y1", Name = "India", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y2", Name = "China", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y3", Name = "United States", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y4", Name = "Brazil", ColumnWidth = 0.4, CornerRadius = 4 }
}
})
<template id="tipStackingColumn100"><b>${x}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "percentChart",
SubTitle = "Source: fas.usda.gov",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackingColumn100" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, Interval = 1, LabelIntersectAction = UiChartLabelIntersectAction.Rotate45 },
PrimaryYAxis = new UiChartAxis { Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y1", Name = "India", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y2", Name = "China", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y3", Name = "United States", ColumnWidth = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn100, XName = "X", YName = "Y4", Name = "Brazil", ColumnWidth = 0.4, CornerRadius = 4 }
}
})
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/StackingColumn100.cshtml; it draws what ViewData holds.
public ActionResult StackingColumn100()
{
List<StackedColumnChartData100> ChartPoints = new List<StackedColumnChartData100>
{
new StackedColumnChartData100 { X = "2019", Y1 = 28500000, Y2 = 26900000, Y3 = 19900000, Y4 = 13000000 },
new StackedColumnChartData100 { X = "2020", Y1 = 27500000, Y2 = 29300000, Y3 = 14600000, Y4 = 13800000 },
new StackedColumnChartData100 { X = "2021", Y1 = 24300000, Y2 = 26700000, Y3 = 17500000, Y4 = 10800000 },
new StackedColumnChartData100 { X = "2022", Y1 = 26300000, Y2 = 30800000, Y3 = 14500000, Y4 = 11700000 },
new StackedColumnChartData100 { X = "2023", Y1 = 25400000, Y2 = 27400000, Y3 = 12100000, Y4 = 14600000 },
new StackedColumnChartData100 { X = "2024", Y1 = 25000000, Y2 = 31000000, Y3 = 14400000, Y4 = 17000000 },
};;
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StackedColumnChartData100
{
public string X { get; set; }
public double Y1 { get; set; }
public double Y2 { get; set; }
public double Y3 { get; set; }
public double Y4 { 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 100% stacked column chart. The 100% stacked column chart displays multiple series of data as stacked columns, ensuring that the cumulative proportion of each stacked element always totals 100%.
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.