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. /****** 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_Population] Script Date: 9/12/2016 7:08:26 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_Population](
  11. [ID] [int] NOT NULL,
  12. [COUNTRY] [varchar](50) NULL,
  13. [POPULATION] [bigint] NULL,
  14. [PERCENT] [decimal](18, 0) NULL,
  15. CONSTRAINT [PK_tbt_Population] PRIMARY KEY CLUSTERED
  16. (
  17. [ID] ASC
  18. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  19. ) ON [PRIMARY]
  20. GO
  21. SET ANSI_PADDING OFF
  22. 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. namespace AngularJS_Chart_Bar_Series.Controllers
  8. {
  9. public class PopulationController : ApiController
  10. {
  11. //Dbcontext
  12. private PopulationDBEntities context = new PopulationDBEntities();
  13. //Get All Population
  14. [HttpGet]
  15. public IEnumerable<tbt_Population> GetPopulation()
  16. {
  17. var PopulationList = context.tbt_Population.ToList().OrderBy(x => x.COUNTRY);
  18. return PopulationList;
  19. }
  20. }
  21. }
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. @section scripts{
  5. <!-- CSS -->
  6. <link href="~/Content/jqx.base.css" rel="stylesheet" />
  7. <!-- JS -->
  8. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  9. <script src="~/Scripts/jqxcore.js"></script>
  10. <script src="~/Scripts/jqxangular.js"></script>
  11. <script src="~/Scripts/jqxdata.js"></script>
  12. <script src="~/Scripts/jqxdraw.js"></script>
  13. <script src="~/Scripts/jqxchart.core.js"></script>
  14. <script type="text/javascript">
  15. var myApp = angular.module('myApp', ["jqwidgets"]);
  16. myApp.controller('ChartCtrl', function ($scope) {
  17. //prepare chart data as an array
  18. var source = {
  19. datatype: 'json',
  20. datafields: [
  21. { name: 'COUNTRY' },
  22. { name: 'POPULATION' },
  23. { name: 'PERCENT' }
  24. ],
  25. url: 'api/Population/GetPopulation',
  26. };
  27. var dataAdapter = new $.jqx.dataAdapter(source);
  28. //prepare jqxchart Settings
  29. var settings = {
  30. title: "Top 5 most populated countries",
  31. description: "Statistics for 2011",
  32. showLegend: true,
  33. enableAnimations: true,
  34. padding: { left: 20, top: 5, right: 20, bottom: 5 },
  35. titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
  36. source: dataAdapter,
  37. xAxis:
  38. {
  39. dataField: 'COUNTRY',
  40. showGridLines: true,
  41. flip: false
  42. },
  43. colorScheme: 'scheme01',
  44. seriesGroups:
  45. [
  46. {
  47. type: 'column',
  48. orientation: 'horizontal',
  49. columnsGapPercent: 100,
  50. toolTipFormatSettings: { thousandsSeparator: ',' },
  51. valueAxis:
  52. {
  53. flip: true,
  54. unitInterval: 100000000,
  55. maxValue: 1500000000,
  56. displayValueAxis: true,
  57. description: '',
  58. formatFunction: function (value) {
  59. return parseInt(value / 1000000);
  60. }
  61. },
  62. series: [
  63. { dataField: 'POPULATION', displayText: 'Population (millions)' }
  64. ]
  65. }
  66. ]
  67. };
  68. $scope.chartSettings = settings;
  69. });
  70. </script>
  71. }
  72. <h2>AngularJS Chart Bar Series Example</h2>
  73. <div ng-app="myApp" ng-controller="ChartCtrl">
  74. <jqx-chart id='chartContainer' jqx-settings="chartSettings" style="width: 850px; height: 500px"></jqx-chart>
  75. </div>
Output

Output

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