Example of Live stock data in ASP.NET Core Charts Control
This Candle Chart example gets a new candle every second: the last one closes and a new one opens.
@Html.UiChart(new UiChartOptions
{
Id = "liveStockChart",
Title = "Live prices",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Animation = false,
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Candle, XName = "Day", High = "High", Low = "Low", Open = "Open", Close = "Close", Name = "Price" }
}
})
@using System.Linq
@Html.UiChart(new UiChartOptions
{
Id = "liveStockChart",
Title = "Live prices",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Animation = false,
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Candle, XName = "Day", High = "High", Low = "Low", Open = "Open", Close = "Close", Name = "Price" }
}
})
<script>
window.addEventListener('load', function () {
var chart = Lumen.get('liveStockChart');
var n = chart.getData().length;
window.stockTimer = setInterval(function () {
var rows = chart.getData().slice(-29);
var previous = rows[rows.length - 1];
var open = previous.Close;
var close = open + (Math.random() - 0.5) * 6;
rows.push({ Day: 'T' + (n++), Open: open, Close: close, High: Math.max(open, close) + Math.random() * 2, Low: Math.min(open, close) - Math.random() * 2 });
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/LiveStockData.cshtml; it draws what ViewData holds.
public ActionResult LiveStockData()
{
ViewData["ChartData"] = StockPrices();
return View();
}
/// <summary>One trading day: the rows of the Hilo, Hilo Open Close and Candle samples.</summary>
public class StockData
{
public DateTime Date { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
}
private static List<StockData> StockPrices()
{
var list = new List<StockData>();
double close = 100;
double[] moves = { 4, -3, 6, 5, -4, 7, -2, 3, -5, 6, 4, -3, 2, 5 };
for (int i = 0; i < moves.Length; i++)
{
DateTime day = new DateTime(2026, 3, 2).AddDays(i + (i / 5) * 2); // weekdays only
double open = close + (i % 3 == 0 ? -1 : 1);
close = open + moves[i];
list.Add(new StockData
{
Date = day,
Open = open,
Close = close,
High = Math.Max(open, close) + 2 + i % 3,
Low = Math.Min(open, close) - 2 - i % 2
});
}
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. *@
Candles arrive every second: the last candle closes and a new one opens (setData from a timer).