Example of Histogram Chart in ASP.NET Core Charts Control
This Histogram Chart example shows how many exam scores fall in each range, with a curve of the normal distribution over them.
@* 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 = "histogramChart",
Title = "Examination result",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Histogram, YName = "Score", Name = "Score", BinInterval = 20, ShowNormalDistribution = true, ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "histogramChart",
Title = "Examination result",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Histogram, YName = "Score", Name = "Score", BinInterval = 20, ShowNormalDistribution = true, ShowDataLabel = true }
}
})
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/HistogramChart.cshtml; it draws what ViewData holds.
public ActionResult HistogramChart()
{
ViewData["ChartData"] = ExamScores();
return View();
}
/// <summary>One exam score: the rows of the Histogram sample.</summary>
public class ScoreData
{
public double Score { get; set; }
}
private static List<ScoreData> ExamScores()
{
double[] scores =
{
5.25, 7.75, 0, 8.275, 9.75, 7.75, 8.275, 6.25, 5.75, 5.25, 23, 26.5, 26.5, 27.75, 25.025, 26.5, 28.025, 29.25, 26.75, 27.25,
26.25, 25.25, 34.5, 25.625, 25.5, 26.625, 36.275, 36.25, 26.875, 40, 43, 46.5, 47.25, 45.5, 47.25, 38.25, 40.5, 39.5, 48.5, 49.5,
63.25, 62, 61.5, 68, 69.25, 62.75, 64.5, 57, 58.5, 59.5, 51, 52.5, 55.5, 54, 41.5, 44, 42.75, 33.5, 31.25, 18.5
};
return scores.Select(v => new ScoreData { Score = v }).ToList();
}
}
}
@* 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. *@
Scores counted in bins of 20 (BinInterval), with the normal distribution curve.