Example of Area in ASP.NET Core Charts Control
This Area Chart example visualizes music sales data by format in US by using a default area series in the chart.
8-TRACK
VINYL
CASSETTE
COMPACT DISC
OTHERS
DOWNLOAD
STREAMING
@Html.UiChart(new UiChartOptions
{
Id = "areaChart",
Title = "US Music Sales By Format",
Height = "440px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "In Billions (USD)", Minimum = 0, Maximum = 25, Interval = 5 },
Annotations = new List<UiChartAnnotation>
{
new UiChartAnnotation { Template = "#areaLabel1", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1976, 1, 1), Y = 1.5 },
new UiChartAnnotation { Template = "#areaLabel2", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1977, 1, 1), Y = 5 },
new UiChartAnnotation { Template = "#areaLabel3", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1990, 1, 1), Y = 3 },
new UiChartAnnotation { Template = "#areaLabel4", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2001, 1, 1), Y = 10 },
new UiChartAnnotation { Template = "#areaLabel5", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2006, 1, 1), Y = 0.5 },
new UiChartAnnotation { Template = "#areaLabel6", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2011, 6, 1), Y = 1.8 },
new UiChartAnnotation { Template = "#areaLabel7", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2015, 1, 1), Y = 1.2 }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Compact Disc", Data = (IEnumerable<object>)ViewData["Compact"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Download", Data = (IEnumerable<object>)ViewData["Download"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Streaming", Data = (IEnumerable<object>)ViewData["Streaming"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Cassette", Data = (IEnumerable<object>)ViewData["Casette"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Vinyl", Data = (IEnumerable<object>)ViewData["Vinyl"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "8-Track", Data = (IEnumerable<object>)ViewData["Track"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Others", Data = (IEnumerable<object>)ViewData["Other"] }
}
})
<template id="areaLabel1"><div style="font-weight: bold; color: white; font-size: 11px;">8-TRACK</div></template>
<template id="areaLabel2"><div style="font-weight: bold; color: white; font-size: 11px;">VINYL</div></template>
<template id="areaLabel3"><div style="font-weight: bold; color: white; font-size: 11px;">CASSETTE</div></template>
<template id="areaLabel4"><div style="font-weight: bold; color: white; font-size: 11px;">COMPACT DISC</div></template>
<template id="areaLabel5"><div style="font-weight: bold; color: white; font-size: 11px;">OTHERS</div></template>
<template id="areaLabel6"><div style="font-weight: bold; color: white; font-size: 11px;">DOWNLOAD</div></template>
<template id="areaLabel7"><div style="font-weight: bold; color: white; font-size: 11px;">STREAMING</div></template>
@Html.UiChart(new UiChartOptions
{
Id = "areaChart",
Title = "US Music Sales By Format",
Height = "440px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "In Billions (USD)", Minimum = 0, Maximum = 25, Interval = 5 },
Annotations = new List<UiChartAnnotation>
{
new UiChartAnnotation { Template = "#areaLabel1", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1976, 1, 1), Y = 1.5 },
new UiChartAnnotation { Template = "#areaLabel2", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1977, 1, 1), Y = 5 },
new UiChartAnnotation { Template = "#areaLabel3", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(1990, 1, 1), Y = 3 },
new UiChartAnnotation { Template = "#areaLabel4", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2001, 1, 1), Y = 10 },
new UiChartAnnotation { Template = "#areaLabel5", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2006, 1, 1), Y = 0.5 },
new UiChartAnnotation { Template = "#areaLabel6", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2011, 6, 1), Y = 1.8 },
new UiChartAnnotation { Template = "#areaLabel7", CoordinateUnits = UiChartAnnotationUnits.Axis, X = new DateTime(2015, 1, 1), Y = 1.2 }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Compact Disc", Data = (IEnumerable<object>)ViewData["Compact"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Download", Data = (IEnumerable<object>)ViewData["Download"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Streaming", Data = (IEnumerable<object>)ViewData["Streaming"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Cassette", Data = (IEnumerable<object>)ViewData["Casette"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Vinyl", Data = (IEnumerable<object>)ViewData["Vinyl"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "8-Track", Data = (IEnumerable<object>)ViewData["Track"] },
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Period", YName = "USD", Name = "Others", Data = (IEnumerable<object>)ViewData["Other"] }
}
})
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/AreaChart.cshtml; it draws what ViewData holds.
public ActionResult AreaChart()
{
ViewData["Compact"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(1990, 1, 1), USD = 0.69 },
new AreaChartData { Period = new DateTime(1992, 1, 1), USD = 2.86 },
new AreaChartData { Period = new DateTime(1995, 1, 1), USD = 10.2 },
new AreaChartData { Period = new DateTime(1996, 1, 1), USD = 13 },
new AreaChartData { Period = new DateTime(1997, 1, 1), USD = 14.35 },
new AreaChartData { Period = new DateTime(1998, 1, 1), USD = 15.17 },
new AreaChartData { Period = new DateTime(1999, 1, 1), USD = 14.89 },
new AreaChartData { Period = new DateTime(2000, 1, 1), USD = 18.96 },
new AreaChartData { Period = new DateTime(2001, 1, 1), USD = 18.78 },
new AreaChartData { Period = new DateTime(2004, 1, 1), USD = 14.25 },
new AreaChartData { Period = new DateTime(2005, 1, 1), USD = 14.24 },
new AreaChartData { Period = new DateTime(2006, 1, 1), USD = 12.34 },
new AreaChartData { Period = new DateTime(2007, 1, 1), USD = 9.34 },
new AreaChartData { Period = new DateTime(2008, 1, 1), USD = 4.45 },
new AreaChartData { Period = new DateTime(2010, 1, 1), USD = 1.46 },
new AreaChartData { Period = new DateTime(2011, 1, 1), USD = 0.64 }
};
ViewData["Download"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(2004, 1, 1), USD = 0.48 },
new AreaChartData { Period = new DateTime(2007, 1, 1), USD = 1.45 },
new AreaChartData { Period = new DateTime(2012, 1, 1), USD = 2.82 },
new AreaChartData { Period = new DateTime(2013, 1, 1), USD = 2.58 },
new AreaChartData { Period = new DateTime(2015, 1, 1), USD = 2.01 },
new AreaChartData { Period = new DateTime(2016, 1, 1), USD = 1.61 },
new AreaChartData { Period = new DateTime(2017, 1, 1), USD = 0.8 }
};
ViewData["Streaming"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(2011, 1, 1), USD = 0.48 },
new AreaChartData { Period = new DateTime(2013, 1, 1), USD = 1.61 },
new AreaChartData { Period = new DateTime(2015, 1, 1), USD = 2.17 },
new AreaChartData { Period = new DateTime(2017, 1, 1), USD = 7.18 }
};
ViewData["Casette"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(1976, 1, 1), USD = 0.08 },
new AreaChartData { Period = new DateTime(1979, 1, 1), USD = 1.85 },
new AreaChartData { Period = new DateTime(1980, 1, 1), USD = 2 },
new AreaChartData { Period = new DateTime(1982, 1, 1), USD = 3.55 },
new AreaChartData { Period = new DateTime(1984, 1, 1), USD = 5.4 },
new AreaChartData { Period = new DateTime(1985, 1, 1), USD = 5.24 },
new AreaChartData { Period = new DateTime(1988, 1, 1), USD = 6.94 },
new AreaChartData { Period = new DateTime(1989, 1, 1), USD = 6.85 },
new AreaChartData { Period = new DateTime(1990, 1, 1), USD = 7.02 },
new AreaChartData { Period = new DateTime(1992, 1, 1), USD = 5.81 },
new AreaChartData { Period = new DateTime(1993, 1, 1), USD = 5.32 },
new AreaChartData { Period = new DateTime(1994, 1, 1), USD = 4.03 },
new AreaChartData { Period = new DateTime(1997, 1, 1), USD = 2.25 },
new AreaChartData { Period = new DateTime(1998, 1, 1), USD = 2.01 },
new AreaChartData { Period = new DateTime(1999, 1, 1), USD = 0.8 },
new AreaChartData { Period = new DateTime(2001, 1, 1), USD = 0.4 }
};
ViewData["Vinyl"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(1973, 1, 1), USD = 7.74 },
new AreaChartData { Period = new DateTime(1974, 1, 1), USD = 7.58 },
new AreaChartData { Period = new DateTime(1976, 1, 1), USD = 8.23 },
new AreaChartData { Period = new DateTime(1977, 1, 1), USD = 9.68 },
new AreaChartData { Period = new DateTime(1978, 1, 1), USD = 10.16 },
new AreaChartData { Period = new DateTime(1979, 1, 1), USD = 8.15 },
new AreaChartData { Period = new DateTime(1981, 1, 1), USD = 6.77 },
new AreaChartData { Period = new DateTime(1982, 1, 1), USD = 5.64 },
new AreaChartData { Period = new DateTime(1984, 1, 1), USD = 4.35 },
new AreaChartData { Period = new DateTime(1985, 1, 1), USD = 2.5 },
new AreaChartData { Period = new DateTime(1989, 1, 1), USD = 0.64 },
new AreaChartData { Period = new DateTime(1990, 1, 1), USD = 0 }
};
ViewData["Track"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(1973, 1, 1), USD = 2.58 },
new AreaChartData { Period = new DateTime(1975, 1, 1), USD = 2.25 },
new AreaChartData { Period = new DateTime(1977, 1, 1), USD = 3.55 },
new AreaChartData { Period = new DateTime(1978, 1, 1), USD = 2.42 },
new AreaChartData { Period = new DateTime(1981, 1, 1), USD = -0.24 },
new AreaChartData { Period = new DateTime(1982, 1, 1), USD = 0 }
};
ViewData["Other"] = new List<AreaChartData>
{
new AreaChartData { Period = new DateTime(1988, 1, 1), USD = -0.16 },
new AreaChartData { Period = new DateTime(1989, 1, 1), USD = -0.17 },
new AreaChartData { Period = new DateTime(1990, 1, 1), USD = -0.08 },
new AreaChartData { Period = new DateTime(1992, 1, 1), USD = 0.08 },
new AreaChartData { Period = new DateTime(1996, 1, 1), USD = 0.161 },
new AreaChartData { Period = new DateTime(1998, 1, 1), USD = 0.48 },
new AreaChartData { Period = new DateTime(1999, 1, 1), USD = 1.16 },
new AreaChartData { Period = new DateTime(2001, 1, 1), USD = 0.4 },
new AreaChartData { Period = new DateTime(2002, 1, 1), USD = 0.32 },
new AreaChartData { Period = new DateTime(2003, 1, 1), USD = 0.807 },
new AreaChartData { Period = new DateTime(2005, 1, 1), USD = 1.12 },
new AreaChartData { Period = new DateTime(2006, 1, 1), USD = 1.614 },
new AreaChartData { Period = new DateTime(2008, 1, 1), USD = 1.21 },
new AreaChartData { Period = new DateTime(2009, 1, 1), USD = 1.12 },
new AreaChartData { Period = new DateTime(2011, 1, 1), USD = 0.64 },
new AreaChartData { Period = new DateTime(2013, 1, 1), USD = 0.161 },
new AreaChartData { Period = new DateTime(2018, 1, 1), USD = 0.08 }
};
return View();
}
/// <summary>The sales of one year: the rows of the Area sample.</summary>
public class AreaChartData
{
public DateTime Period { get; set; }
public double USD { 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 the area type chart can be rendered and configured. It is like the line chart, which represents time-dependent data and shows trends at equal intervals, but the area is closed and filled with the color of the series.