Example of Pie with Patterns in ASP.NET MVC Charts Control
This sample fills the slices of a pie and the columns of a chart with patterns instead of plain colours.
@Html.UiChart(new UiChartOptions
{
Id = "patternPieChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Patterns on a pie",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Pie, XName = "Region", YName = "Q4", Name = "Q4",
Patterns = new List<UiChartPattern> { UiChartPattern.Dots, UiChartPattern.DiagonalForward, UiChartPattern.Crosshatch, UiChartPattern.Chessboard }
}
}
})
@Html.UiChart(new UiChartOptions
{
Id = "patternColumnChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "A pattern on columns",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1", Patterns = new List<UiChartPattern> { UiChartPattern.DiagonalBackward } },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4", Patterns = new List<UiChartPattern> { UiChartPattern.Dots } }
}
})
<div class="row g-3">
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "patternPieChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Patterns on a pie",
Legend = new UiChartLegend { Position = UiChartLegendPosition.Bottom },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Pie, XName = "Region", YName = "Q4", Name = "Q4",
Patterns = new List<UiChartPattern> { UiChartPattern.Dots, UiChartPattern.DiagonalForward, UiChartPattern.Crosshatch, UiChartPattern.Chessboard }
}
}
})
</div>
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "patternColumnChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "A pattern on columns",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1", Patterns = new List<UiChartPattern> { UiChartPattern.DiagonalBackward } },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4", Patterns = new List<UiChartPattern> { UiChartPattern.Dots } }
}
})
</div>
</div>
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/PiePatterns.cshtml; it draws what ViewData holds.
public ActionResult PiePatterns()
{
ViewData["ChartData"] = RegionSalesData();
return View();
}
/// <summary>The sales of one region per quarter: the rows of the Bar, Stacked Column and Bar Gradient samples.</summary>
public class RegionData
{
public string Region { get; set; }
public double Q1 { get; set; }
public double Q2 { get; set; }
public double Q3 { get; set; }
public double Q4 { get; set; }
}
private static List<RegionData> RegionSalesData()
{
return new List<RegionData>
{
new RegionData { Region = "Luzon", Q1 = 42000, Q2 = 48500, Q3 = 51200, Q4 = 60300 },
new RegionData { Region = "Visayas", Q1 = 27500, Q2 = 29800, Q3 = 33100, Q4 = 36900 },
new RegionData { Region = "Mindanao", Q1 = 21300, Q2 = 24100, Q3 = 23800, Q4 = 28700 },
new RegionData { Region = "Metro Manila", Q1 = 55800, Q2 = 58200, Q3 = 61900, Q4 = 70400 }
};
}
}
}
@* 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. *@
Patterns fills the slices with patterns instead of (or over) plain colours, in turn: dots, diagonal lines, a grid, a chessboard, and so on. They help when the chart is printed in black and white or read by people who cannot tell the colours apart. Columns and areas take a pattern in the same way.