This topic walks through instantiating an igSparkline
™ in an ASP.NET MVC view and bind to a .NET collection of objects.
The following table lists the concepts and topics required as a prerequisite to understanding this topic.
Topics
This topic contains the following sections:
The igSparkline
can be added to an ASP.NET MVC view using the ASP.NET MVC helper. Because the igSparkline
is a data-bound control, data is generated on the server to pass to the view within the controller ActionMethod
.
The Invoice model object contains an ExtendedPrice
field containing the amount of the order and an OrderDate
field containing the date of the order purchase. The ValueMemberPath
of the igSparkline
is set to the ExtendedPrice
, and the LabelMemberPath
is set to the OrderDate
.
An ASP.NET MVC application configured with the required JavaScript files, CSS files, and ASP.NET MVC assembly as outlined in the Adding Controls to an MVC Project topic.
This procedure walks through instantiating an igSparkline
in an ASP.NET MVC view and bind to a .NET collection of objects.
The following screenshot is a preview of the result.
To complete the procedure, you need the following:
Following is a conceptual overview of the process:
Add a reference to Infragistics.Web.Mvc.dll
Configure the view dependencies
Define the data collection
Instantiate the Sparkline
Full ASPX View Code Listing
The following steps demonstrate adding an igSparkline
to an ASP.NET MVC ASPX view using the ASP.NET MVC helper.
Adding a reference to Infragistics.Web.Mvc.dll
Add a reference to the Infragistics.Web.Mvc.dll
in your ASP.NET application if you have not done so already. The details for adding this assembly are available in the Adding Controls to an MVC Project topic.
Configuring the view dependencies
Import the Infragistics.Web.Mvc namespace
In order to use the ASP.NET MVC helper, you must import the Infragistics.Web.Mvc namespace onto your view.
In ASPX:
<%@ Import Namespace="Infragistics.Web.Mvc" %>
Adding references to all of the required JavaScript and CSS files
Add the following file references to the HEAD tag of the ASP.NET MVC View:
In ASPX:
<link href="<%= Url.Content("~/infragistics/css/themes/infragistics/infragistics.theme.css") %>" rel="stylesheet" />
<link href="<%= Url.Content("~/infragistics/css/structure/infragistics.css") %>" rel="stylesheet" />
<script src="<%= Url.Content("~/js/jquery.js") %>"></script>
<script src="<%= Url.Content("~/js/jquery-ui.js") %>"></script>
<script src="<%= Url.Content("~/js/modernizr.js") %>"></script>
<script src="<%= Url.Content("~/infragistics/js/infragistics.core.js") %>"></script>
<script src="<%= Url.Content("~/infragistics/js/infragistics.dv.js") %>"></script>
Defining the data collection
Define the model object Invoice
Define a basic Invoice object in your application to use for the data collection.
In C#:
using System;
public class Invoice
{
public Nullable<System.DateTime> OrderDate { get; set; }
public Nullable<decimal> ExtendedPrice { get; set; }
}
Note: This definition does not specify a namespace for the class. If you do use a namespace for the class, you must add an Import statement in your ASP.NET ASPX view to compile the rest of the example.
Create a generic list of Invoice objects in your controller’s action method and return with your view.
In C#:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<Invoice> invoices = new List<Invoice>
{
new Invoice{OrderDate = new DateTime(2012,8,23), ExtendedPrice = 356.89m },
new Invoice{OrderDate = new DateTime(2012,8,23), ExtendedPrice = 500.98m },
new Invoice{OrderDate = new DateTime(2012,8,23), ExtendedPrice = 125.98m },
new Invoice{OrderDate = new DateTime(2012,8,24), ExtendedPrice = 56.23m },
new Invoice{OrderDate = new DateTime(2012,8,24), ExtendedPrice = 895.60m },
new Invoice{OrderDate = new DateTime(2012,8,24), ExtendedPrice = 400.56m },
new Invoice{OrderDate = new DateTime(2012,8,25), ExtendedPrice = 600.25m },
new Invoice{OrderDate = new DateTime(2012,8,25), ExtendedPrice = 986.30m },
new Invoice{OrderDate = new DateTime(2012,8,26), ExtendedPrice = 111.26m },
new Invoice{OrderDate = new DateTime(2012,8,26), ExtendedPrice = 356.25m },
new Invoice{OrderDate = new DateTime(2012,8,26), ExtendedPrice = 29.65m },
};
return View(invoices);
}
}
Instantiating the igSparkline
Instantiate the igSparkline
using the ASP.NET MVC helper and set basic options
Within the body of your ASP.NET view, use the ASP.NET MVC helper to instantiate the igSparkline
.
By providing the type in the Sparkline method, Sparkline
When instantiating the Finally, as with all of the Ignite UI for MVC controls, you must call the Render method to render the HTML and JavaScript to the view. In ASPX: Full ASPX View Code Listing In ASPX: The following topics provide additional information related to this topic. jQuery and MVC API Links (igSparkline): This topic provides links to the API documentation for jQuery and ASP.NET MVC helper class for the Adding igSparkline to an HTML Document: This topic explains how to add the The following sample provides additional information related to this topic.LabelMemberPath
and ValueMemberPath
based off the underlying model object.igSparkline
, there are several helper methods that should be set for basic rendering including the following:
Helper Method
Purpose
DataSource()
Accepts the data collection for the
igSparkline
- in this case, the List of Invoices that we set as the model for the view
Height()
Sets the string height of the
igSparkline
Width()
Sets the string width of the
igSparkline
ValueMemberPath()
Set this helper method to the Invoice member that signifies the value the
igSparkline
renders on the vertical axis for each item.
LabelMemberPath()
Set this helper method to the Invoice member that represents the horizontal axis value.
<body>
<%= Html.Infragistics().Sparkline<Invoice>(Model)
.DataSource(Model)
.Height("200px")
.Width("300px")
.LabelMemberPath(m => m.OrderDate)
.ValueMemberPath(m => m.ExtendedPrice)
.Render()%>
</body>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Invoice>>" %>
<%@ Import Namespace="Infragistics.Web.Mvc" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="<%= Url.Content("~/infragistics/css/themes/infragistics/infragistics.theme.css") %>" rel="stylesheet" />
<link href="<%= Url.Content("~/infragistics/css/structure/infragistics.css") %>" rel="stylesheet" />
<script src="<%= Url.Content("~/js/jquery.js") %>"></script>
<script src="<%= Url.Content("~/js/jquery-ui.js") %>"></script>
<script src="<%= Url.Content("~/js/modernizr.js") %>"></script>
<script src="<%= Url.Content("~/infragistics/js/infragistics.core.js") %>"></script>
<script src="<%= Url.Content("~/infragistics/js/infragistics.dv.js") %>"></script>
</head>
<body>
<%= Html.Infragistics().Sparkline<Invoice>(Model)
.DataSource(Model)
.Height("200px")
.Width("300px")
.LabelMemberPath(m => m.OrderDate)
.ValueMemberPath(m => m.ExtendedPrice)
.Render()%>
</body>
</html>
Related Content
Topics
igSparkline
control.igSparkline
to an HTML page and bind to a JavaScript array.Samples
View on GitHub