Example of DateTime Category Axis in ASP.NET Core Charts Control
This example puts dates one after the other with no gap for weekends, and shows the same prices on a date axis with the gaps.
@Html.UiChart(new UiChartOptions
{
Id = "dateGapChart",
Title = "DateTime axis",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "dd MMM", Interval = 3 },
PrimaryYAxis = new UiChartAxis { Minimum = 90 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Date", YName = "Close", Name = "Close", ShowMarker = true } }
})
@Html.UiChart(new UiChartOptions
{
Id = "dateCategoryChart",
Title = "DateTimeCategory axis",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTimeCategory, LabelFormat = "dd MMM" },
PrimaryYAxis = new UiChartAxis { Minimum = 90 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Date", YName = "Close", Name = "Close", ShowMarker = true } }
})
@using System.Linq
<div class="row g-3">
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "dateGapChart",
Title = "DateTime axis",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "dd MMM", Interval = 3 },
PrimaryYAxis = new UiChartAxis { Minimum = 90 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Date", YName = "Close", Name = "Close", ShowMarker = true } }
})
</div>
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "dateCategoryChart",
Title = "DateTimeCategory axis",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTimeCategory, LabelFormat = "dd MMM" },
PrimaryYAxis = new UiChartAxis { Minimum = 90 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Date", YName = "Close", Name = "Close", ShowMarker = 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/DateTimeCategoryAxis.cshtml; it draws what ViewData holds.
public ActionResult DateTimeCategoryAxis()
{
var days = Enumerable.Range(0, 28).Select(i => new DateTime(2026, 3, 2).AddDays(i)).Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday).ToList();
var prices = days.Select((d, i) => new { Date = d, Close = Math.Round(100 + i * 0.8 + 6 * Math.Sin(i / 2.5), 2) }).ToList();
ViewData["ChartData"] = prices;
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. *@
Prices exist on weekdays only. A DateTime axis leaves a gap for every weekend (left); DateTimeCategory puts the dates one after the other, one place for each date that has data (right).