Example of StackingArea in ASP.NET Core Charts Control
This Stacked Area example visualizes the amount of sales by payment mode with default 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 = "stackAreaChart",
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, IntervalType = UiChartIntervalType.Years, LabelFormat = "yyyy" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}k", Title = "Amount of sales in €", Interval = 1, Minimum = 0, Maximum = 7 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "OrganicSales", Name = "Bank Transfer", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "FairTradeSales", Name = "Credit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "VegAlternativesSales", Name = "Debit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "OtherSales", Name = "Cash", Opacity = 1 }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "stackAreaChart",
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, IntervalType = UiChartIntervalType.Years, LabelFormat = "yyyy" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}k", Title = "Amount of sales in €", Interval = 1, Minimum = 0, Maximum = 7 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "OrganicSales", Name = "Bank Transfer", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "FairTradeSales", Name = "Credit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, Width = 2, XName = "Period", YName = "VegAlternativesSales", Name = "Debit Card", Opacity = 1 },
new UiChartSeries { Type = UiChartSeriesType.StackingArea, 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/StackingArea.cshtml; it draws what ViewData holds.
public ActionResult StackingArea()
{
List<StackedAreaChartData> ChartPoints = new List<StackedAreaChartData>
{
new StackedAreaChartData { Period = new DateTime(2000, 01, 01), OrganicSales = 0.61, FairTradeSales = 0.03, VegAlternativesSales = 0.48, OtherSales = 0.23 },
new StackedAreaChartData { Period = new DateTime(2002, 01, 01), OrganicSales = 0.91, FairTradeSales = 0.06, VegAlternativesSales = 0.57, OtherSales = 0.17 },
new StackedAreaChartData { Period = new DateTime(2004, 01, 01), OrganicSales = 1.19, FairTradeSales = 0.14, VegAlternativesSales = 0.63, OtherSales = 0.23 },
new StackedAreaChartData { Period = new DateTime(2006, 01, 01), OrganicSales = 1.74, FairTradeSales = 0.29, VegAlternativesSales = 0.66, OtherSales = 0.43 },
new StackedAreaChartData { Period = new DateTime(2008, 01, 01), OrganicSales = 1.99, FairTradeSales = 0.64, VegAlternativesSales = 0.77, OtherSales = 0.72 },
new StackedAreaChartData { Period = new DateTime(2010, 01, 01), OrganicSales = 1.48, FairTradeSales = 1.06, VegAlternativesSales = 0.54, OtherSales = 1.38 },
new StackedAreaChartData { Period = new DateTime(2012, 01, 01), OrganicSales = 1.66, FairTradeSales = 1.55, VegAlternativesSales = 0.61, OtherSales = 2.16 },
new StackedAreaChartData { 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 StackedAreaChartData
{
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 stacked area chart. This chart visualizes data with y-values stacked one over another in a series order. It shows the relationship between individual values to the total sum of points.