Data Visualization in Facebook Applications Using Infragistics jQuery Chart

[Infragistics] Mihail Mateev / Wednesday, December 14, 2011

The development of Facebook applications is almost always associated the attractive visualization of data.

In this article you will learn how to create attractive charts based on data from social network with NetAdvantage for jQuery Vol.2 2011 and in particular with jQuery Chart (CTP for 11.2 Release). Infragistics jQuery components could be used in different Web applications as jQuery widgets or via .NET helpers in ASP.Net Web Forms and MVC applications.

An interesting case is the development of Facebook applications with ASP.Net MVC3 and Razor. If you are not familiar with ASP.Net MVC3 and/or Facebook C# SDK you could learn how to start from the blog Creating Facebook applications with NetAdvantage for jQuery, ASP.Net MVC3 and Facebook C # SDK – using jQuery Grid (Part 1) .

This article will demonstrate a simple prototype that shows in Infragistics jQuery  Grid number of photo albums for each of the friends of current user.

Before you start:

To begin development, you must satisfy the following requirements

Steps of implementation:

  • Create Facebook application and set application properties
  • Create ASP.Net MVC3 Razor application
  • Add references to NetAdvantage for jQuery and Facebook C# SDK
  • Create required controllers  and views and implement code to maintain user friends info

Sample application

Sample application is based on the sample from article Creating Facebook applications with NetAdvantage for jQuery, ASP.Net MVC3 and Facebook C # SDK – using jQuery Grid (Part 1) . You could download this sample here.

Facebook application settings.

Demo application  is designed to run within an iframe on a Facebook Canvas page. You could create a canvas application in http://developers.facebook.com/. In the current sample Facebook application is named jQueryGridDemo

Update references to NetAdvantage for jQuery and Facebook C# SDK

You should update references to NetAdvantage for jQuery. To be possible to use jQuery Chart you need to add style for chart:

<link href="@Url.Content("~/Content/IGStyles/base/ig.ui.chart.css")" rel="stylesheet" type="text/css" />

and jQuery Chart JavaScript code:

<script src="@Url.Content("~/Scripts/IG/ig.ui.chart.js")" type="text/javascript"></script>

in default view (Shared/_Layout.cshtml in this case).

All references are shown below:

   1: <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
   2:  
   3:  <link href="@Url.Content("~/Content/IGStyles/ig/jquery.ui.custom.css")" rel="stylesheet" type="text/css" />
   4:  <link href="@Url.Content("~/Content/IGStyles/ig/ig.ui.min.css")" rel="stylesheet" type="text/css" />
   5:  <link href="@Url.Content("~/Content/IGStyles/base/ig.ui.grid.css")" rel="stylesheet" type="text/css" />
   6:  <link href="@Url.Content("~/Content/IGStyles/base/ig.ui.shared.css")" rel="stylesheet" type="text/css" />
   7:  <link href="@Url.Content("~/Content/IGStyles/base/ig.ui.editors.css")" rel="stylesheet" type="text/css" />
   8:  <link href="@Url.Content("~/Content/IGStyles/base/ig.ui.chart.css")" rel="stylesheet" type="text/css" />
   9:  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  10:  <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
  11:  
  12:  <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>  
  13:  @*<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>*@
  14:  <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
  15:  <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
  16:  <script src="@Url.Content("~/Scripts/IG/ig.ui.min.js")" type="text/javascript"></script>
  17:  <script src="@Url.Content("~/Scripts/IG/ig.ui.chart.js")" type="text/javascript"></script>

 

You need also to update the Infragistics.Web.Mvc assembly reference from NetAdvantage for jQuery 2011 Vol.2.

Controllers:

In the sample is used the FacebookController class from the origin article.

To be possible to get information about friend photos you need to add a “friends_photo”s permission.

   1: private const string ExtendedPermissions = 
   2: "user_about_me,publish_stream,friends_location,user_location,friends_online_presence,read_friendlists,friends_photos";

 

