Example of 100% Stacked Bar in ASP.NET MVC Charts Control
This 100% Stacked Bar Chart example visualizes the comparison of renewable energy generation in China from 2020 to 2023, segmented by wind, solar, and hydro sources, using a default 100% stacked bar series. The legend provides additional information about the series.
Renewable Energy Generation
@Html.UiChart(new UiChartOptions
{
Id = "stackedBar100Chart",
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 = "#tipStackedBar100" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y1", Name = "Wind", ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y2", Name = "Solar", ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y3", Name = "Hydro", CornerRadius = 4, ColumnWidth = 0.6 }
}
})
<template id="tipStackedBar100"><b>Renewable Energy Generation</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "stackedBar100Chart",
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 = "#tipStackedBar100" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y1", Name = "Wind", ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y2", Name = "Solar", ColumnWidth = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StackingBar100, Width = 2, XName = "X", YName = "Y3", Name = "Hydro", 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/StackedBar100.cshtml; it draws what ViewData holds.
public ActionResult StackedBar100()
{
List<StackedBar100ChartData> ChartPoints = new List<StackedBar100ChartData>
{
new StackedBar100ChartData { X = "2020", Y1 = 466, Y2 = 261, Y3 = 1355 },
new StackedBar100ChartData { X = "2021", Y1 = 656, Y2 = 327, Y3 = 1340 },
new StackedBar100ChartData { X = "2022", Y1 = 763, Y2 = 427, Y3 = 1352 },
new StackedBar100ChartData { X = "2023", Y1 = 886, Y2 = 584, Y3 = 1286 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StackedBar100ChartData
{
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 100% stacked bar chart. The 100% stacked bar chart displays multiple series of data as stacked bars, ensuring that the cumulative value 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.