Example of Selection in ASP.NET Core Charts Control
This example selects points or whole series with a click, and tells which are selected.
Selected: nothing
@Html.UiChart(new UiChartOptions
{
Id = "selectPointChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "SelectionMode = Point",
SelectionMode = UiChartSelectionMode.Point,
AllowMultiSelection = true,
OnSelectionChanged = "selectionChanged",
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 = "selectSeriesChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "SelectionMode = Series",
SelectionMode = UiChartSelectionMode.Series,
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" }
}
})
<p class="small text-muted">Selected: <span id="selectionText">nothing</span></p>
<script>
window.selectionChanged = function (args) {
document.getElementById('selectionText').textContent = args.points.map(function (p) { return p.seriesName + ' ' + p.x; }).join(', ') || 'nothing';
};
</script>
<div class="row g-3">
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "selectPointChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "SelectionMode = Point",
SelectionMode = UiChartSelectionMode.Point,
AllowMultiSelection = true,
OnSelectionChanged = "selectionChanged",
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" }
}
})
</div>
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "selectSeriesChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
Title = "SelectionMode = Series",
SelectionMode = UiChartSelectionMode.Series,
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" }
}
})
</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/ChartSelection.cshtml; it draws what ViewData holds.
public ActionResult ChartSelection()
{
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 }
};
}
}
}
@* 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. *@
SelectionMode = Point: a click selects a column (or a slice) and outlines it; AllowMultiSelection lets several stay selected (a click toggles one). OnSelectionChanged and getSelectedPoints() say which. Left: points, several at a time. Right: Series selects every column of the series.