Example of 100% Stacked Area in ASP.NET Core Charts Control
This 100% Stacked Area Chart example visualizes the amount of sales by payment mode with default 100% stacked area series. A legend in the sample shows information about the series.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "stackingArea100Chart",
Title = "Amount of Sales by Payment Mode",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", MinimumDate = new DateTime(1999, 1, 1), MaximumDate = new DateTime(2015, 01, 1), IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "OrganicSales", Name = "Bank Transfer", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "FairTradeSales", Name = "Credit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "VegAlternativesSales", Name = "Debit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "OtherSales", Name = "Cash", Opacity = 1 }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "stackingArea100Chart",
Title = "Amount of Sales by Payment Mode",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", MinimumDate = new DateTime(1999, 1, 1), MaximumDate = new DateTime(2015, 01, 1), IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Interval = 20 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "OrganicSales", Name = "Bank Transfer", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "FairTradeSales", Name = "Credit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "VegAlternativesSales", Name = "Debit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea100, Width = 2, XName = "Period", YName = "OtherSales", Name = "Cash", Opacity = 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/StackingArea100.cshtml; it draws what ViewData holds.
public ActionResult StackingArea100()
{
List<StackedArea100ChartData> ChartPoints = new List<StackedArea100ChartData>
{
new StackedArea100ChartData { Period = new DateTime(2000, 01, 01), OrganicSales = 0.61, FairTradeSales = 0.03, VegAlternativesSales = 0.48, OtherSales = 0.23 },
new StackedArea100ChartData { Period = new DateTime(2001, 01, 01), OrganicSales = 0.81, FairTradeSales = 0.05, VegAlternativesSales = 0.53, OtherSales = 0.17 },
new StackedArea100ChartData { Period = new DateTime(2002, 01, 01), OrganicSales = 0.91, FairTradeSales = 0.06, VegAlternativesSales = 0.57, OtherSales = 0.17 },
new StackedArea100ChartData { Period = new DateTime(2003, 01, 01), OrganicSales = 1.00, FairTradeSales = 0.09, VegAlternativesSales = 0.61, OtherSales = 0.20 },
new StackedArea100ChartData { Period = new DateTime(2004, 01, 01), OrganicSales = 1.19, FairTradeSales = 0.14, VegAlternativesSales = 0.63, OtherSales = 0.23 },
new StackedArea100ChartData { Period = new DateTime(2005, 01, 01), OrganicSales = 1.47, FairTradeSales = 0.20, VegAlternativesSales = 0.64, OtherSales = 0.36 },
new StackedArea100ChartData { Period = new DateTime(2006, 01, 01), OrganicSales = 1.74, FairTradeSales = 0.29, VegAlternativesSales = 0.66, OtherSales = 0.43 },
new StackedArea100ChartData { Period = new DateTime(2007, 01, 01), OrganicSales = 1.98, FairTradeSales = 0.46, VegAlternativesSales = 0.76, OtherSales = 0.51 },
new StackedArea100ChartData { Period = new DateTime(2008, 01, 01), OrganicSales = 1.99, FairTradeSales = 0.64, VegAlternativesSales = 0.77, OtherSales = 0.72 },
new StackedArea100ChartData { Period = new DateTime(2009, 01, 01), OrganicSales = 1.70, FairTradeSales = 0.75, VegAlternativesSales = 0.55, OtherSales = 1.29 },
new StackedArea100ChartData { Period = new DateTime(2010, 01, 01), OrganicSales = 1.48, FairTradeSales = 1.06, VegAlternativesSales = 0.54, OtherSales = 1.38 },
new StackedArea100ChartData { Period = new DateTime(2011, 01, 01), OrganicSales = 1.38, FairTradeSales = 1.25, VegAlternativesSales = 0.57, OtherSales = 1.82 },
new StackedArea100ChartData { Period = new DateTime(2012, 01, 01), OrganicSales = 1.66, FairTradeSales = 1.55, VegAlternativesSales = 0.61, OtherSales = 2.16 },
new StackedArea100ChartData { Period = new DateTime(2013, 01, 01), OrganicSales = 1.66, FairTradeSales = 1.55, VegAlternativesSales = 0.67, OtherSales = 2.51 },
new StackedArea100ChartData { Period = new DateTime(2014, 01, 01), OrganicSales = 1.67, FairTradeSales = 1.65, VegAlternativesSales = 0.67, OtherSales = 2.61 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StackedArea100ChartData
{
public DateTime Period { get; set; }
public double OrganicSales { get; set; }
public double FairTradeSales { get; set; }
public double VegAlternativesSales { get; set; }
public double OtherSales { 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 area chart. This chart visualizes data with y-values stacked, ensuring that the cumulative proportion of each stacked element always totals 100%.