Example of Category Axis in ASP.NET Core Charts Control
This sample shows a category axis in a chart with details about internet users across different countries.
@* 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 = "categoryAxisChart",
Title = "Internet Users – 2016",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 800, ShowGridLines = false, LabelFormat = "{value}M" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Bar, Width = 2, XName = "Country", YName = "Users", Name = "Users", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "categoryAxisChart",
Title = "Internet Users – 2016",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 800, ShowGridLines = false, LabelFormat = "{value}M" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Bar, Width = 2, XName = "Country", YName = "Users", Name = "Users", 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/CategoryAxis.cshtml; it draws what ViewData holds.
public ActionResult CategoryAxis()
{
List<CategoryData> ChartPoints = new List<CategoryData>
{
new CategoryData { Country = "Germany", Users = 72, TooltipMappingName = "GER: 72" },
new CategoryData { Country = "Russia", Users = 103.1, TooltipMappingName = "RUS: 103.1" },
new CategoryData { Country = "Brazil", Users = 139.1, TooltipMappingName = "BRZ: 139.1" },
new CategoryData { Country = "India", Users = 462.1, TooltipMappingName = "IND: 462.1" },
new CategoryData { Country = "China", Users = 721.4, TooltipMappingName = "CHN: 721.4" },
new CategoryData { Country = "United States<br>Of America", Users = 286.9, TooltipMappingName = "USA: 286.9" },
new CategoryData { Country = "Great Britain", Users = 115.1, TooltipMappingName = "GBR: 115.1" },
new CategoryData { Country = "Nigeria", Users = 97.2, TooltipMappingName = "NGR: 97.2" }
};
ViewData["ChartPoints"] = ChartPoints;
ViewData["font"] = new
{
fontWeight = "600",
color = "#ffffff"
};
return View();
}
public class CategoryData
{
public string Country { get; set; }
public double Users { get; set; }
public string TooltipMappingName { get; set; }
}
}
}
@* 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. *@
You can use the category axis to represent string values instead of numbers in the chart. To use the category axis, set ValueType in axis to Category.