You need to modify the action “OnlineIndex” used to return view with online users. To improve the performance of the applications you could use multi-query with Facebook Query Language (FQL).

   1: var fb = new FacebookWebClient();
   2: dynamic me = fb.Get("me");
   3:  
   4: string query0 =
   5:     "SELECT uid, name, pic_square, ***, locale, online_presence, current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND online_presence IN ('active', 'idle')";
   6:  
   7: string query1 = "SELECT aid,owner,name FROM album WHERE owner IN (SELECT uid FROM #query0)";
   8:  
   9: dynamic selectedFriends = fb.Query(query0, query1);
  10:  
  11: ViewBag.OnlineFriends = selectedFriends;

 

Here you could see the  FacebookController code

   1: using System.Collections.Generic;
   2: using System.Dynamic;
   3: using System.Linq;
   4: using System.Web.Mvc;
   5: using Facebook.Web;
   6: using Facebook.Web.Mvc;
   7:  
   8:  
   9: namespace CS_Canvas_AspNetMvc3_WithoutJsSdk.Controllers
  10: {
  11:     public class FacebookController : Controller
  12:     {
  13:         private const string SimpleExtendedPermissions = "user_about_me,publish_stream";
  14:  
  15:         private const string ExtendedPermissions = "user_about_me,publish_stream,friends_location,user_location,friends_online_presence,read_friendlists,friends_photos";
  16:  
  17:         [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
  18:         [CanvasAuthorize(Permissions = SimpleExtendedPermissions)]
  19:         public ActionResult Index()
  20:         {
  21:             var fb = new FacebookWebClient();
  22:             dynamic me = fb.Get("me");
  23:  
  24:             dynamic friendsData = fb.Get("/me/friends");
  25:  
  26:  
  27:             ViewBag.FriendsData = friendsData;
  28:  
  29:  
  30:             ViewBag.ProfilePictureUrl = string.Format("http://graph.facebok.com/{0}/picture", me.id);
  31:             ViewBag.Name = me.name;           
  32:             ViewBag.FirstName = me.first_name;
  33:             ViewBag.LastName = me.last_name;
  34:  
  35:  
  36:             ViewBag.MessagePostSuccess = Request.QueryString.AllKeys.Contains("success") &&
  37:                                          Request.QueryString["success"] == "True";
  38:  
  39:             return View();
  40:  
  41:             
  42:         }
  43:  
  44:         [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
  45:         [CanvasAuthorize(Permissions = ExtendedPermissions)]
  46:         public ActionResult OnlineIndex()
  47:         {
  48:             var fb = new FacebookWebClient();
  49:             dynamic me = fb.Get("me");
  50:  
  51:             string query0 =
  52:                 "SELECT uid, name, pic_square, ***, locale, online_presence, current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND online_presence IN ('active', 'idle')";
  53:  
  54:             string query1 = "SELECT aid,owner,name FROM album WHERE owner IN (SELECT uid FROM #query0)";
  55:  
  56:             dynamic selectedFriends = fb.Query(query0, query1);
  57:  
  58:             ViewBag.OnlineFriends = selectedFriends;
  59:  
  60:  
  61:             ViewBag.ProfilePictureUrl = string.Format("http://graph.facebok.com/{0}/picture", me.id);
  62:             ViewBag.Name = me.name;
  63:             ViewBag.FirstName = me.first_name;
  64:             ViewBag.LastName = me.last_name;
  65:  
  66:             return View();
  67:  
  68:  
  69:         }
  70:  
  71:     }
  72: }

 

View Models

In the sample application is added a view model for photo album (AlbumInfo).

   1: public class AlbumInfo
   2: {
   3:     public string Aid { get; set; }
   4:     public long Owner { get; set; }
   5:     public string Name { get; set; }
   6: }

 

FacebookFriendInfo class is also modified to have a property AlbumsCount

   1: public class FacebookFriendInfo
   2: {
   3:     public long Id { get; set; }
   4:     public string Name { get; set; }
   5:     public string Gender { get; set; }
   6:     public string PictureUrl { get; set; }
   7:     public string CurrentLocation { get; set; }
   8:     public string OnlinePresence { get; set; }
   9:     public int AlbumsCount { get; set; }
  10: }

OnlineIndex View

You will have two collections with friends and friend albums

   1: dynamic friendsData =     ViewBag.OnlineFriends;
   2:  
   3: var result0 = friendsData[0].fql_result_set;
   4:  
   5: var result1 = friendsData[1].fql_result_set;

 

To be possible to use data in Infragistics  jQuery Grid and jQuery Chart you a collection, that implements IQueryable.

   1: List<FacebookFriendInfo> myFriends = new List<FacebookFriendInfo>();
   2:  
   3: List<AlbumInfo> albums = new List<AlbumInfo>();
   4:  
   5:  
   6: foreach (dynamic friend in result0)
   7: {
   8:     FacebookFriendInfo rowList = new FacebookFriendInfo();
   9:  
  10:     rowList.Id = friend.uid;
  11:     rowList.Name = friend.name;
  12:     rowList.Gender = friend.***;
  13:     if (friend.current_location != null)
  14:     {
  15:         rowList.CurrentLocation = friend.current_location.name;
  16:     }        
  17:     rowList.OnlinePresence = friend.online_presence;
  18:     rowList.PictureUrl = friend.pic_square;
  19:  
  20:     myFriends.Add(rowList);
  21:  
  22:  
  23: }
  24:  
  25: foreach (dynamic album in result1)
  26: {
  27:     AlbumInfo rowList = new AlbumInfo();
  28:     rowList.Aid = album.aid;
  29:     rowList.Name = album.name;
  30:     rowList.Owner = album.owner;
  31:     albums.Add(rowList);
  32:  
  33: }
  34:  
  35: foreach (FacebookFriendInfo friendInfo in myFriends)
  36: {
  37:     friendInfo.AlbumsCount = albums.Where(a => a.Owner == friendInfo.Id).Count();     
  38: }
  39:  
  40:  
  41: var queryable = myFriends.AsQueryable();

 

Infragistics jQuery Chart

Chart uses the collection “queryable”.  For X-axis is used the Facebook friend name. For Y-axis is used the number of photo albums.

   1: @(Html.Infragistics().DataChart(queryable)
   2:    .ID("chart1")
   3:    .Width("500px")
   4:    .Height("500px")
   5:    .VerticalZoomable(true)
   6:    .HorizontalZoomable(true)
   7:    .Legend(legend => legend.ID("legend1"))
   8:    .Axes((axes) =>
   9:        {
  10:            axes.CategoryX("xAxis").Label((d) => d.Name);
  11:            axes.NumericY("yAxis");
  12:        })
  13:    .Series((series) =>
  14:        {
  15:            series
  16:            .Column("series1")
  17:            .Title("Friend Albums")
  18:            .XAxis("xAxis").YAxis("yAxis")
  19:            .ValueMemberPath((item) => item.AlbumsCount)
  20:            .ShowTooltip(true)
  21:            .TooltipTemplate("tooltipTemplate");
  22:        }).DataBind().Render()
  23:       )

 

Tooltip templates

To display friendly data in the tooltips you could use tooltip templates.

   1: <script type="text/javascript">
   2:  
   3:     $(document).ready(function () {
   4:         $("#tooltipTemplate").template("tooltipTemplate");
   5:         $('#homeButton').click(function () {
   6:             location.href = '/Home/Index';
   7:         });
   8:         $('#friendsButton').click(function () {
   9:             location.href = '/Facebook/Index';
  10:         });
  11:     });
  12:  
  13: </script>

 

Run the application

Enjoy! You have a nice Facebook ASP.Net MVC3 application with Infragistics jQuery Chart inside it.

Source code you could download :