Example of Sorting in ASP.NET Core Charts Control
This sample visualizes sales for vehicle range for different zone with default stacked column series in chart. Legend in the sample shows the information about those series.
Properties
@Html.UiChart(new UiChartOptions
{
Id = "sortByYChart",
Title = "Vehicle Sales by Region",
Data = (IEnumerable<object>)ViewData["dataSource"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { LabelIntersectAction = UiChartLabelIntersectAction.Rotate45, ShowGridLines = false, ValueType = UiChartValueType.Category },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}K", Title = "Sales" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "car", Name = "Car" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "trucks", Name = "Truck" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "bike", Name = "Bike" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "cycle", Name = "Cycle" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "sortByYChart",
Title = "Vehicle Sales by Region",
Data = (IEnumerable<object>)ViewData["dataSource"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { LabelIntersectAction = UiChartLabelIntersectAction.Rotate45, ShowGridLines = false, ValueType = UiChartValueType.Category },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}K", Title = "Sales" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "car", Name = "Car" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "trucks", Name = "Truck" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "bike", Name = "Bike" },
new UiChartSeries { Type = UiChartSeriesType.StackingColumn, XName = "x", YName = "cycle", Name = "Cycle" }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="sortMode">Sort By</label><select id="sortMode"><option>None</option><option>Sort by X</option><option>Sort by Y</option></select></div>
<div class="demo-property-row"><label for="descending">Descending</label><input type="checkbox" id="descending" disabled /></div>
</div>
<script>
(function () {
var chart = function () { return window.Lumen.get('sortByYChart'); };
var original = null;
var mode = document.getElementById('sortMode'), descending = document.getElementById('descending');
// Rows are put in order here and handed to the chart, as Syncfusion's sample does with ej.charts.sort.
function sortRows(rows, fields, desc) {
return rows.slice().sort(function (a, b) {
for (var i = 0; i < fields.length; i++) {
var f = fields[i], d = a[f] < b[f] ? -1 : a[f] > b[f] ? 1 : 0;
if (d !== 0) { return desc ? -d : d; }
}
return 0;
});
}
function apply() {
var c = chart();
if (!original) { original = c.getData(); }
descending.disabled = mode.value === 'None';
c.setData(mode.value === 'Sort by X' ? sortRows(original, ['x'], descending.checked)
: mode.value === 'Sort by Y' ? sortRows(original, ['car', 'trucks', 'bike', 'cycle'], descending.checked) : original);
}
mode.addEventListener('change', apply);
descending.addEventListener('change', apply);
}());
</script>
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/ChartSorting.cshtml; it draws what ViewData holds.
public ActionResult ChartSorting()
{
List<SortingData> chartData = new List<SortingData>
{
new SortingData { x= "Asia ", car= 120, trucks= 90, bike= 180, cycle= 90 },
new SortingData { x= "Canada ", car= 100, trucks= 80, bike= 90, cycle= 80 },
new SortingData { x= "Europe ", car= 80, trucks= 90, bike= 60, cycle= 50 },
new SortingData { x= "Africa ", car= 40, trucks= 20, bike= 30, cycle= 30 },
new SortingData { x= "Mexico ", car= 40, trucks= 50, bike= 80, cycle= 50 },
new SortingData { x= "US ", car= 140, trucks= 90, bike= 75, cycle= 70 }
};
ViewData["dataSource"] = chartData;
ViewData["data"] = new string[] { "None", "Sort by X", "Sort by Y" };
return View();
}
public class SortingData
{
public string x { get; set; }
public double car { get; set; }
public double trucks { get; set; }
public double bike { get; set; }
public double cycle { get; set; }
}
private List<LineChartData1> chartData = null;
public class LineChartData1
{
public DateTime x { get; set; }
public double y { 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. *@
In this example, you can see how to sort the series data in chart. To sort the data points of the series, use the sort method. This method determines whether the series data points should be sorted by their arguments or values.
Tooltip is enabled in this example. To see the tooltip in action, hover a point or tap a point in touch enabled devices.