TreeMap For AngularJS Using ASP.NET MVC 5

Introduction

In this post, I will show you how to create TreeMap, using Web API2, AngularJS, and Entity Framework.

Prerequisites

As I said earlier, we are going to use the TreeMap plugin in our MVC application with AngularJS. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL Database part

Here, you can find the scripts to create the database and table.

Create Database

  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object: Database [PopulationDB] Script Date: 9/12/2016 8:54:39 AM ******/  
  5. CREATE DATABASE [PopulationDB]  
  6. CONTAINMENT = NONE  
  7. ON PRIMARY   
  8. NAME = N'PopulationDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PopulationDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9. LOG ON   
  10. NAME = N'PopulationDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PopulationDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [PopulationDB] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [PopulationDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [PopulationDB] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [PopulationDB] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [PopulationDB] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [PopulationDB] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [PopulationDB] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [PopulationDB] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [PopulationDB] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [PopulationDB] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [PopulationDB] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [PopulationDB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [PopulationDB] SET CURSOR_DEFAULT GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [PopulationDB] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [PopulationDB] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [PopulationDB] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [PopulationDB] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [PopulationDB] SET DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [PopulationDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [PopulationDB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [PopulationDB] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [PopulationDB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [PopulationDB] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [PopulationDB] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [PopulationDB] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [PopulationDB] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [PopulationDB] SET MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [PopulationDB] SET PAGE_VERIFY CHECKSUM   
  98. GO  
  99.   
  100. ALTER DATABASE [PopulationDB] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [PopulationDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [PopulationDB] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [PopulationDB] SET READ_WRITE   
  110. GO  
Create Table
  1. USE [PopulationDB]  
  2. GO  
  3.   
  4. /****** Object: Table [dbo].[tbt_populations] Script Date: 9/13/2016 8:20:32 AM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[tbt_populations](  
  15. [ID] [intNOT NULL,  
  16. [COUNTRY] [varchar](50) NULL,  
  17. [POPULATION] [bigintNULL,  
  18. CONSTRAINT [PK_tbt_populations] PRIMARY KEY CLUSTERED   
  19. (  
  20. [ID] ASC  
  21. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  22. ON [PRIMARY]  
  23.   
  24. GO  
  25.   
  26. SET ANSI_PADDING OFF  
  27. GO  
After creating the table, you can add some records, as shown below.

table

Create your MVC application

Open Visual Studio and select File >> New Project. The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

MVC application

Now, a new window pops up for selecting the template. Choose Web API and click OK.

Web API

Now, we are going to add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Data Model, right click on the project name and click Add > Add New Item. A new dialog box will pop up. Inside Visual C#, select Data >> ADO.NET Entity Data Model.
 
Now, enter the name for your Dbcontext model as DbContextPopulation and finally, click Add.

Adding ADO.NET Entity Data Model

At this level, choose "EF Designer from database", as show below.

EF Designer

In the following window, we need to choose the data connection which should be used to connect to the database. If the connection doesn’t exist, click on the "New Connection" button for creating a new one.

connection

After clicking on Next button, the Entity Data Model Wizard will pop up for choosing the object which we want to use. In this example, we are going to choose tbt_populations table and click Finish. Finally, we see that EDMX model generates tbt_Populations class.

class

Create a Controller

Now, we are going to create a Controller. Right click on the "Controllers" folder > Add > Controller> selecting Web API 2 Controller – Empty > click Add.

controller

Enter Controller name (‘PopulationController’).

Controller name

PopulationController.cs
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. using TreeMap_For_AngularJS.Models;  
  9.   
  10. namespace TreeMap_For_AngularJS.Controllers  
  11. {  
  12.     public class PopulationController : ApiController  
  13.     {  
  14.   
  15.         //DbContext  
  16.         private PopulationsDBEntities context = new PopulationsDBEntities();  
  17.   
  18.         [HttpGet]  
  19.         public IEnumerable<PopulationModel> GetPopulation()  
  20.         {  
  21.             var PopulationList = context.tbt_populations.Select(p => new PopulationModel { label = p.COUNTRY, value = p.POPULATION });  
  22.   
  23.             return PopulationList.ToList();  
  24.   
  25.         }  
  26.           
  27.   
  28.     }  
  29. }  
Here, I’m creating GetPopulation() action which will select all data from tbt_Poplulations table and return the result as list of PopulationModel.

Here, you find the definition of PopulationModel class.

PopulationModel.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace TreeMap_For_AngularJS.Models  
  7. {  
  8.     public class PopulationModel  
  9.     {  
  10.   
  11.         public string label { get; set; }  
  12.         public long value { get; set; }  
  13.     }  
  14. }  
HomeController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Http;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. using TreeMap_For_AngularJS.Models;  
  8.   
  9. namespace TreeMap_For_AngularJS.Controllers  
  10. {  
  11.     public class HomeController : Controller  
  12.     {  
  13.         public ActionResult Index()  
  14.         {  
  15.             return View();  
  16.         }  
  17.   
  18.         IEnumerable<PopulationModel> PopulationList = Enumerable.Empty<PopulationModel>();  
  19.   
  20.         [HttpGet]  
  21.         public JsonResult GetPopulationList()  
  22.         {  
  23.             HttpClient client = new HttpClient();  
  24.             client.BaseAddress = new Uri("http://localhost:56720/");  
  25.             client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));  
  26.   
  27.             HttpResponseMessage response = client.GetAsync("api/Population").Result;  
  28.   
  29.             if (response.IsSuccessStatusCode)  
  30.             {  
  31.                  PopulationList = response.Content.ReadAsAsync<IEnumerable<PopulationModel>>().Result;  
  32.             }  
  33.   
  34.             return Json(PopulationList, JsonRequestBehavior.AllowGet);  
  35.               
  36.              
  37.         }  
  38.     }  
  39. }  
As you can see, I’m creating GetPopulationList() action which calls our API.

For calling our API, you need to,
  • Create an object from HttpClient class.
  • Specify URI of our API (in this example the URI used is: http://localhost:56720/).
  • Select the header of request. I’m choosing application/json, but you can choose another format, like xml, csv …
  • Finally, for calling the API, we need to use GetAsyc("api/Population") as mentioned above.

Adding View

In Home Controller, just right click on Index() action and select Add View. A new dialog will pop up. Write a name for your View and finally, click Add.

Adding View

Note

Don’t forget to download the following libraries from jqxwidgets.

  1. <!-- CSS -->  
  2. <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  3. <!-- JS -->  
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  6. <script src="~/Scripts/jqxcore.js"></script>  
  7. <script src="~/Scripts/jqxdata.js"></script>  
  8. <script src="~/Scripts/jqxtreemap.js"></script>  
  9. <script src="~/Scripts/jqxbuttons.js"></script>  
  10. <script src="~/Scripts/jqxangular.js"></script>  
  11. <script src="~/Scripts/demos.js"></script>  
Index cshtml
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5.   
  6.   
  7. @section scripts  
  8. {  
  9.     <!-- CSS -->  
  10. <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  11.     <!-- JS -->  
  12. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  13. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  14. <script src="~/Scripts/jqxcore.js"></script>  
  15. <script src="~/Scripts/jqxdata.js"></script>  
  16. <script src="~/Scripts/jqxtreemap.js"></script>  
  17. <script src="~/Scripts/jqxbuttons.js"></script>  
  18. <script src="~/Scripts/jqxangular.js"></script>  
  19. <script src="~/Scripts/demos.js"></script>  
  20.   
  21.   
  22.     <script type="text/javascript">  
  23.   
  24.         var myApp = angular.module('myApp', ["jqwidgets"]);  
  25.         myApp.controller('TreemapCtrl'function ($scope, $http) {  
  26.   
  27.             $scope.PopulationData;  
  28.             $scope.treeMapSettings;  
  29.   
  30.              
  31.   
  32.             $http.get("GetPopulationList").success(function (data) {  
  33.   
  34.                 $scope.PopulationData = data;  
  35.   
  36.             }).error(function (data) {  
  37.   
  38.                 console.log('Something Wrong');  
  39.   
  40.             });  
  41.               
  42.             $scope.treeMapSettings =  
  43.                 {  
  44.                     width: 800,  
  45.                     showLegend: false,  
  46.                     height: 400,  
  47.                     colorRange: 100,  
  48.                     colorMode: 'autoColors',  
  49.                     baseColor: '#52CBFF'  
  50.                 }  
  51.   
  52.         });  
  53.   
  54.   
  55.   
  56.     </script>  
  57.   
  58. }  
  59.   
  60. <h2>TreeMap directive for AngularJS </h2>  
  61.   
  62. <div ng-app="myApp" ng-controller="TreemapCtrl" style="margin-top:10px;">  
  63.   
  64.     <jqx-tree-map jqx-source="PopulationData" jqx-settings="treeMapSettings"></jqx-tree-map>  
  65.   
  66. </div>  
Output

 

Output


Similar Articles