Example of Empty Points in ASP.NET Core Charts Control
This Pie Chart example shows what the chart does with rows that have no value: a gap, a zero or the average of the others.
@Html.UiChart(new UiChartOptions
{
Id = "pieEmpty" + mode,
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
Title = "EmptyPointMode = " + mode,
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Channel", YName = "Sales", Name = "Sales", EmptyPointMode = mode, ShowDataLabel = true }
}
})
<div class="row g-3">
@foreach (var mode in new[] { UiChartEmptyPointMode.Gap, UiChartEmptyPointMode.Zero, UiChartEmptyPointMode.Average })
{
<div class="col-lg-4">
@Html.UiChart(new UiChartOptions
{
Id = "pieEmpty" + mode,
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
Title = "EmptyPointMode = " + mode,
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "Channel", YName = "Sales", Name = "Sales", EmptyPointMode = mode, ShowDataLabel = true }
}
})
</div>
}
</div>
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/PieEmptyPoints.cshtml; it draws what ViewData holds.
public ActionResult PieEmptyPoints()
{
ViewData["ChartData"] = new[]
{
new { Channel = "Online", Sales = (double?)420 }, new { Channel = "Retail", Sales = (double?)null },
new { Channel = "Wholesale", Sales = (double?)310 }, new { Channel = "Partners", Sales = (double?)null },
new { Channel = "Export", Sales = (double?)150 }
};
return View();
}
}
}
@* 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. *@
Two of the five rows have no value. EmptyPointMode decides what the pie does with them: Gap draws no slice, Zero keeps the legend entry with a slice of nothing, Average gives the slice the average of the others.