Example of Keyboard Navigation in ASP.NET MVC Charts Control
This example moves between the points and the series of a chart with the arrow keys.
@* 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 = "keyboardChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Title = "Sales by region",
EnableKeyboardNavigation = true,
SelectionMode = UiChartSelectionMode.Point,
AllowMultiSelection = true,
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "keyboardChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Title = "Sales by region",
EnableKeyboardNavigation = true,
SelectionMode = UiChartSelectionMode.Point,
AllowMultiSelection = true,
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4" }
}
})
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/ChartKeyboard.cshtml; it draws what ViewData holds.
public ActionResult ChartKeyboard()
{
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. *@
EnableKeyboardNavigation: press Tab to reach the chart, then Left / Right to move between the points, Up / Down between the series, Home / End to the first / last point, Enter or Space to select and Escape to let go. The point the keyboard is on shows its tooltip and is read out to screen readers.