Example of Candle and Moving Average in ASP.NET MVC Charts Control
This Candle Chart example shows the open, high, low and close price of each trading day, with a candle that is coloured by the direction of the day.
@* 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 = "candleChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Crosshair = new UiChartCrosshair { Enable = true },
PrimaryXAxis = new UiChartAxis { LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 90, Maximum = 135 },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Candle, XName = "Day", High = "High", Low = "Low", Open = "Open", Close = "Close", Name = "Lumen",
Trendlines = new List<UiChartTrendline> { new UiChartTrendline { Type = UiChartTrendlineType.MovingAverage, Period = 3, Name = "3-day average", Fill = "#c50f1f" } }
}
}
})
@Html.UiChart(new UiChartOptions
{
Id = "candleChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Crosshair = new UiChartCrosshair { Enable = true },
PrimaryXAxis = new UiChartAxis { LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 90, Maximum = 135 },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Candle, XName = "Day", High = "High", Low = "Low", Open = "Open", Close = "Close", Name = "Lumen",
Trendlines = new List<UiChartTrendline> { new UiChartTrendline { Type = UiChartTrendlineType.MovingAverage, Period = 3, Name = "3-day average", Fill = "#c50f1f" } }
}
}
})
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/CandleChart.cshtml; it draws what ViewData holds.
public ActionResult CandleChart()
{
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;
}
}
}
@* 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. *@
Rising candles hollow (EnableSolidCandles = false); a MovingAverage trendline over 3 days.