Example of Waterfall Chart in ASP.NET Core Charts Control
This Waterfall Chart example shows how an income turns into a net profit through the costs that are taken away from it.
@* 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 = "waterfallChart",
Title = "Company revenue and profit",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Waterfall, XName = "Item", YName = "Amount", Name = "USA", ShowDataLabel = true,
IntermediateSumIndexes = new List<int> { 4 }, SumIndexes = new List<int> { 7 }
}
}
})
@Html.UiChart(new UiChartOptions
{
Id = "waterfallChart",
Title = "Company revenue and profit",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Waterfall, XName = "Item", YName = "Amount", Name = "USA", ShowDataLabel = true,
IntermediateSumIndexes = new List<int> { 4 }, SumIndexes = new List<int> { 7 }
}
}
})
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/WaterfallChart.cshtml; it draws what ViewData holds.
public ActionResult WaterfallChart()
{
ViewData["ChartData"] = CashFlows();
return View();
}
/// <summary>One line of a cash flow: the rows of the Waterfall sample (a row without an amount is a total).</summary>
public class CashFlowData
{
public string Item { get; set; }
public double Amount { get; set; }
}
private static List<CashFlowData> CashFlows()
{
return new List<CashFlowData>
{
new CashFlowData { Item = "Income", Amount = 4711 },
new CashFlowData { Item = "Sales", Amount = -1015 },
new CashFlowData { Item = "Development", Amount = -688 },
new CashFlowData { Item = "Revenue", Amount = 1030 },
new CashFlowData { Item = "Balance" },
new CashFlowData { Item = "Expense", Amount = -361 },
new CashFlowData { Item = "Tax", Amount = -695 },
new CashFlowData { Item = "Net Profit" }
};
}
}
}
@* 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. *@
Each column starts where the previous ended. IntermediateSumIndexes = { 4 } (Balance) and SumIndexes = { 7 } (Net Profit) show totals.