Example of Indexed Category Axis in ASP.NET MVC Charts Control
This example shows an indexed category axis, which gives every row its own place even when rows share a value.
@* 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 = "categoryIndexedChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "ValueType = IndexedCategory",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.IndexedCategory },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Item", YName = "Sold", Name = "Sold", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "categoryIndexedChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "ValueType = IndexedCategory",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.IndexedCategory },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Item", YName = "Sold", Name = "Sold", 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/IndexedCategoryAxis.cshtml; it draws what ViewData holds.
public ActionResult IndexedCategoryAxis()
{
ViewData["ChartData"] = new[]
{
new { Item = "Hand drill", Sold = 120 }, new { Item = "Saw", Sold = 80 }, new { Item = "Hand drill", Sold = 95 },
new { Item = "Level", Sold = 60 }, new { Item = "Hand drill", Sold = 140 }, new { Item = "Saw", Sold = 110 }
};
return View();
}
}
}
@* 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. *@
ValueType = IndexedCategory gives every row its own place on the axis, in data order, even when rows share an X value: the six rows here, three of them for "Hand drill", draw six columns. (A plain Category axis would add the three into one.)