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. /****** Object: Database [PopulationDB] Script Date: 9/12/2016 8:54:39 AM ******/
  4. CREATE DATABASE [PopulationDB]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. ( NAME = N'PopulationDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PopulationDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOG ON
  9. ( 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%)
  10. GO
  11. ALTER DATABASE [PopulationDB] SET COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  14. begin
  15. EXEC [PopulationDB].[dbo].[sp_fulltext_database] @action = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE [PopulationDB] SET ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE [PopulationDB] SET ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE [PopulationDB] SET ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE [PopulationDB] SET ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE [PopulationDB] SET ARITHABORT OFF
  27. GO
  28. ALTER DATABASE [PopulationDB] SET AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE [PopulationDB] SET AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [PopulationDB] SET AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE [PopulationDB] SET AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE [PopulationDB] SET CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE [PopulationDB] SET CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE [PopulationDB] SET CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE [PopulationDB] SET NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE [PopulationDB] SET QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE [PopulationDB] SET RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE [PopulationDB] SET DISABLE_BROKER
  49. GO
  50. ALTER DATABASE [PopulationDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE [PopulationDB] SET DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE [PopulationDB] SET TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE [PopulationDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE [PopulationDB] SET PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE [PopulationDB] SET READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE [PopulationDB] SET HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE [PopulationDB] SET RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE [PopulationDB] SET MULTI_USER
  67. GO
  68. ALTER DATABASE [PopulationDB] SET PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE [PopulationDB] SET DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE [PopulationDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  73. GO
  74. ALTER DATABASE [PopulationDB] SET TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE [PopulationDB] SET READ_WRITE
  77. GO
Create Table
  1. USE [PopulationDB]
  2. GO
  3. /****** Object: Table [dbo].[tbt_populations] Script Date: 9/13/2016 8:20:32 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[tbt_populations](
  11. [ID] [int] NOT NULL,
  12. [COUNTRY] [varchar](50) NULL,
  13. [POPULATION] [bigint] NULL,
  14. CONSTRAINT [PK_tbt_populations] PRIMARY KEY CLUSTERED
  15. (
  16. [ID] ASC
  17. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  18. ) ON [PRIMARY]
  19. GO
  20. SET ANSI_PADDING OFF
  21. 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. namespace TreeMap_For_AngularJS.Controllers
  10. {
  11. public class PopulationController : ApiController
  12. {
  13. //DbContext
  14. private PopulationsDBEntities context = new PopulationsDBEntities();
  15. [HttpGet]
  16. public IEnumerable<PopulationModel> GetPopulation()
  17. {
  18. var PopulationList = context.tbt_populations.Select(p => new PopulationModel { label = p.COUNTRY, value = p.POPULATION });
  19. return PopulationList.ToList();
  20. }
  21. }
  22. }
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. namespace TreeMap_For_AngularJS.Models
  6. {
  7. public class PopulationModel
  8. {
  9. public string label { get; set; }
  10. public long value { get; set; }
  11. }
  12. }
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. namespace TreeMap_For_AngularJS.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. IEnumerable<PopulationModel> PopulationList = Enumerable.Empty<PopulationModel>();
  17. [HttpGet]
  18. public JsonResult GetPopulationList()
  19. {
  20. HttpClient client = new HttpClient();
  21. client.BaseAddress = new Uri("http://localhost:56720/");
  22. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  23. HttpResponseMessage response = client.GetAsync("api/Population").Result;
  24. if (response.IsSuccessStatusCode)
  25. {
  26. PopulationList = response.Content.ReadAsAsync<IEnumerable<PopulationModel>>().Result;
  27. }
  28. return Json(PopulationList, JsonRequestBehavior.AllowGet);
  29. }
  30. }
  31. }
As you can see, I’m creating GetPopulationList() action which calls our API.

For calling our API, you need to,

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

Output