This section explains you the steps required to create a Sparkline and demonstrate the basic usage of the Sparkline control.
To get start with ASP.NET Core application, need to ensure the following software to be installed on the machine.
The following steps to create ASP.NET Core Application.
Step 1: Create ASP.NET Core Web Application with default template project in Visual Studio 2017.
Step 2: Once your project created. We need to add Syncfusion EJ2 package into your application by using Nugget Package Manager.
Open the nuGet
package manager.
Install the Syncfusion.EJ2 package to the application
After Installation complete this will included in the project. You can refer it from the Project Assembly Reference.
We need to install NewtonSoft.JSON as dependency since it Syncfusion.EJ2 dependent to NewtonSoft.JSON package.
Step 3: Open the _ViewImports.cshtml to import Syncfusion.EJ2 package.
@addTagHelper *, Syncfusion.EJ2
Step 4: Add client side resource through CDN
or local package
in the layout page _Layout.cshtml.
@* Syncfusion Essential JS 2 Scripts *@
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>
Step 5: Adding Script Manager in layout page _Layout.cshtml.
<ejs-scripts></ejs-scripts>
Step 6: Add the below code to your Index.cshtml view page which is present under Views/Home folder, to initialize the smith chart.
@using Syncfusion.EJ2;
<ejs-sparkline id="sparkline">
</ejs-sparkline>
The [dataSource
] property is used for binding data source to the sparkline. This property takes the collection value as input. For example, the list of objects can be provided as input.
@using Syncfusion.EJ2;
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" height="100px" width="70%" dataSource="ViewBag.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>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;
namespace EJ2_Core_Application.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.data = DataSource.GetData();
return View();
}
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;
}
}
}
}
You can change the sparkline type by setting the [type
] property to [Line
], [Column
], [WinLoss
], [Pie
], or [Area
]. Here, the sparkline type has been set to [area
].
@using Syncfusion.EJ2;
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" type="Area" height="100px" width="70%" dataSource="ViewBag.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>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;
namespace EJ2_Core_Application.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.data = DataSource.GetData();
return View();
}
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;
}
}
}
}
The sparkline 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.
@using Syncfusion.EJ2;
<div class="spark" align="center">
<ejs-sparkline id="sparkline" loaded="loaded" type="Area" height="100px" width="70%" dataSource="ViewBag.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>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;
namespace EJ2_Core_Application.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.data = DataSource.GetData();
return View();
}
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;
}
}
}
}