Example of MultiLevel Labels in ASP.NET MVC Charts Control
This example names groups of categories under the X axis: the months by quarter, and the quarters by half year.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "multiLevelChart",
Title = "Online sales",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "400px",
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis
{
MultiLevelLabels = new List<UiChartMultiLevelLabel>
{
new UiChartMultiLevelLabel { Start = "Jan", End = "Mar", Text = "Q1", Level = 0 },
new UiChartMultiLevelLabel { Start = "Apr", End = "Jun", Text = "Q2", Level = 0 },
new UiChartMultiLevelLabel { Start = "Jul", End = "Sep", Text = "Q3", Level = 0 },
new UiChartMultiLevelLabel { Start = "Oct", End = "Dec", Text = "Q4", Level = 0 },
new UiChartMultiLevelLabel { Start = "Jan", End = "Jun", Text = "First half", Level = 1 },
new UiChartMultiLevelLabel { Start = "Jul", End = "Dec", Text = "Second half", Level = 1 }
}
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Online", Name = "Online" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "multiLevelChart",
Title = "Online sales",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "400px",
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis
{
MultiLevelLabels = new List<UiChartMultiLevelLabel>
{
new UiChartMultiLevelLabel { Start = "Jan", End = "Mar", Text = "Q1", Level = 0 },
new UiChartMultiLevelLabel { Start = "Apr", End = "Jun", Text = "Q2", Level = 0 },
new UiChartMultiLevelLabel { Start = "Jul", End = "Sep", Text = "Q3", Level = 0 },
new UiChartMultiLevelLabel { Start = "Oct", End = "Dec", Text = "Q4", Level = 0 },
new UiChartMultiLevelLabel { Start = "Jan", End = "Jun", Text = "First half", Level = 1 },
new UiChartMultiLevelLabel { Start = "Jul", End = "Dec", Text = "Second half", Level = 1 }
}
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Online", Name = "Online" }
}
})
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/MultiLevelLabels.cshtml; it draws what ViewData holds.
public ActionResult MultiLevelLabels()
{
ViewData["ChartData"] = MonthlySalesData();
return View();
}
/// <summary>The sales of one month per channel: the rows of the Line, Column and Area samples.</summary>
public class SalesData
{
public string MonthName { get; set; }
public double Online { get; set; }
public double Retail { get; set; }
public double Wholesale { get; set; }
public double Target { get; set; }
}
private static List<SalesData> MonthlySalesData()
{
var list = new List<SalesData>();
for (int m = 0; m < 12; m++)
{
list.Add(new SalesData
{
MonthName = new DateTime(2026, 1, 1).AddMonths(m).ToString("MMM"),
Online = 18000 + m * 1400 + (m % 3) * 2100,
Retail = 26000 + (m % 4) * 1800 - m * 300,
Wholesale = 12000 + m * 900 + (m % 2) * 3000,
Target = 60000 + m * 1500
});
}
return list;
}
}
}
@* 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. *@
MultiLevelLabels on the X axis name groups of categories: level 0 (nearest the axis) groups the months by quarter, level 1 by half year.