Getting Started with ASP.NET Core Sparkline Charts Control
23 Jul 202613 minutes to read
This section briefly explains how to include the ASP.NET Core Sparkline Charts control in your ASP.NET Core application using Visual Studio.
Create an ASP.NET Core Web App with Razor Pages
Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.
Install the required ASP.NET Core package
To add ASP.NET Core Sparkline Charts control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetCore.Sparkline package. All Syncfusion ASP.NET Core packages are available in nuget.org. See the NuGet packages topic for details.
Alternatively, you can install the same package using the Package Manager Console with the following command.
Install-Package Syncfusion.AspNetCore.Sparkline -Version 34.1.29Add the ASP.NET Core Tag Helpers
After the package is installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.Charts Tag Helpers.
@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.ChartsAdd script resources
Include the script reference inside the <head> of ~/Pages/Shared/_Layout.cshtml.
<head>
...
<!-- ASP.NET Core controls scripts -->
<script src="_content/Syncfusion.AspNetCore.Sparkline/scripts/sf-sparkline.min.js"></script>
</head>Register the script manager
Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.
<body>
...
<!-- ASP.NET Core Script Manager -->
<ejs-scripts></ejs-scripts>
</body>Add ASP.NET Core Sparkline Charts control
Add the ASP.NET Core Sparkline Charts control in the ~/Pages/Index.cshtml file.
<ejs-sparkline id="sparkline">
</ejs-sparkline>Bind data source to Sparkline Charts
The dataSource property is used for binding data source to the Sparkline Charts. This property takes the collection value as input. For example, you can provide a list of objects as input.
@{
var data = DataSource.GetData();
}
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" height="100px" width="70%" dataSource="data" xName="xval" yName="yval"></ejs-sparkline>
</div>
<style>
.spark {
border: 1px solid rgb(209, 209, 209);
border-radius: 2px;
width: 100%;
height: 100%;
}
</style>
<script>
function loaded(args)
{
window.sparkline = args.sparkline;
args.sparkline.loaded = null;
args.sparkline.refresh();
}
</script>public class DataSource
{
public int x;
public string xval;
public double yval;
public static List<DataSource> GetData()
{
List<DataSource> data1 = new List<DataSource>();
data1.Add(new DataSource() { x = 0, xval = "2005", yval = 20090440 });
data1.Add(new DataSource() { x = 1, xval = "2006", yval = 20264080 });
data1.Add(new DataSource() { x = 2, xval = "2007", yval = 20434180 });
data1.Add(new DataSource() { x = 3, xval = "2008", yval = 21007310 });
data1.Add(new DataSource() { x = 4, xval = "2009", yval = 21262640 });
data1.Add(new DataSource() { x = 5, xval = "2010", yval = 21515750 });
data1.Add(new DataSource() { x = 6, xval = "2011", yval = 21766710 });
data1.Add(new DataSource() { x = 7, xval = "2012", yval = 22015580 });
data1.Add(new DataSource() { x = 8, xval = "2013", yval = 22262500 });
data1.Add(new DataSource() { x = 9, xval = "2014", yval = 22507620 });
return data1;
}
}Run the application
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The ASP.NET Core Sparkline Charts will render in your default web browser.

Change the type of Sparkline Charts
You can change the Sparkline Charts type by setting the type property to Line, Column, WinLoss, Pie, or Area. Here, the sparkline type has been set to area.
@{
var data = DataSource.GetData();
}
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" type="Area" height="100px" width="70%" dataSource="data" xName="xval" yName="yval"></ejs-sparkline>
</div>
<style>
.spark {
border: 1px solid rgb(209, 209, 209);
border-radius: 2px;
width: 100%;
height: 100%;
}
</style>
<script>
function loaded(args)
{
window.sparkline = args.sparkline;
args.sparkline.loaded = null;
args.sparkline.refresh();
}
</script>public class DataSource
{
public int x;
public string xval;
public double yval;
public static List<DataSource> GetData()
{
List<DataSource> data1 = new List<DataSource>();
data1.Add(new DataSource() { x = 0, xval = "2005", yval = 20090440 });
data1.Add(new DataSource() { x = 1, xval = "2006", yval = 20264080 });
data1.Add(new DataSource() { x = 2, xval = "2007", yval = 20434180 });
data1.Add(new DataSource() { x = 3, xval = "2008", yval = 21007310 });
data1.Add(new DataSource() { x = 4, xval = "2009", yval = 21262640 });
data1.Add(new DataSource() { x = 5, xval = "2010", yval = 21515750 });
data1.Add(new DataSource() { x = 6, xval = "2011", yval = 21766710 });
data1.Add(new DataSource() { x = 7, xval = "2012", yval = 22015580 });
data1.Add(new DataSource() { x = 8, xval = "2013", yval = 22262500 });
data1.Add(new DataSource() { x = 9, xval = "2014", yval = 22507620 });
return data1;
}
}
Enable tooltip for Sparkline Charts
The Sparkline Charts displays additional information through tooltip when the mouse is hovered over the sparkline. You can enable tooltip by setting the visible property to true in tooltipSettings object.
@{
var data = DataSource.GetData();
}
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" type="Area" height="100px" width="70%" dataSource="data" xName="xval" yName="yval">
<e-sparkline-tooltipsettings visible="true" format="${xval} : ${yval}"></e-sparkline-tooltipsettings>
</ejs-sparkline>
</div>
<style>
.spark {
border: 1px solid rgb(209, 209, 209);
border-radius: 2px;
width: 100%;
height: 100%;
}
</style>
<script>
function loaded(args)
{
window.sparkline = args.sparkline;
args.sparkline.loaded = null;
args.sparkline.refresh();
}
</script>public class DataSource
{
public int x;
public string xval;
public double yval;
public static List<DataSource> GetData()
{
List<DataSource> data1 = new List<DataSource>();
data1.Add(new DataSource() { x = 0, xval = "2005", yval = 20090440 });
data1.Add(new DataSource() { x = 1, xval = "2006", yval = 20264080 });
data1.Add(new DataSource() { x = 2, xval = "2007", yval = 20434180 });
data1.Add(new DataSource() { x = 3, xval = "2008", yval = 21007310 });
data1.Add(new DataSource() { x = 4, xval = "2009", yval = 21262640 });
data1.Add(new DataSource() { x = 5, xval = "2010", yval = 21515750 });
data1.Add(new DataSource() { x = 6, xval = "2011", yval = 21766710 });
data1.Add(new DataSource() { x = 7, xval = "2012", yval = 22015580 });
data1.Add(new DataSource() { x = 8, xval = "2013", yval = 22262500 });
data1.Add(new DataSource() { x = 9, xval = "2014", yval = 22507620 });
return data1;
}
}
NOTE