Example of Range Selection in ASP.NET Core Charts Control
This example selects the points inside a rectangle or a band that is dragged over the plot.
Inside the rectangle: nothing
@Html.UiChart(new UiChartOptions
{
Id = "rangeSelectChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Price and units sold",
SelectionMode = UiChartSelectionMode.DragXY,
OnSelectionChanged = "rangeSelected",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Title = "Price" },
PrimaryYAxis = new UiChartAxis { Title = "Units sold" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Scatter, XName = "Price", YName = "UnitsSold", Name = "Products", Marker = new UiChartMarker { Width = 12, Height = 12 } }
}
})
<p class="small text-muted">Inside the rectangle: <span id="rangeText">nothing</span></p>
<script>
window.rangeSelected = function (args) {
document.getElementById('rangeText').textContent = args.points.map(function (p) { return p.x; }).join(', ') || 'nothing';
};
</script>
@Html.UiChart(new UiChartOptions
{
Id = "rangeSelectChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Title = "Price and units sold",
SelectionMode = UiChartSelectionMode.DragXY,
OnSelectionChanged = "rangeSelected",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Title = "Price" },
PrimaryYAxis = new UiChartAxis { Title = "Units sold" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Scatter, XName = "Price", YName = "UnitsSold", Name = "Products", Marker = new UiChartMarker { Width = 12, Height = 12 } }
}
})
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/ChartRangeSelection.cshtml; it draws what ViewData holds.
public ActionResult ChartRangeSelection()
{
ViewData["ChartData"] = ProductStats();
return View();
}
/// <summary>One product: the rows of the Scatter and Bubble samples.</summary>
public class ProductData
{
public string Product { get; set; }
public double Price { get; set; }
public int UnitsSold { get; set; }
public double Revenue { get; set; }
public double Rating { get; set; }
}
private static List<ProductData> ProductStats()
{
var products = new List<ProductData>
{
new ProductData { Product = "Hand drill", Price = 89, UnitsSold = 420, Rating = 4.4 },
new ProductData { Product = "Angle grinder", Price = 129, UnitsSold = 310, Rating = 4.1 },
new ProductData { Product = "Circular saw", Price = 199, UnitsSold = 180, Rating = 4.6 },
new ProductData { Product = "Tool box", Price = 45, UnitsSold = 760, Rating = 3.9 },
new ProductData { Product = "Workbench", Price = 349, UnitsSold = 65, Rating = 4.8 },
new ProductData { Product = "Safety goggles", Price = 12, UnitsSold = 1350, Rating = 3.6 },
new ProductData { Product = "Laser level", Price = 159, UnitsSold = 140, Rating = 4.3 },
new ProductData { Product = "Screwdriver set", Price = 29, UnitsSold = 980, Rating = 4.0 }
};
foreach (ProductData product in products) { product.Revenue = product.Price * product.UnitsSold; }
return products;
}
}
}
@* 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 = DragXY: drag a rectangle over the plot and the points inside it are selected, the others fade. DragX and DragY select with a band across one axis. The result arrives in OnSelectionChanged.