Example of Range Color Mapping in ASP.NET Core Charts Control
This example colours each column by the range its value falls in, with the ranges shown as a key above the chart.
@* 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 = "rangeColorChart",
Title = "Highest temperature",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Legend = new UiChartLegend { Visible = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}\u00B0C" },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Column, XName = "Month", YName = "High", Name = "High", ShowDataLabel = true,
ColorRanges = new List<UiChartColorRange>
{
new UiChartColorRange { To = 20, Color = "#0076e5", Label = "Cold" },
new UiChartColorRange { From = 20, To = 28, Color = "#09af74", Label = "Mild" },
new UiChartColorRange { From = 28, To = 32, Color = "#f7a600", Label = "Warm" },
new UiChartColorRange { From = 32, Color = "#d13438", Label = "Hot" }
}
}
}
})
@Html.UiChart(new UiChartOptions
{
Id = "rangeColorChart",
Title = "Highest temperature",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Legend = new UiChartLegend { Visible = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}\u00B0C" },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Column, XName = "Month", YName = "High", Name = "High", ShowDataLabel = true,
ColorRanges = new List<UiChartColorRange>
{
new UiChartColorRange { To = 20, Color = "#0076e5", Label = "Cold" },
new UiChartColorRange { From = 20, To = 28, Color = "#09af74", Label = "Mild" },
new UiChartColorRange { From = 28, To = 32, Color = "#f7a600", Label = "Warm" },
new UiChartColorRange { From = 32, Color = "#d13438", Label = "Hot" }
}
}
}
})
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/RangeColorMapping.cshtml; it draws what ViewData holds.
public ActionResult RangeColorMapping()
{
ViewData["ChartData"] = TemperatureRanges();
return View();
}
/// <summary>The temperature of one month in two cities: the rows of the range samples.</summary>
public class TemperatureData
{
public string Month { get; set; }
public double Low { get; set; }
public double High { get; set; }
public double OtherLow { get; set; }
public double OtherHigh { get; set; }
}
private static List<TemperatureData> TemperatureRanges()
{
double[][] rows =
{
new[] { 4.0, 14, 8, 20 }, new[] { 6.0, 17, 9, 22 }, new[] { 9.0, 20, 11, 24 }, new[] { 12.0, 24, 13, 27 },
new[] { 15.0, 27, 16, 30 }, new[] { 18.0, 31, 19, 33 }, new[] { 20.0, 33, 21, 34 }, new[] { 19.0, 32, 21, 33 }
};
return rows.Select((t, i) => new TemperatureData
{
Month = new DateTime(2026, i + 1, 1).ToString("MMM"),
Low = t[0],
High = t[1],
OtherLow = t[2],
OtherHigh = t[3]
}).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. *@
ColorRanges colours each column by the range its value falls in (Cold below 20, Mild to 28, Warm to 32, Hot above), and the ranges are the key above the chart.