Example of JavaScript API in ASP.NET Core Charts Control
This example works on a chart with its methods from buttons: replace the data, hide a series, export a picture and retry a failing request.
Three series from the same rows: each series picks its own YName. Values on the Y axis use LabelFormat = "C0".
@Html.AntiForgeryToken()
@Html.UiChart(new UiChartOptions
{
Id = "columnChart",
Title = "Monthly sales by channel",
SubTitle = "2026",
Data = SampleSales.Monthly,
PrimaryXAxis = new UiChartAxis { Title = "Month" },
PrimaryYAxis = new UiChartAxis { Title = "Sales", LabelFormat = "C0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Online", Name = "Online" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Retail", Name = "Retail" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Wholesale", Name = "Wholesale" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "failingChart",
DataUrl = Url.Action("Failing", "ChartDemo"),
Height = "300px",
ErrorText = "This chart fails on purpose to show OnDataError.",
OnDataError = "ChartDemo.onDataError",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1" }
}
})
@using Lumen.UI.Mvc.Demo.Models
@Html.AntiForgeryToken()
@* Samples/_JavaScriptApi.cshtml *@
@Html.Partial("Samples/_ApiColumnChart")
@Html.Partial("Samples/_FailingChart")
@* Samples/_ApiColumnChart.cshtml *@
<p>Three series from the same rows: each series picks its own <code>YName</code>. Values on the Y axis use <code>LabelFormat = "C0"</code>.</p>
@Html.UiChart(new UiChartOptions
{
Id = "columnChart",
Title = "Monthly sales by channel",
SubTitle = "2026",
Data = SampleSales.Monthly,
PrimaryXAxis = new UiChartAxis { Title = "Month" },
PrimaryYAxis = new UiChartAxis { Title = "Sales", LabelFormat = "C0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Online", Name = "Online" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Retail", Name = "Retail" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "MonthName", YName = "Wholesale", Name = "Wholesale" }
}
})
@* Samples/_FailingChart.cshtml *@
@Html.UiChart(new UiChartOptions
{
Id = "failingChart",
DataUrl = Url.Action("Failing", "ChartDemo"),
Height = "300px",
ErrorText = "This chart fails on purpose to show OnDataError.",
OnDataError = "ChartDemo.onDataError",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1" }
}
})
@section below {
<h3>Event log</h3>
<ul id="eventLog" style="font-family:monospace;font-size:12px;max-height:200px;overflow-y:auto;border:1px solid var(--ui-border, #dadce3);padding:8px;"></ul>
}
@section scripts {
<script>
window.ChartDemo = (function () {
'use strict';
function log(message) {
var list = document.getElementById('eventLog');
if (!list) { return; }
var item = document.createElement('li');
item.textContent = new Date().toLocaleTimeString() + ' - ' + message;
list.insertBefore(item, list.firstChild);
}
return {
onDataError: function (message, chartId) {
log(chartId + ': data error - ' + message);
},
randomize: function () {
var chart = Lumen.get('columnChart');
var rows = chart.getData().map(function (r) {
return {
MonthName: r.MonthName,
Online: Math.round(10000 + Math.random() * 30000),
Retail: Math.round(10000 + Math.random() * 30000),
Wholesale: Math.round(10000 + Math.random() * 30000)
};
});
chart.setData(rows);
}
};
})();
</script>
}
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
{
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Failing()
{
throw new InvalidOperationException("Simulated failure for the Lumen.UI.Mvc chart demo.");
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>Sales of one month per channel.</summary>
public class MonthlySales
{
public DateTime Month { get; set; }
/// <summary>"Jan", "Feb", ... for category axes.</summary>
public string MonthName { get; set; }
public decimal Online { get; set; }
public decimal Retail { get; set; }
public decimal Wholesale { get; set; }
public decimal Target { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory test data for the chart demo. A real app would query a database instead.</summary>
public static class SampleSales
{
public static readonly IList<MonthlySales> Monthly = BuildMonthly();
private static IList<MonthlySales> BuildMonthly()
{
var list = new List<MonthlySales>();
for (int m = 0; m < 12; m++)
{
var month = new DateTime(2026, 1, 1).AddMonths(m);
list.Add(new MonthlySales
{
Month = month,
MonthName = month.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;
}
}
}
@* 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. *@
Every chart is reachable with Lumen.get(id). These buttons work on the column chart below; retry reloads the deliberately failing chart.