Example of Remote Data in ASP.NET Core Charts Control
This example reads the rows of a chart from a server action, with the daily order totals.
@Html.AntiForgeryToken()
@Html.UiChart(new UiChartOptions
{
Id = "remoteDataChart",
Title = "Daily order amount",
DataUrl = Url.Action("DailyOrders", "ChartDemo"),
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "MMM yyyy" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 0 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Date", YName = "Amount", Name = "Amount", Opacity = 0.3 }
}
})
@Html.AntiForgeryToken()
@Html.UiChart(new UiChartOptions
{
Id = "remoteDataChart",
Title = "Daily order amount",
DataUrl = Url.Action("DailyOrders", "ChartDemo"),
Height = "340px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "MMM yyyy" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 0 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Date", YName = "Amount", Name = "Amount", Opacity = 0.3 }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Lumen.UI.Mvc.Demo.Models;
// With a DataUrl the chart POSTs JSON and accepts a plain array of rows or { result: [...] }.
namespace Lumen.UI.Mvc.Demo.Controllers
{
/// <summary>Separately navigable chart samples and their data endpoints.</summary>
public partial class ChartDemoController : Controller
{
/// <summary>
/// Chart DataUrl: daily order totals from the grid demo's orders. A chart accepts a plain array
/// of rows or { result: [...] }; the host does the grouping, the chart only draws.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult DailyOrders()
{
lock (SampleOrders.Sync)
{
var days = SampleOrders.All
.GroupBy(o => o.OrderDate.Date)
.OrderBy(g => g.Key)
.Select(g => new { Date = g.Key, Amount = g.Sum(o => o.Amount), Orders = g.Count() })
.ToList();
return Json(new { result = days });
}
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
public class Order
{
public int Id { get; set; }
public string OrderNo { get; set; }
public DateTime OrderDate { get; set; }
public string Customer { get; set; }
/// <summary>Key of the customer (see SampleOrders.Customers), to show a foreign key column.</summary>
public int CustomerId { get; set; }
/// <summary>North, South, East or West: a column with few values, to show grouping and a dropdown editor.</summary>
public string Region { get; set; }
public decimal Amount { get; set; }
public bool Shipped { get; set; }
/// <summary>Long free text, to show text wrapping in the grid.</summary>
public string Notes { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory test data for the grid and chart demos. A real app would query a database instead.</summary>
public static class SampleOrders
{
/// <summary>Lock held while reading or changing <see cref="All"/> (the grid demo edits it).</summary>
public static readonly object Sync = new object();
private static readonly string[] Customers =
{
"Santos Trading", "Reyes Hardware", "Dela Cruz Foods", "Bautista Auto Parts",
"Garcia Textiles", "Mendoza Electronics", "Torres Logistics", "Flores Bakery"
};
// Long notes to show text wrapping; some contain one very long word (a part number without spaces),
// which is broken inside the word only because it is wider than the column.
private static readonly string[] Notes =
{
"Deliver to the back entrance before 10 AM; the front gate is closed on weekends and public holidays.",
"Customer asked for partial delivery: send the available items now and the rest when back in stock.",
"Replacement part PN-XJ9000-ULTRAHEAVYDUTY-STAINLESSSTEELFLANGEASSEMBLY-REV7 must be checked before shipping.",
"Fragile.",
"Invoice must show the purchase order number, the project code and the name of the receiving warehouse.",
"Supercalifragilisticexpialidociousnessmaximizationprogramme discount applied at checkout.",
"Call the site supervisor thirty minutes before arrival so the loading bay can be cleared.",
"Standard delivery."
};
/// <summary>The values of Order.Region: the grouping demo's groups and the dropdown editor's choices.</summary>
public static readonly string[] Regions = { "North", "South", "East", "West" };
public static readonly List<Order> All = Build();
private static List<Order> Build()
{
var orders = new List<Order>();
var start = new DateTime(2026, 1, 5);
for (int i = 1; i <= 57; i++)
{
orders.Add(new Order
{
Id = i,
OrderNo = "SO-" + (10000 + i),
OrderDate = start.AddDays(i * 3 % 240),
Customer = Customers[i % Customers.Length],
CustomerId = i % Customers.Length + 1,
Region = Regions[i * 7 % Regions.Length],
Amount = 500m + (i * 137 % 4500),
Shipped = i % 3 != 0,
Notes = Notes[i % Notes.Length]
});
}
return orders;
}
}
}
@* 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. *@
Remote data: DataUrl points at an action that returns the rows (here the daily order totals of /ChartDemo/DailyOrders); the chart reads them when it opens. DataUrlParams would add the values the action takes.