Example of Vertical Charts in ASP.NET Core Charts Control
This sample illustrates a sales versus profit margin analysis using a vertical chart by changing the orientation of the x-axis to vertical and the y-axis to horizontal.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "verticalChart",
Title = "Sales Vs Profit Margin",
Data = (IEnumerable<object>)ViewData["chartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true },
Legend = new UiChartLegend { Visible = false, Position = UiChartLegendPosition.Right },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Sales in Billion", ShowGridLines = false, Minimum = 11000, Maximum = 15000, Interval = 1000 },
IsTransposed = true,
Axes = new List<UiChartAxis>
{
new UiChartAxis { OpposedPosition = true, Minimum = 0, Maximum = 4, Name = "yAxis2", LabelFormat = "{value}%", Interval = 0.5, Title = "Profit(In Percentage)", ShowGridLines = false }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Year", YName = "column", Name = "Sales" },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Year", YName = "series", Name = "Profit Margin", YAxisName = "yAxis2", Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, IsFilled = true } }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "verticalChart",
Title = "Sales Vs Profit Margin",
Data = (IEnumerable<object>)ViewData["chartPoints"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true },
Legend = new UiChartLegend { Visible = false, Position = UiChartLegendPosition.Right },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Sales in Billion", ShowGridLines = false, Minimum = 11000, Maximum = 15000, Interval = 1000 },
IsTransposed = true,
Axes = new List<UiChartAxis>
{
new UiChartAxis { OpposedPosition = true, Minimum = 0, Maximum = 4, Name = "yAxis2", LabelFormat = "{value}%", Interval = 0.5, Title = "Profit(In Percentage)", ShowGridLines = false }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Year", YName = "column", Name = "Sales" },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Year", YName = "series", Name = "Profit Margin", YAxisName = "yAxis2", Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, IsFilled = true } }
}
})
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/VerticalCharts.cshtml; it draws what ViewData holds.
public ActionResult VerticalCharts()
{
List<VerticalChartData> chartPoints = new List<VerticalChartData>
{
new VerticalChartData {Year = "2016", column =13600, series =0.5 },
new VerticalChartData {Year = "2017", column =12900, series =1.5 },
new VerticalChartData {Year = "2018", column =12500, series =3.5 },
new VerticalChartData {Year = "2019", column =14500, series =1.5 },
new VerticalChartData {Year = "2020", column =14500, series =3 },
new VerticalChartData {Year = "2021", column =12000, series =2.5 }
};
ViewData["chartPoints"] = chartPoints;
return View();
}
public class VerticalChartData
{
public string Year { get; set; }
public double column { get; set; }
public double series { 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 a vertical chart. Use the IsTransposed property to render the chart vertically.