Example of Stacked Bar in ASP.NET MVC Charts Control
This Stacked Bar Chart example stacks the four quarters of each region on one horizontal bar.
Renewable Energy Generation
${series} : ${y}
${series} : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "stackedBarChart",
Title = "Annual Renewable Energy Generation in China (2020-2023) by Source",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackedBar" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}TWh", Maximum = 3500 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y1", Name = "Wind", ShowDataLabel = true, ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y2", Name = "Solar", ShowDataLabel = true, ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y3", Name = "Hydro", ShowDataLabel = true, CornerRadius = 4, ColumnWidth = 0.6 }
}
})
<template id="tipStackedBar"><b>Renewable Energy Generation</b><br>${series} : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "stackedBarChart",
Title = "Annual Renewable Energy Generation in China (2020-2023) by Source",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipStackedBar" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}TWh", Maximum = 3500 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y1", Name = "Wind", ShowDataLabel = true, ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y2", Name = "Solar", ShowDataLabel = true, ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar, Width = 2, XName = "X", YName = "Y3", Name = "Hydro", ShowDataLabel = true, CornerRadius = 4, ColumnWidth = 0.6 }
}
})
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/StackedBar.cshtml; it draws what ViewData holds.
public ActionResult StackedBar()
{
List<StackedBarChartData> ChartPoints = new List<StackedBarChartData>
{
new StackedBarChartData { X = "2020", Y1 = 466, Y2 = 261, Y3 = 1355 },
new StackedBarChartData { X = "2021", Y1 = 656, Y2 = 327, Y3 = 1340 },
new StackedBarChartData { X = "2022", Y1 = 763, Y2 = 427, Y3 = 1352 },
new StackedBarChartData { X = "2023", Y1 = 886, Y2 = 584, Y3 = 1286 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StackedBarChartData
{
public string X { get; set; }
public double Y1 { get; set; }
public double Y2 { get; set; }
public double Y3 { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("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 bar chart. The stacked bar chart stacks points in the series horizontally. You can also use the StackingGroup property to group stacked collections based on category.
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.