Example of DateTime Axis in ASP.NET Core Charts Control
This sample shows the date-time axis in a chart with a weather report for the year 2016.
@* 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 = "dateChart",
Title = "Alaska Weather Statistics - 2016",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true },
Legend = new UiChartLegend { Visible = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "MMM d", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = -20, Maximum = 30, Interval = 10, LabelFormat = "{value}°C" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "MaxTemp", Name = "Warmest", Opacity = 1, ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 8, Height = 8, IsFilled = true, Shape = UiChartMarkerShape.Pentagon } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "MinTemp", Name = "Coldest", Opacity = 1, ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 8, Height = 8, IsFilled = true, Shape = UiChartMarkerShape.Diamond } }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "dateChart",
Title = "Alaska Weather Statistics - 2016",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true },
Legend = new UiChartLegend { Visible = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "MMM d", ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Minimum = -20, Maximum = 30, Interval = 10, LabelFormat = "{value}°C" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "MaxTemp", Name = "Warmest", Opacity = 1, ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 8, Height = 8, IsFilled = true, Shape = UiChartMarkerShape.Pentagon } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "MinTemp", Name = "Coldest", Opacity = 1, ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 8, Height = 8, IsFilled = true, Shape = UiChartMarkerShape.Diamond } }
}
})
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/DateAxis.cshtml; it draws what ViewData holds.
public ActionResult DateAxis()
{
List<DateTimeData> ChartPoints = new List<DateTimeData>
{
new DateTimeData { Period = new DateTime(2016, 3, 07), MaxTemp = 6.3, MinTemp = -5.3},
new DateTimeData { Period = new DateTime(2016, 4, 15), MaxTemp = 13.3, MinTemp = 1.0 },
new DateTimeData { Period = new DateTime(2016, 5, 10), MaxTemp = 18.0, MinTemp = 6.9 },
new DateTimeData { Period = new DateTime(2016, 6, 17), MaxTemp = 19.8, MinTemp = 9.4 },
new DateTimeData { Period = new DateTime(2016, 7, 13), MaxTemp = 18.1, MinTemp = 7.6 },
new DateTimeData { Period = new DateTime(2016, 8, 11), MaxTemp = 13.1, MinTemp = 2.6 },
new DateTimeData { Period = new DateTime(2016, 9, 16), MaxTemp = 4.1, MinTemp = -4.9 }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class DateTimeData
{
public DateTime Period { get; set; }
public double MaxTemp { get; set; }
public double MinTemp { 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 date-time axis uses a date-time scale and displays date-time values as the axis labels. To use a date-time axis, set the ValueType in axis to DateTime.
Tooltip is enabled in this example. To see the tooltip in action, hover over or tap on the chart.