Example of Axes Crossing in ASP.NET Core Charts Control
This example moves an axis to cross the other one at a chosen value, instead of at the edge of the plot.
@Html.UiChart(new UiChartOptions
{
Id = "crossXChart",
Title = "X axis crosses at 0",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { CrossesAt = 0 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Profit", Name = "Profit" } }
})
@Html.UiChart(new UiChartOptions
{
Id = "crossYChart",
Title = "Y axis crosses at Jun",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Legend = new UiChartLegend { Visible = false },
PrimaryYAxis = new UiChartAxis { CrossesAt = "Jun" },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Month", YName = "Profit", Name = "Profit", ShowMarker = true } }
})
@using System.Linq
<div class="row g-3">
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "crossXChart",
Title = "X axis crosses at 0",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { CrossesAt = 0 },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Month", YName = "Profit", Name = "Profit" } }
})
</div>
<div class="col-lg-6">
@Html.UiChart(new UiChartOptions
{
Id = "crossYChart",
Title = "Y axis crosses at Jun",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "360px",
Legend = new UiChartLegend { Visible = false },
PrimaryYAxis = new UiChartAxis { CrossesAt = "Jun" },
Series = new List<UiChartSeries> { new UiChartSeries { Type = UiChartSeriesType.Line, XName = "Month", YName = "Profit", Name = "Profit", ShowMarker = true } }
})
</div>
</div>
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/AxesCrossing.cshtml; it draws what ViewData holds.
public ActionResult AxesCrossing()
{
var profit = new[] { 12, 8, -4, -9, 3, 15, 22, 9, -6, -12, 5, 18 }
.Select((v, i) => new { Month = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }[i], Profit = v }).ToList();
ViewData["ChartData"] = profit;
return View();
}
}
}
@* 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. *@
CrossesAt moves an axis to a value of the other one. Left: the X axis crosses at 0, so the month names sit on the zero line. Right: the Y axis crosses at "Jun", in the middle of the plot.