Example of Smart Axis Labels in ASP.NET Core Charts Control
This example shows what the X axis does with labels that would overlap: hide, rotate, wrap, trim or write them in rows.
@Html.UiChart(new UiChartOptions
{
Id = "smartLabels" + action,
Title = action.ToString(),
Data = (action == UiChartLabelIntersectAction.MultipleRows ? (IEnumerable<object>)ViewData["ShortNames"] : (IEnumerable<object>)ViewData["ChartData"]),
Height = "300px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { LabelIntersectAction = action },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Department", YName = "Staff", Name = "Staff" } }
})
@using System.Linq
@{
var actions = new[] { UiChartLabelIntersectAction.Hide, UiChartLabelIntersectAction.Rotate45, UiChartLabelIntersectAction.Rotate90, UiChartLabelIntersectAction.Wrap, UiChartLabelIntersectAction.Trim, UiChartLabelIntersectAction.MultipleRows };
}
<div class="row g-3">
@foreach (var action in actions)
{
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "smartLabels" + action,
Title = action.ToString(),
Data = (action == UiChartLabelIntersectAction.MultipleRows ? (IEnumerable<object>)ViewData["ShortNames"] : (IEnumerable<object>)ViewData["ChartData"]),
Height = "300px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { LabelIntersectAction = action },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Department", YName = "Staff", Name = "Staff" } }
})
</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/SmartAxisLabels.cshtml; it draws what ViewData holds.
public ActionResult SmartAxisLabels()
{
var departments = new[] { "Human Resources", "Information Technology", "Research and Development", "Customer Support", "Marketing and Communications", "Legal and Compliance", "Finance and Accounting", "Facilities Management" }
.Select((d, i) => new { Department = d, Staff = 12 + (i * 7) % 23 }).ToList();
ViewData["ChartData"] = departments.ToList<object>();
ViewData["ShortNames"] = departments.Select(x => new { Department = x.Department.Split(' ')[0], x.Staff }).ToList<object>();
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. *@
LabelIntersectAction chooses what the X axis does with labels that would overlap: Hide, Rotate45, Rotate90, Wrap, Trim, MultipleRows or None.