Example of Drilldown in ASP.NET MVC Charts Control
This sample demonstrates a drill down chart with a pie for automobiles sales by region. Selecting a category navigates to its sub-categories, where sales are further broken down by country.
@Html.UiChart(new UiChartOptions
{
Id = "pieDrilldownChart",
OnDrilldown = "drillRegion",
Title = "Automobile Sales by Region - 2023",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["dataSource"],
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "x", YName = "y", Name = "Automobile Sales", ShowDataLabel = true, DataLabelPosition = UiChartLabelPosition.Outside, Radius = "70%", CornerRadius = 3, StartAngle = -30, EndAngle = 330 }
}
})
<script>
window.regionDetail = @Html.Raw(UiJson.Serialize(ViewData["RegionDetail"]));
window.drillRegion = function (args) {
var rows = window.regionDetail[args.x];
return rows ? { rows: rows, title: 'Automobile Sales in the ' + args.x + ' region' } : null;
};
</script>
@Html.UiChart(new UiChartOptions
{
Id = "pieDrilldownChart",
OnDrilldown = "drillRegion",
Title = "Automobile Sales by Region - 2023",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["dataSource"],
Legend = new UiChartLegend { Visible = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Pie, XName = "x", YName = "y", Name = "Automobile Sales", ShowDataLabel = true, DataLabelPosition = UiChartLabelPosition.Outside, Radius = "70%", CornerRadius = 3, StartAngle = -30, EndAngle = 330 }
}
})
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/PieDrilldown.cshtml; it draws what ViewData holds.
public ActionResult PieDrilldown()
{
List<DrilldownData> chartData = new List<DrilldownData>
{
new DrilldownData { x = "Asia-Pacific", y = 45 },
new DrilldownData { x = "Europe", y = 25 },
new DrilldownData { x = "North America", y = 25 },
new DrilldownData { x = "Latin America", y = 7 },
new DrilldownData { x = "Middle East & Africa", y = 3 }
};
ViewData["dataSource"] = chartData;
// What a click on a slice shows: the countries of the region (the series' rows, as Syncfusion's sample keeps them in script).
ViewData["RegionDetail"] = new Dictionary<string, List<DrilldownData>>
{
{ "Asia-Pacific", new List<DrilldownData> { new DrilldownData { x = "China", y = 66.7 }, new DrilldownData { x = "Japan", y = 17.8 }, new DrilldownData { x = "India", y = 11.1 }, new DrilldownData { x = "South Korea", y = 3.3 }, new DrilldownData { x = "Others", y = 1.1 } } },
{ "Europe", new List<DrilldownData> { new DrilldownData { x = "Germany", y = 32 }, new DrilldownData { x = "UK", y = 20 }, new DrilldownData { x = "France", y = 16 }, new DrilldownData { x = "Italy", y = 12 }, new DrilldownData { x = "Spain", y = 8 }, new DrilldownData { x = "Others", y = 12 } } },
{ "North America", new List<DrilldownData> { new DrilldownData { x = "USA", y = 75 }, new DrilldownData { x = "Canada", y = 15 }, new DrilldownData { x = "Mexico", y = 10 } } },
{ "Latin America", new List<DrilldownData> { new DrilldownData { x = "Brazil", y = 57.1 }, new DrilldownData { x = "Argentina", y = 21.4 }, new DrilldownData { x = "Chile", y = 14.3 }, new DrilldownData { x = "Others", y = 7.1 } } },
{ "Middle East & Africa", new List<DrilldownData> { new DrilldownData { x = "South Africa", y = 33.3 }, new DrilldownData { x = "Egypt", y = 26.7 }, new DrilldownData { x = "UAE", y = 23.3 }, new DrilldownData { x = "Others", y = 16.7 } } }
};
return View();
}
public class DrilldownData
{
public string x { get; set; }
public double y { get; set; }
}
private List<LineChartData1> chartData = null;
public class LineChartData1
{
public DateTime x { get; set; }
public double y { get; set; }
}
}
}
@* 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. *@
In this example, you can see how to achieve the drilldown concept using a pie chart. Automobile sales are shown in different categories. By clicking each category, you can navigate to the next level, which shows the sales by categories made by each company. Datalabel are used in this sample to show information about the data points.