Example of No Data Template in ASP.NET MVC Charts Control
This example shows your own markup when there is nothing to draw, instead of the plain text.
📊
No sales yet
Orders will appear here once the first one is placed.
@Html.UiChart(new UiChartOptions
{
Id = "noDataTemplateChart",
Title = "With a template",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
EmptyTemplate = "#noSalesTemplate",
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Amount", Name = "Amount" } }
})
@Html.UiChart(new UiChartOptions
{
Id = "noDataTextChart",
Title = "With EmptyText only",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
EmptyText = "Nothing to show",
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Amount", Name = "Amount" } }
})
@using System.Linq
<template id="noSalesTemplate">
<div style="text-align:center">
<div style="font-size:40px;line-height:1">📊</div>
<div style="font-weight:600;margin:6px 0">No sales yet</div>
<div style="font-size:12px;opacity:.8">Orders will appear here once the first one is placed.</div>
</div>
</template>
<div class="row g-3">
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "noDataTemplateChart",
Title = "With a template",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
EmptyTemplate = "#noSalesTemplate",
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Amount", Name = "Amount" } }
})
</div>
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "noDataTextChart",
Title = "With EmptyText only",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "320px",
EmptyText = "Nothing to show",
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Amount", Name = "Amount" } }
})
</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/NoDataTemplate.cshtml; it draws what ViewData holds.
public ActionResult NoDataTemplate()
{
ViewData["ChartData"] = Enumerable.Empty<object>().ToList();
return View();
}
}
}
@* 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. *@
When there is nothing to draw, EmptyTemplate shows markup from the page instead of the plain EmptyText.