Example of Column in ASP.NET MVC Charts Control
This Column Chart example visualizes the production of Walnuts and Almonds for different countries in 2023.
${x}
${series}: ${y}
${series}: ${y}
@Html.UiChart(new UiChartOptions
{
Id = "columnChart",
Title = "Walnuts and Almonds Estimated Production for 2023",
SubTitle = "Source: fas.usda.gov",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipColumnChart" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { Interval = 1, ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Metric Tons", Interval = 40000 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Walnuts", Name = "Walnuts", CornerRadius = 4, ColumnSpacing = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Almonds", Name = "Almonds", ColumnSpacing = 0.4 }
}
})
<template id="tipColumnChart"><b>${x}</b><br>${series}: <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "columnChart",
Title = "Walnuts and Almonds Estimated Production for 2023",
SubTitle = "Source: fas.usda.gov",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipColumnChart" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { Interval = 1, ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Metric Tons", Interval = 40000 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Walnuts", Name = "Walnuts", CornerRadius = 4, ColumnSpacing = 0.4 },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "Almonds", Name = "Almonds", ColumnSpacing = 0.4 }
}
})
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/ColumnChart.cshtml; it draws what ViewData holds.
public ActionResult ColumnChart()
{
List<ColumnChartData> ChartPoints = new List<ColumnChartData>
{
new ColumnChartData { Country = "Chile", Walnuts = 175000, Almonds = 11300 },
new ColumnChartData { Country = "European Union", Walnuts = 140000, Almonds = 135000 },
new ColumnChartData {Country = "Turkey", Walnuts = 67000, Almonds = 24000 },
new ColumnChartData {Country = "India", Walnuts = 33000, Almonds = 4200 },
new ColumnChartData {Country = "Australia", Walnuts = 12000, Almonds = 154000 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class ColumnChartData
{
public string Country { get; set; }
public double Walnuts { get; set; }
public double Almonds { 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 render and configure a column chart. The column chart is used to compare the frequency, count, total, or average of data in different categories.
Tooltip is enabled in this example. To see the tooltip in action, hover over a point or tap on a point in touch-enabled devices.