Example of Benchmark in ASP.NET Core Charts Control
This sample demonstrates the performance of Charts rendering 100K data points.
@* 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 = "benchmarkChart",
Title = "Chart with 100k points",
Data = (IEnumerable<object>)ViewData["chartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, Interval = 1, IntervalType = UiChartIntervalType.Years, Title = "Years", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Interval = 2000, Title = "Values", Minimum = 0, Maximum = 12000 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "x", YName = "y", Name = "Series", Fill = "url(#fluent-gradient-chart)" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "benchmarkChart",
Title = "Chart with 100k points",
Data = (IEnumerable<object>)ViewData["chartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, Interval = 1, IntervalType = UiChartIntervalType.Years, Title = "Years", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Interval = 2000, Title = "Values", Minimum = 0, Maximum = 12000 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, XName = "x", YName = "y", Name = "Series", Fill = "url(#fluent-gradient-chart)" }
}
})
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/Benchmark.cshtml; it draws what ViewData holds.
public ActionResult Benchmark()
{
LoadPoints();
ViewData["chartPoints"] = chartData;
return View();
}
private bool isPointsLoaded;
private Random randomNum = new Random();
private List<LineChartData1> chartData = null;
private bool visibleProperty;
private double value = 0;
private void LoadPoints()
{
chartData = new List<LineChartData1>();
isPointsLoaded = true;
visibleProperty = true;
for (int pts = 0; pts < 100000; pts++)
{
if (pts % 3 == 0)
{
value -= (randomNum.Next(0, 100) / 3) * 4;
}
else if (pts % 2 == 0)
{
value += (randomNum.Next(0, 100) / 3) * 4;
}
if (value < 0)
{
value = value * -1;
}
if (value >= 12000)
{
value = randomNum.Next(1000, 11000);
}
chartData.Add(new LineChartData1() { x = new DateTime(2005, 1, 1).AddHours(pts), y = value });
}
}
public class LineChartData1
{
public DateTime x { get; set; }
public double y { get; set; }
}
}
}
@* 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. *@
Chart includes several data rendering optimizations to achieve the best possible performance when plotting large volumes of data as well as handling high frequency real-time data. In this demo, an area series is rendered with 100K points.