AngularJS Chart Bar Series With ASP.NET MVC 5

Introduction

In this article, we will see how to bind data in Chart Bar series, using Web API2, AngularJS, and Entity Framework. In the following example, I want to display the population data, using AngularJS Chart plugin and Web API2.

Let’s go.

Prerequisites

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

SQL Database part

Here, you will 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_Population]    Script Date: 9/12/2016 7:08:26 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_Population](  
  15.     [ID] [intNOT NULL,  
  16.     [COUNTRY] [varchar](50) NULL,  
  17.     [POPULATION] [bigintNULL,  
  18.     [PERCENT] [decimal](18, 0) NULL,  
  19.  CONSTRAINT [PK_tbt_Population] PRIMARY KEY CLUSTERED   
  20. (  
  21.     [ID] ASC  
  22. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  23. ON [PRIMARY]  
  24. GO  
  25.   
  26. SET ANSI_PADDING OFF  
  27. GO  
After creating the table, you can add some records.

table

Create your MVC application

Now, open "Visual Studio" and select File >> New Project. A new window will pop up with the name New Project. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

MVC application

Next, select the Web API template in Template window, and click OK.

Web API

After creating the project, add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

First of all, right click on the project name and click Add >> Add New Item. A dialog box will pop up. Inside Visual C#, select Data >> ADO.NET Entity Data Model.

Now, enter name for your Dbcontext model as DbContextPopulation. Finally, click Add.

ADO.NET Entity Data Model

At this level, we are going to choose "EF Designer from database", as shown below.

EF Designer

In the following snapshot, we need to choose the data connection which should be used to connect to the database. If the connection doesn’t exist, create a new connection by clicking on the "New Connection" button.

connection

After clicking on Next, the Entity Data Model Wizard will pop up for choosing the object which we need to use. In our case, we are choosing tbt_Population table and click Finish. Finally, we see that EDMX model generates tbt_Population class.

class

Make sure that DbContextPopulation.edmx file figures in Solution Explorer panel.

DbContextPopulation

Create a Controller

Now, 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.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7.   
  8. namespace AngularJS_Chart_Bar_Series.Controllers  
  9. {  
  10.     public class PopulationController : ApiController  
  11.     {  
  12.         //Dbcontext  
  13.         private PopulationDBEntities context = new PopulationDBEntities();  
  14.   
  15.   
  16.         //Get All Population  
  17.         [HttpGet]  
  18.         public IEnumerable<tbt_Population> GetPopulation()  
  19.         {  
  20.             var PopulationList = context.tbt_Population.ToList().OrderBy(x => x.COUNTRY);  
  21.   
  22.             return PopulationList;  
  23.         }  
  24.   
  25.   
  26.     }  
  27. }  
Here, I’m creating GetPopulation() action which will select all data from tbt_Poplulation table.

Adding View

In Home Controller, right click on Index() action and select Add View. In the new pop-up, write a name for your View. 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/jqxcore.js"></script>  
  6. <script src="~/Scripts/jqxangular.js"></script>  
  7. <script src="~/Scripts/jqxdata.js"></script>  
  8. <script src="~/Scripts/jqxdraw.js"></script>  
  9. <script src="~/Scripts/jqxchart.core.js"></script>  
Index cshtml
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. @section scripts{  
  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/jqxcore.js"></script>  
  11. <script src="~/Scripts/jqxangular.js"></script>  
  12. <script src="~/Scripts/jqxdata.js"></script>  
  13. <script src="~/Scripts/jqxdraw.js"></script>  
  14. <script src="~/Scripts/jqxchart.core.js"></script>  
  15.   
  16.     <script type="text/javascript">  
  17.   
  18.         var myApp = angular.module('myApp', ["jqwidgets"]);  
  19.         myApp.controller('ChartCtrl', function ($scope) {  
  20.   
  21.             //prepare chart data as an array  
  22.             var source = {  
  23.                 datatype: 'json',  
  24.                 datafields: [  
  25.                     { name: 'COUNTRY' },  
  26.                     { name: 'POPULATION' },  
  27.                     { name: 'PERCENT' }  
  28.                 ],  
  29.                 url: 'api/Population/GetPopulation',  
  30.   
  31.             };  
  32.             var dataAdapter = new $.jqx.dataAdapter(source);  
  33.   
  34.             //prepare jqxchart Settings  
  35.   
  36.             var settings = {  
  37.   
  38.                 title: "Top 5 most populated countries",  
  39.                 description: "Statistics for 2011",  
  40.                 showLegend: true,  
  41.                 enableAnimations: true,  
  42.                 padding: { left: 20, top: 5, right: 20, bottom: 5 },  
  43.                 titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },  
  44.                 source: dataAdapter,  
  45.                 xAxis:  
  46.                         {  
  47.                             dataField: 'COUNTRY',  
  48.                             showGridLines: true,  
  49.                             flip: false  
  50.                         },  
  51.                 colorScheme: 'scheme01',  
  52.                 seriesGroups:  
  53.                     [  
  54.                         {  
  55.                             type: 'column',  
  56.                             orientation: 'horizontal',  
  57.                             columnsGapPercent: 100,  
  58.                             toolTipFormatSettings: { thousandsSeparator: ',' },  
  59.                             valueAxis:  
  60.                             {  
  61.                                 flip: true,  
  62.                                 unitInterval: 100000000,  
  63.                                 maxValue: 1500000000,  
  64.                                 displayValueAxis: true,  
  65.                                 description: '',  
  66.                                 formatFunction: function (value) {  
  67.                                     return parseInt(value / 1000000);  
  68.                                 }  
  69.                             },  
  70.                             series: [  
  71.                                     { dataField: 'POPULATION', displayText: 'Population (millions)' }  
  72.                             ]  
  73.                         }  
  74.                     ]  
  75.             };  
  76.   
  77.             $scope.chartSettings = settings;  
  78.         });  
  79.   
  80.           
  81.   
  82.   
  83.     </script>  
  84.   
  85.   
  86.   
  87. }  
  88.   
  89. <h2>AngularJS Chart Bar Series Example</h2>  
  90. <div ng-app="myApp" ng-controller="ChartCtrl">  
  91.     <jqx-chart id='chartContainer' jqx-settings="chartSettings" style="width: 850px; height: 500px"></jqx-chart>  
  92. </div>  
Output

 

Output

That's it. Please share your feedback and queries in the Comments section. 


Similar Articles