Example of Animation in ASP.NET Core Charts Control
This sample demonstrates the animation capabilities of various chart series types. Dynamic updates at regular intervals showcase smooth transitions and animations.
@* 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 = "animationChart",
Data = (IEnumerable<object>)ViewData["LineData"],
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Maximum = 100, Minimum = 0, LabelFormat = "{value}" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 3, XName = "x", YName = "y", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Height = 8, Width = 8 } }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "animationChart",
Data = (IEnumerable<object>)ViewData["LineData"],
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Maximum = 100, Minimum = 0, LabelFormat = "{value}" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 3, XName = "x", YName = "y", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Height = 8, Width = 8 } }
}
})
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/Animation.cshtml; it draws what ViewData holds.
public ActionResult Animation()
{
ViewData["LineData"] = GetLineData();
ViewData["ColumnData"] = GetColumnData();
ViewData["SplineData"] = GetSplineData();
ViewData["AreaData"] = GetAreaData();
ViewData["BarData"] = GetBarData();
ViewData["BubbleData"] = GetBubbleData();
ViewData["ScatterData"] = GetScatterData();
ViewData["StepLineData"] = GetStepLineData();
ViewData["RangeColumnData"] = GetRangeColumnData();
return View();
}
private List<DataPoint> GetLineData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 30 },
new DataPoint { x = "2", y = 10 },
new DataPoint { x = "3", y = 80 },
new DataPoint { x = "4", y = 20 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 5 },
new DataPoint { x = "7", y = 69 },
new DataPoint { x = "8", y = 15 },
new DataPoint { x = "9", y = 60 },
new DataPoint { x = "10", y = 70 }
// Add more points here
};
}
private List<DataPoint> GetColumnData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 90 },
new DataPoint { x = "2", y = 10 },
new DataPoint { x = "3", y = 50 },
new DataPoint { x = "4", y = 20 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 70 },
new DataPoint { x = "7", y = 9 }
};
}
private List<DataPoint> GetSplineData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 30 },
new DataPoint { x = "2", y = 10 },
new DataPoint { x = "3", y = 80 },
new DataPoint { x = "4", y = 20 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 5 },
new DataPoint { x = "7", y = 69 },
new DataPoint { x = "8", y = 15 },
new DataPoint { x = "9", y = 60 },
new DataPoint { x = "10", y = 70 }
};
}
private List<DataPoint> GetAreaData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 10 },
new DataPoint { x = "2", y = 20 },
new DataPoint { x = "3", y = 80 },
new DataPoint { x = "4", y = 15 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 40 },
new DataPoint { x = "7", y = 69 },
new DataPoint { x = "8", y = 15 }
// Add more points here
};
}
private List<DataPoint> GetBarData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 90 },
new DataPoint { x = "2", y = 10 },
new DataPoint { x = "3", y = 50 },
new DataPoint { x = "4", y = 20 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 70 },
new DataPoint { x = "7", y = 9 }
};
}
private List<DataPoint> GetBubbleData()
{
return new List<DataPoint>
{
new DataPoint { x = "1.5", y = 80, size = 5 },
new DataPoint { x = "2", y = 60, size = 10 },
new DataPoint { x = "3", y = 70, size = 8 },
new DataPoint { x = "4", y = 13, size = 6 },
new DataPoint { x = "5", y = 30, size = 9 },
new DataPoint { x = "6", y = 20, size = 7 },
new DataPoint { x = "6.5", y = 40, size = 11 }
};
}
private List<DataPoint> GetScatterData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 15, y1 = 10 },
new DataPoint { x = "1.25", y = 35, y1 = 20 },
new DataPoint { x = "1.5", y = 60, y1 = 50 },
// ... 34 more rows
};
}
private List<DataPoint> GetStepLineData()
{
return new List<DataPoint>
{
new DataPoint { x = "1", y = 10 },
new DataPoint { x = "2", y = 30 },
new DataPoint { x = "3", y = 80 },
new DataPoint { x = "4", y = 20 },
new DataPoint { x = "5", y = 30 },
new DataPoint { x = "6", y = 40 },
new DataPoint { x = "7", y = 69 },
new DataPoint { x = "8", y = 15 },
new DataPoint { x = "9", y = 60 },
new DataPoint { x = "10", y = 70 }
};
}
private List<DataRangePoint> GetRangeColumnData()
{
return new List<DataRangePoint>
{
new DataRangePoint { x = "1", low = 30, high = 60 },
new DataRangePoint { x = "2", low = 42, high = 73 },
new DataRangePoint { x = "3", low = 35, high = 80 },
new DataRangePoint { x = "4", low = 20, high = 50 },
new DataRangePoint { x = "5", low = 30, high = 80 },
new DataRangePoint { x = "6", low = 10, high = 40 },
new DataRangePoint { x = "7", low = 15, high = 69 }
// Add more points here
};
}
public class DataPoint
{
public string x { get; set; }
public double y { get; set; }
public double? size { get; set; }
public double? y1 { get; set; } // Only used for scatter with multiple series
}
public class DataRangePoint
{
public string x { get; set; }
public double low { get; set; }
public double high { 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. *@
In this example, different types of chart series, such as Line, Column, Spline, Area, Bar, Bubble, Scatter, Step Line, and Range Column, are animated to demonstrate real-time data updates. The chart dynamically updates with random values using the setData method. Each chart type reflects its unique characteristics and enhances visual understanding through animations.