Example of Inversed Axis in ASP.NET MVC Charts Control
This sample uses an inverse axis in a chart to plot an exchange rate over a period of time.
@* 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 = "inversedXChart",
Title = "Exchange Rate(INR per USD)",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { OpposedPosition = true, IsInversed = true, ShowGridLines = false, ValueType = UiChartValueType.Category },
PrimaryYAxis = new UiChartAxis { LabelIntersectAction = UiChartLabelIntersectAction.Rotate45, IsInversed = true, ShowGridLines = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Year", YName = "ExchangeRate", Name = "Rate", Opacity = 1, ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "inversedXChart",
Title = "Exchange Rate(INR per USD)",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true },
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { OpposedPosition = true, IsInversed = true, ShowGridLines = false, ValueType = UiChartValueType.Category },
PrimaryYAxis = new UiChartAxis { LabelIntersectAction = UiChartLabelIntersectAction.Rotate45, IsInversed = true, ShowGridLines = false },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Year", YName = "ExchangeRate", Name = "Rate", Opacity = 1, ShowDataLabel = 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/InversedAxis.cshtml; it draws what ViewData holds.
public ActionResult InversedAxis()
{
List<InversedAxisChartData> ChartPoints = new List<InversedAxisChartData>
{
new InversedAxisChartData { Year = "2008", ExchangeRate = 1.5 },
new InversedAxisChartData { Year = "2009", ExchangeRate = 7.6 },
new InversedAxisChartData { Year = "2010", ExchangeRate = 11 },
new InversedAxisChartData { Year = "2011", ExchangeRate = 16.2 },
new InversedAxisChartData { Year = "2012", ExchangeRate = 18 },
new InversedAxisChartData { Year = "2013", ExchangeRate = 21.4 },
new InversedAxisChartData { Year = "2014", ExchangeRate = 16 },
new InversedAxisChartData { Year = "2015", ExchangeRate = 17.1 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class InversedAxisChartData
{
public string Year { get; set; }
public double ExchangeRate { 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 invert the axis of the chart. Here, both X and Y axis are inverse by setting the IsInversed property in axis to true.
Tooltip is enabled in this example. To see the tooltip in action, hover over a data point or tap one on touch-enabled devices.