Example of Logarithmic Axis in ASP.NET Core Charts Control
This sample shows a logarithmic axis in a chart with data about the sales of a product between 1995 and 2005.
${x} : ${y}
@Html.UiChart(new UiChartOptions
{
Id = "logChart",
Title = "Product X Growth [1995-2005]",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true, Template = "#tipLogarithmicAxis" },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Maximum = 100000, Minimum = 0, LabelFormat = "${value}", ValueType = UiChartValueType.Logarithmic, Interval = 1 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 2, XName = "Period", YName = "ProfitInfo", Name = "Product X", Opacity = 1, Marker = new UiChartMarker { Visible = true, Height = 7, Width = 7, IsFilled = true } }
}
})
<template id="tipLogarithmicAxis">${x} : <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "logChart",
Title = "Product X Growth [1995-2005]",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true, Template = "#tipLogarithmicAxis" },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Maximum = 100000, Minimum = 0, LabelFormat = "${value}", ValueType = UiChartValueType.Logarithmic, Interval = 1 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 2, XName = "Period", YName = "ProfitInfo", Name = "Product X", Opacity = 1, Marker = new UiChartMarker { Visible = true, Height = 7, Width = 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/LogarithmicAxis.cshtml; it draws what ViewData holds.
public ActionResult LogarithmicAxis()
{
List<LogAxisChartData> ChartPoints = new List<LogAxisChartData>
{
new LogAxisChartData { Period = new DateTime(1995, 1, 1), ProfitInfo = 80 },
new LogAxisChartData { Period = new DateTime(1996, 1, 1), ProfitInfo = 200 },
new LogAxisChartData { Period = new DateTime(1997, 1, 1), ProfitInfo = 400 },
new LogAxisChartData { Period = new DateTime(1998, 1, 1), ProfitInfo = 600 },
new LogAxisChartData { Period = new DateTime(1999, 1, 1), ProfitInfo = 700 },
new LogAxisChartData { Period = new DateTime(2000, 1, 1), ProfitInfo = 1400 },
new LogAxisChartData { Period = new DateTime(2001, 1, 1), ProfitInfo = 2000 },
new LogAxisChartData { Period = new DateTime(2002, 1, 1), ProfitInfo = 4000 },
new LogAxisChartData { Period = new DateTime(2003, 1, 1), ProfitInfo = 6000 },
new LogAxisChartData { Period = new DateTime(2004, 1, 1), ProfitInfo = 8000 },
new LogAxisChartData { Period = new DateTime(2005, 1, 1), ProfitInfo = 11000 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class LogAxisChartData
{
public DateTime Period { get; set; }
public double ProfitInfo { 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. *@
The logarithmic axis uses a logarithmic scale and is very useful when viewing data with values of both lower orders of magnitude (e.g., 10^-6) and higher orders of magnitude (e.g. 10^6). To use the logarithmic axis, set ValueType in axis to Logarithmic.
Tooltip is enabled in this example. To see the tooltip in action, hover over a data point or tap one on touch-enabled devices.