Example of Lazy Loading in ASP.NET MVC Charts Control
This example loads the rows of the range in view while you zoom, so the points get finer as less is shown.
Loading...
@Html.AntiForgeryToken()
@Html.UiChart(new UiChartOptions
{
Id = "lazyChart",
Title = "Daily visitors",
DataUrl = Url.Action("LazyVisitors", "ChartDemo"),
DataUrlParams = new { from = (DateTime?)null, to = (DateTime?)null },
Height = "400px",
AllowZoom = true,
KeepZoom = true,
Animation = false,
OnZoomed = "lazyZoomed",
OnLoaded = "lazyLoaded",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis
{
ValueType = UiChartValueType.DateTime,
LabelFormat = "MMM yyyy",
Minimum = (new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc) - epoch).TotalMilliseconds,
Maximum = (new DateTime(2026, 9, 30, 0, 0, 0, DateTimeKind.Utc) - epoch).TotalMilliseconds
},
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Date", YName = "Visitors", Name = "Visitors", Opacity = 0.35, Width = 1 }
}
})
@Html.AntiForgeryToken()
<p class="small text-muted" id="lazyInfo">Loading...</p>
<script>
window.lazyLoaded = function (rows) {
var days = rows.length ? rows[0].Days : 0;
document.getElementById('lazyInfo').textContent = rows.length + ' points loaded, each the average of ' + days + ' day' + (days === 1 ? '' : 's') + '.';
};
window.lazyZoomed = function (args, chartId) {
var span = args.end - args.start;
var whole = new Date(2026, 8, 30) - new Date(2024, 0, 1);
if (span >= whole * 0.97) {
Lumen.get(chartId).reload({ from: null, to: null });
return;
}
// The range in view plus half of it on each side, so a small pan needs no new request.
var from = new Date(args.start - span / 2);
var to = new Date(args.end + span / 2);
Lumen.get(chartId).reload({ from: from.toISOString(), to: to.toISOString() });
};
</script>
@{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
@Html.UiChart(new UiChartOptions
{
Id = "lazyChart",
Title = "Daily visitors",
DataUrl = Url.Action("LazyVisitors", "ChartDemo"),
DataUrlParams = new { from = (DateTime?)null, to = (DateTime?)null },
Height = "400px",
AllowZoom = true,
KeepZoom = true,
Animation = false,
OnZoomed = "lazyZoomed",
OnLoaded = "lazyLoaded",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis
{
ValueType = UiChartValueType.DateTime,
LabelFormat = "MMM yyyy",
Minimum = (new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc) - epoch).TotalMilliseconds,
Maximum = (new DateTime(2026, 9, 30, 0, 0, 0, DateTimeKind.Utc) - epoch).TotalMilliseconds
},
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "Date", YName = "Visitors", Name = "Visitors", Opacity = 0.35, Width = 1 }
}
})
// This chart uses local data: no controller action is needed.
@* 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. *@
Lazy loading: the chart opens with the whole period (2024 to September 2026) as about 240 averaged points. Zoom with the wheel or the slider and OnZoomed asks the server for just that range, so the points get finer (down to one per day) while only what is needed travels. KeepZoom keeps the range in view while the new rows replace the old.