Example of Stacked Line in ASP.NET Core Charts Control
This Stacked Line Chart example visualizes the yearly renewable energy generation in India from 2015 to 2023.
${series}
${x} : ${y}
${x} : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "stackedLineChart",
Title = "Yearly Renewable Energy Generation in India (2015-2023)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true, Template = "#tipStackedLine" },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}TWh", Title = "Energy Generation (TWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y", Name = "Wind", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Circle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y1", Name = "Bio mass", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Diamond, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y2", Name = "Small Hydro", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 5, Height = 5, Shape = UiChartMarkerShape.Rectangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y3", Name = "Solar", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 6, Height = 6, Shape = UiChartMarkerShape.Triangle, IsFilled = true } }
}
})
<template id="tipStackedLine"><b>${series}</b><br>${x} : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "stackedLineChart",
Title = "Yearly Renewable Energy Generation in India (2015-2023)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true, Template = "#tipStackedLine" },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}TWh", Title = "Energy Generation (TWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y", Name = "Wind", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Circle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y1", Name = "Bio mass", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Diamond, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y2", Name = "Small Hydro", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 5, Height = 5, Shape = UiChartMarkerShape.Rectangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.StackingLine, Width = 2, XName = "X", YName = "Y3", Name = "Solar", Opacity = 1, Marker = new UiChartMarker { Visible = true, Width = 6, Height = 6, Shape = UiChartMarkerShape.Triangle, IsFilled = true } }
}
})
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/StackedLine.cshtml; it draws what ViewData holds.
public ActionResult StackedLine()
{
List<StackedChartData> ChartPoints = new List<StackedChartData>{
new StackedChartData { X = 2015, Y = 28.2, Y1 = 15.0, Y2 = 8.1, Y3 = 4.6 },
new StackedChartData { X = 2016, Y = 28.6, Y1 = 16.7, Y2 = 8.4, Y3 = 7.5 },
new StackedChartData { X = 2017, Y = 46.0, Y1 = 14.2, Y2 = 7.73, Y3 =12.1 },
new StackedChartData { X = 2018, Y = 52.7, Y1 = 15.3, Y2 = 5.1, Y3 = 25.9 },
new StackedChartData { X = 2019, Y = 62.0, Y1 = 16.4, Y2 = 8.7, Y3 = 39.3 },
new StackedChartData { X = 2020, Y = 64.6, Y1 = 13.9, Y2 = 9.4, Y3 = 50.1 },
new StackedChartData { X = 2021, Y = 60.1, Y1 = 14.8, Y2 = 10.3, Y3 = 60.4 },
new StackedChartData { X = 2022, Y = 68.6, Y1 = 16.1, Y2 = 10.4, Y3 = 73.5 },
new StackedChartData { X = 2023, Y = 71.81, Y1 = 16.02, Y2 = 11.17, Y3 = 102.01 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StackedChartData
{
public double X { get; set; }
public double Y { get; set; }
public double Y1 { get; set; }
public double Y2 { get; set; }
public double Y3 { 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 line chart. The stacked line 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.
Tooltip is enabled in this example. To see a tooltip in action, hover over or tap on the chart.