Example of Line in ASP.NET Core Charts Control
This Line Chart example represents the crude steel production annual growth data with default line series in the chart. Data points are enhanced with markers, data labels and tooltips.
${series}
${x} : ${y}M
${x} : ${y}M
@Html.UiChart(new UiChartOptions
{
Id = "lineChart",
Height = "460px",
Title = "Annual Crude Steel Production by Country (2016-2024)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartData"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#lineTooltip" },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Volume in million metric tons", Minimum = 0, Maximum = 25, Interval = 5 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Viet_Growth", Name = "Vietnam", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Circle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Can_Growth", Name = "Indonesia", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 6, Height = 6, Shape = UiChartMarkerShape.Triangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Mal_Growth", Name = "France", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Diamond, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Egy_Growth", Name = "Poland", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 5, Height = 5, Shape = UiChartMarkerShape.Rectangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Ind_Growth", Name = "Mexico", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Pentagon, IsFilled = true } }
}
})
<template id="lineTooltip"><b>${series}</b><br>${x} : <b>${y}M</b></template>
@Html.UiChart(new UiChartOptions
{
Id = "lineChart",
Height = "460px",
Title = "Annual Crude Steel Production by Country (2016-2024)",
SubTitle = "Source: wikipedia.org",
Data = (IEnumerable<object>)ViewData["ChartData"],
Tooltip = new UiChartTooltip { Enable = true, Template = "#lineTooltip" },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { Title = "Volume in million metric tons", Minimum = 0, Maximum = 25, Interval = 5 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Viet_Growth", Name = "Vietnam", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Circle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Can_Growth", Name = "Indonesia", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 6, Height = 6, Shape = UiChartMarkerShape.Triangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Mal_Growth", Name = "France", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Diamond, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Egy_Growth", Name = "Poland", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 5, Height = 5, Shape = UiChartMarkerShape.Rectangle, IsFilled = true } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "Ind_Growth", Name = "Mexico", ShowDataLabel = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, Shape = UiChartMarkerShape.Pentagon, IsFilled = 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/LineChart.cshtml; it draws what ViewData holds.
public ActionResult LineChart()
{
ViewData["ChartData"] = new List<LineChartData>
{
new LineChartData { Period = 2016, Can_Growth = 4.8, Viet_Growth = 7.8, Mal_Growth = 14.6, Egy_Growth = 8.9, Ind_Growth = 19.0 },
new LineChartData { Period = 2017, Can_Growth = 5.2, Viet_Growth = 10.3, Mal_Growth = 15.5, Egy_Growth = 10.3, Ind_Growth = 20.0 },
new LineChartData { Period = 2018, Can_Growth = 6.2, Viet_Growth = 15.5, Mal_Growth = 15.4, Egy_Growth = 10.8, Ind_Growth = 20.2 },
new LineChartData { Period = 2019, Can_Growth = 7.8, Viet_Growth = 17.5, Mal_Growth = 14.4, Egy_Growth = 9.0, Ind_Growth = 18.4 },
new LineChartData { Period = 2020, Can_Growth = 9.3, Viet_Growth = 19.5, Mal_Growth = 11.6, Egy_Growth = 7.9, Ind_Growth = 16.8 },
new LineChartData { Period = 2021, Can_Growth = 14.3, Viet_Growth = 23.0, Mal_Growth = 13.9, Egy_Growth = 8.5, Ind_Growth = 18.5 },
new LineChartData { Period = 2022, Can_Growth = 15.6, Viet_Growth = 20.0, Mal_Growth = 12.1, Egy_Growth = 7.4, Ind_Growth = 18.4 },
new LineChartData { Period = 2023, Can_Growth = 16.0, Viet_Growth = 19.0, Mal_Growth = 10.0, Egy_Growth = 6.4, Ind_Growth = 16.3 },
new LineChartData { Period = 2024, Can_Growth = 17.0, Viet_Growth = 22.1, Mal_Growth = 10.8, Egy_Growth = 7.1, Ind_Growth = 13.7 }
};
return View();
}
/// <summary>One year of the annual crude steel production (million metric tons) of five countries: the rows of the Line sample.</summary>
public class LineChartData
{
public int Period { get; set; }
public double Can_Growth { get; set; }
public double Viet_Growth { get; set; }
public double Mal_Growth { get; set; }
public double Egy_Growth { get; set; }
public double Ind_Growth { 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. *@
Line Chart represents and visualizes time-dependent data to show the trends at equal intervals. In this example, you can see how to render and configure a line chart. Marker is used to represent individual data and its values.
Tooltip is enabled in this example, with its own template for the text. To see the tooltip in action, hover over or tap on the chart.
ShowDataLabel is enabled in this example to display the value of each point, and the legend names each line series.