Example of Pagination in ASP.NET Core Charts Control
This example pages the legend of a chart when it has too many entries to show at once.
@* 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 = "paginationChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "420px",
Title = "Twelve products over six weeks",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = Enumerable.Range(1, 12).Select(s => new UiChartSeries
{
Type = UiChartSeriesType.Line, XName = "Week", YName = "Product " + s, Name = "Product " + s
}).ToList()
})
@Html.UiChart(new UiChartOptions
{
Id = "paginationChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "420px",
Title = "Twelve products over six weeks",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = Enumerable.Range(1, 12).Select(s => new UiChartSeries
{
Type = UiChartSeriesType.Line, XName = "Week", YName = "Product " + s, Name = "Product " + s
}).ToList()
})
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/ChartPagination.cshtml; it draws what ViewData holds.
public ActionResult ChartPagination()
{
var weeks = Enumerable.Range(1, 6).Select(w => new Dictionary<string, object>(
Enumerable.Range(1, 12).ToDictionary(s => "Product " + s, s => (object)(20 + (w * s * 7) % 40))) { { "Week", "W" + w } }).ToList();
ViewData["ChartData"] = weeks;
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. *@
When a chart has more series than its legend can show, the legend pages itself: the arrows and "1/3" under the chart move through the entries (Syncfusion's legend pagination). Here twelve series share a narrow chart. A click on an entry hides its series.