Example of Custom Animation in ASP.NET MVC Charts Control
This sample visualizes the almond yield per hectare data for different countries using a spline series in the chart. The data points are enhanced with tooltip and the animation delay is configured to provide a smooth, staggered effect.
${series}: ${y}
@Html.UiChart(new UiChartOptions
{
Id = "customAnimationChart",
AnimationDuration = 1500,
AnimationDelay = 120,
Title = "Almond Yield Comparison Across Countries (2010-2022)",
Data = (IEnumerable<object>)ViewData["ChartData"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, ShowNearestTooltip = true, Template = "#tipCustomAnimation" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, ShowGridLines = false, LabelFormat = "y" },
PrimaryYAxis = new UiChartAxis { Title = "Yield (In Tons per Hectare)", Interval = 2, Maximum = 8 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y1", Name = "United States" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y2", Name = "China" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y3", Name = "Afghanistan" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y4", Name = "Yemen" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y", Name = "Australia" }
}
})
<template id="tipCustomAnimation"><b>Almond Yield - ${x}</b><br>${series}: <b>${y}</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "customAnimationChart",
AnimationDuration = 1500,
AnimationDelay = 120,
Title = "Almond Yield Comparison Across Countries (2010-2022)",
Data = (IEnumerable<object>)ViewData["ChartData"],
Tooltip = new UiChartTooltip { Enable = true, EnableHighlight = true, ShowNearestTooltip = true, Template = "#tipCustomAnimation" },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, ShowGridLines = false, LabelFormat = "y" },
PrimaryYAxis = new UiChartAxis { Title = "Yield (In Tons per Hectare)", Interval = 2, Maximum = 8 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y1", Name = "United States" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y2", Name = "China" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y3", Name = "Afghanistan" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y4", Name = "Yemen" },
new UiChartSeries { Type = UiChartSeriesType.Spline, Width = 3, XName = "X", YName = "Y", Name = "Australia" }
}
})
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/CustomAnimation.cshtml; it draws what ViewData holds.
public ActionResult CustomAnimation()
{
List<CustomAnimationChartData> chartData = new List<CustomAnimationChartData>
{
new CustomAnimationChartData { X = new DateTime(2010, 01, 01), Y = 5.00, Y1 = 4.54, Y2 = 3.62, Y3 = 2.92, Y4 = 1.87 },
new CustomAnimationChartData { X = new DateTime(2011, 01, 01), Y = 5.69, Y1 = 4.50, Y2 = 3.23, Y3 = 3.00, Y4 = 1.87 },
new CustomAnimationChartData { X = new DateTime(2012, 01, 01), Y = 4.99, Y1 = 4.60, Y2 = 4.19, Y3 = 2.97, Y4 = 1.85 },
new CustomAnimationChartData { X = new DateTime(2013, 01, 01), Y = 5.65, Y1 = 5.04, Y2 = 2.99, Y3 = 2.97, Y4 = 1.84 },
new CustomAnimationChartData { X = new DateTime(2014, 01, 01), Y = 5.43, Y1 = 4.39, Y2 = 3.07, Y3 = 2.00, Y4 = 1.84 },
new CustomAnimationChartData { X = new DateTime(2015, 01, 01), Y = 5.51, Y1 = 3.86, Y2 = 3.19, Y3 = 1.88, Y4 = 1.65 },
new CustomAnimationChartData { X = new DateTime(2016, 01, 01), Y = 6.12, Y1 = 4.12, Y2 = 3.28, Y3 = 1.81, Y4 = 1.69 },
new CustomAnimationChartData { X = new DateTime(2017, 01, 01), Y = 6.68, Y1 = 6.35, Y2 = 4.12, Y3 = 1.79, Y4 = 1.38 },
new CustomAnimationChartData { X = new DateTime(2018, 01, 01), Y = 5.52, Y1 = 3.90, Y2 = 3.39, Y3 = 1.75, Y4 = 1.72 },
new CustomAnimationChartData { X = new DateTime(2019, 01, 01), Y = 5.59, Y1 = 4.01, Y2 = 3.46, Y3 = 1.75, Y4 = 1.31 },
new CustomAnimationChartData { X = new DateTime(2020, 01, 01), Y = 5.46, Y1 = 4.64, Y2 = 3.52, Y3 = 1.78, Y4 = 1.75 },
new CustomAnimationChartData { X = new DateTime(2021, 01, 01), Y = 6.08, Y1 = 4.12, Y2 = 3.58, Y3 = 1.74, Y4 = 1.29 },
new CustomAnimationChartData { X = new DateTime(2022, 01, 01), Y = 6.23, Y1 = 3.64, Y2 = 3.40, Y3 = 1.73, Y4 = 1.64 }
};
ViewData["ChartData"] = chartData;
return View();
}
public class CustomAnimationChartData
{
public DateTime X { get; set; }
public double Y { get; set; }
public double Y1 { get; set; }
public double Y2 { get; set; }
public double Y3 { get; set; }
public double Y4 { get; set; }
}
private List<LineChartData1> chartData = null;
public class LineChartData1
{
public DateTime x { get; set; }
public double y { 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 render and configure a spline chart with multiple series representing almond yield across different countries over time. Spline charts are ideal for visualizing smooth trends in data over equal intervals, particularly time-dependent data. The Width and Animation properties are used to customize the appearance and behavior of the spline. The staggered animations enhance the visual experience by introducing each spline in sequence.
Tooltip is enabled in this example. To see the tooltip in action, hover over or tap on the chart.