Example of Trendlines in ASP.NET MVC Charts Control
This example draws linear, polynomial and exponential trendlines over the points of a series, with a forecast on each side of the linear one.
@* 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 = "trendChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
PrimaryXAxis = new UiChartAxis { Title = "Rating", ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { Title = "Units sold" },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Scatter, XName = "Rating", YName = "UnitsSold", Name = "Products",
Trendlines = new List<UiChartTrendline>
{
new UiChartTrendline { Type = UiChartTrendlineType.Linear, Name = "Linear", ForwardForecast = 0.2, BackwardForecast = 0.2 },
new UiChartTrendline { Type = UiChartTrendlineType.Polynomial, PolynomialOrder = 3, Name = "Polynomial", Fill = "#e7910f" },
new UiChartTrendline { Type = UiChartTrendlineType.Exponential, Name = "Exponential", Fill = "#09af74", DashArray = "5,3" }
}
}
}
})
@Html.UiChart(new UiChartOptions
{
Id = "trendChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "340px",
PrimaryXAxis = new UiChartAxis { Title = "Rating", ValueType = UiChartValueType.Double },
PrimaryYAxis = new UiChartAxis { Title = "Units sold" },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Scatter, XName = "Rating", YName = "UnitsSold", Name = "Products",
Trendlines = new List<UiChartTrendline>
{
new UiChartTrendline { Type = UiChartTrendlineType.Linear, Name = "Linear", ForwardForecast = 0.2, BackwardForecast = 0.2 },
new UiChartTrendline { Type = UiChartTrendlineType.Polynomial, PolynomialOrder = 3, Name = "Polynomial", Fill = "#e7910f" },
new UiChartTrendline { Type = UiChartTrendlineType.Exponential, Name = "Exponential", Fill = "#09af74", DashArray = "5,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/Trendlines.cshtml; it draws what ViewData holds.
public ActionResult Trendlines()
{
ViewData["ChartData"] = ProductStats();
return View();
}
/// <summary>One product: the rows of the Scatter and Bubble samples.</summary>
public class ProductData
{
public string Product { get; set; }
public double Price { get; set; }
public int UnitsSold { get; set; }
public double Revenue { get; set; }
public double Rating { get; set; }
}
private static List<ProductData> ProductStats()
{
var products = new List<ProductData>
{
new ProductData { Product = "Hand drill", Price = 89, UnitsSold = 420, Rating = 4.4 },
new ProductData { Product = "Angle grinder", Price = 129, UnitsSold = 310, Rating = 4.1 },
new ProductData { Product = "Circular saw", Price = 199, UnitsSold = 180, Rating = 4.6 },
new ProductData { Product = "Tool box", Price = 45, UnitsSold = 760, Rating = 3.9 },
new ProductData { Product = "Workbench", Price = 349, UnitsSold = 65, Rating = 4.8 },
new ProductData { Product = "Safety goggles", Price = 12, UnitsSold = 1350, Rating = 3.6 },
new ProductData { Product = "Laser level", Price = 159, UnitsSold = 140, Rating = 4.3 },
new ProductData { Product = "Screwdriver set", Price = 29, UnitsSold = 980, Rating = 4.0 }
};
foreach (ProductData product in products) { product.Revenue = product.Price * product.UnitsSold; }
return products;
}
}
}
@* 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. *@
Linear with a forecast of 0.2 on each side (ForwardForecast, BackwardForecast), Polynomial (order 3) and Exponential (dashed); they show in the legend.