Example of Bar Chart in ASP.NET Core Charts Control
This Bar Chart example demonstrates the global smartphone sales trends by brand from 2021 to 2023 using a bar chart. The data is visualized with bars representing unit sales for different brands, highlighting the comparative performance of each brand over the years.
${series}
${x} : ${y}
${x} : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "barChart",
Title = "Global Smartphone Sales Trends by Brand (2021-2023)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipBarChart" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}M", Title = "Units Sold (in Millions)", Maximum = 300 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count1", Name = "Apple", CornerRadius = 4, ColumnSpacing = 0.3 },
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count2", Name = "Xiaomi", CornerRadius = 4, ColumnSpacing = 0.3 },
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count3", Name = "Oppo", CornerRadius = 4, ColumnSpacing = 0.3 }
}
})
<template id="tipBarChart"><b>${series}</b><br>${x} : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "barChart",
Title = "Global Smartphone Sales Trends by Brand (2021-2023)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, Template = "#tipBarChart" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "{value}M", Title = "Units Sold (in Millions)", Maximum = 300 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count1", Name = "Apple", CornerRadius = 4, ColumnSpacing = 0.3 },
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count2", Name = "Xiaomi", CornerRadius = 4, ColumnSpacing = 0.3 },
new UiChartSeries { Type = UiChartSeriesType.Bar, XName = "Year", YName = "Count3", Name = "Oppo", CornerRadius = 4, ColumnSpacing = 0.3 }
}
})
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/BarChart.cshtml; it draws what ViewData holds.
public ActionResult BarChart()
{
List<BarChartData> ChartPoints = new List<BarChartData>
{
new BarChartData { Year = "2021", Count1 = 237, Count2 = 190, Count3 = 143 },
new BarChartData { Year = "2022", Count1 = 226.4, Count2 = 153.1, Count3 = 103.3 },
new BarChartData { Year = "2023", Count1 = 234.6, Count2 = 145.9, Count3 = 103.1 },
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class BarChartData
{
public string Year { get; set; }
public double Count1 { get; set; }
public double Count2 { get; set; }
public double Count3 { 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 render and configure the bar chart. The bar chart is similar to the column chart, but the orientation of the y-axis is horizontal rather than vertical.
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.