Example of Spline updating each second in ASP.NET MVC Charts Control
This Spline Chart example gets a new point every second and keeps the last 25.
@Html.UiChart(new UiChartOptions
{
Id = "splineLiveChart",
Title = "Live reading",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Animation = false,
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, XName = "Tick", YName = "Value", Name = "Value", Width = 2 }
}
})
@using System.Linq
@Html.UiChart(new UiChartOptions
{
Id = "splineLiveChart",
Title = "Live reading",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Animation = false,
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, XName = "Tick", YName = "Value", Name = "Value", Width = 2 }
}
})
<script>
window.addEventListener('load', function () {
var chart = Lumen.get('splineLiveChart');
var tick = 25;
window.splineTimer = setInterval(function () {
var rows = chart.getData().slice(1);
var last = rows[rows.length - 1].Value;
rows.push({ Tick: tick++, Value: Math.max(5, Math.min(95, last + (Math.random() - 0.5) * 18)) });
chart.setData(rows);
}, 1000);
});
</script>
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/SplineUpdating.cshtml; it draws what ViewData holds.
public ActionResult SplineUpdating()
{
ViewData["ChartData"] = Enumerable.Range(0, 25).Select(i => new { Tick = i, Value = 50 + 20 * System.Math.Sin(i / 3.0) }).ToList();
return View();
}
}
}
@* 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. *@
A spline that gets a new point every second; the chart keeps the last 25 (setData from a timer).