Example of Step Area in ASP.NET MVC Charts Control
This Step Area Chart example visualizes electricity generation data using renewable and non-renewable resources in a step area chart.
@* 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 = "stepAreaChart",
Title = "Electricity - Production",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}B", Title = "Production (Billion as kWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StepArea, XName = "Period", YName = "RenewableResourcesProduction", Name = "Renewable", Opacity = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StepArea, Width = 2, XName = "Period", YName = "NonRenewableResourcesProduction", Name = "Non-Renewable", Opacity = 0.6 }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "stepAreaChart",
Title = "Electricity - Production",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true },
Legend = new UiChartLegend { EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}B", Title = "Production (Billion as kWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StepArea, XName = "Period", YName = "RenewableResourcesProduction", Name = "Renewable", Opacity = 0.6 },
new UiChartSeries { Type = UiChartSeriesType.StepArea, Width = 2, XName = "Period", YName = "NonRenewableResourcesProduction", Name = "Non-Renewable", Opacity = 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/StepArea.cshtml; it draws what ViewData holds.
public ActionResult StepArea()
{
List<StepAreaChartData> ChartPoints = new List<StepAreaChartData>
{
new StepAreaChartData { Period = 2000, RenewableResourcesProduction = 416, NonRenewableResourcesProduction = 180 },
new StepAreaChartData { Period = 2001, RenewableResourcesProduction = 490, NonRenewableResourcesProduction = 240 },
new StepAreaChartData { Period = 2002, RenewableResourcesProduction = 470, NonRenewableResourcesProduction = 370 },
new StepAreaChartData { Period = 2003, RenewableResourcesProduction = 500, NonRenewableResourcesProduction = 200 },
new StepAreaChartData { Period = 2004, RenewableResourcesProduction = 449, NonRenewableResourcesProduction = 229 },
new StepAreaChartData { Period = 2005, RenewableResourcesProduction = 470, NonRenewableResourcesProduction = 210 },
new StepAreaChartData { Period = 2006, RenewableResourcesProduction = 437, NonRenewableResourcesProduction = 337 },
new StepAreaChartData { Period = 2007, RenewableResourcesProduction = 458, NonRenewableResourcesProduction = 258 },
new StepAreaChartData { Period = 2008, RenewableResourcesProduction = 500, NonRenewableResourcesProduction = 300 },
new StepAreaChartData { Period = 2009, RenewableResourcesProduction = 473, NonRenewableResourcesProduction = 173 },
new StepAreaChartData { Period = 2010, RenewableResourcesProduction = 520, NonRenewableResourcesProduction = 220 },
new StepAreaChartData { Period = 2011, RenewableResourcesProduction = 520, NonRenewableResourcesProduction = 220 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class StepAreaChartData
{
public double Period { get; set; }
public double RenewableResourcesProduction { get; set; }
public double NonRenewableResourcesProduction { 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 a step area chart. This series forms a step progress by connecting points through vertical and horizontal lines with the area being filled.
Tooltip is enabled in this example. To see the tooltip in action, hover over or tap on the chart.