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
Create Table - USE [PopulationDB]
- GO
-
- /****** Object: Table [dbo].[tbt_Population] Script Date: 9/12/2016 7:08:26 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[tbt_Population](
- [ID] [int] NOT NULL,
- [COUNTRY] [varchar](50) NULL,
- [POPULATION] [bigint] NULL,
- [PERCENT] [decimal](18, 0) NULL,
- CONSTRAINT [PK_tbt_Population] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
-
- SET ANSI_PADDING OFF
- GO
After creating the table, you can add some records.
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.
Next, select the Web API template in Template window, and click OK.
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.
At this level, we are going to choose "EF Designer from database", as shown below.
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.
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.
Make sure that DbContextPopulation.edmx file figures in Solution Explorer panel.
Create a Controller
Now, create a Controller. Right click on the "Controllers" folder > Add > Controller> selecting Web API 2 Controller – Empty > click Add.
Enter Controller name (‘PopulationController’).
PopulationController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace AngularJS_Chart_Bar_Series.Controllers
- {
- public class PopulationController : ApiController
- {
-
- private PopulationDBEntities context = new PopulationDBEntities();
-
-
-
- [HttpGet]
- public IEnumerable<tbt_Population> GetPopulation()
- {
- var PopulationList = context.tbt_Population.ToList().OrderBy(x => x.COUNTRY);
-
- return PopulationList;
- }
-
-
- }
- }
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.
Note:
Don’t forget to download the following libraries from jqxwidgets.
- <!-- CSS -->
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
- <!-- JS -->
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxdraw.js"></script>
- <script src="~/Scripts/jqxchart.core.js"></script>
Index cshtml
- @{
- ViewBag.Title = "Index";
- }
-
- @section scripts{
-
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
-
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxdraw.js"></script>
- <script src="~/Scripts/jqxchart.core.js"></script>
-
- <script type="text/javascript">
-
- var myApp = angular.module('myApp', ["jqwidgets"]);
- myApp.controller('ChartCtrl', function ($scope) {
-
- //prepare chart data as an array
- var source = {
- datatype: 'json',
- datafields: [
- { name: 'COUNTRY' },
- { name: 'POPULATION' },
- { name: 'PERCENT' }
- ],
- url: 'api/Population/GetPopulation',
-
- };
- var dataAdapter = new $.jqx.dataAdapter(source);
-
- //prepare jqxchart Settings
-
- var settings = {
-
- title: "Top 5 most populated countries",
- description: "Statistics for 2011",
- showLegend: true,
- enableAnimations: true,
- padding: { left: 20, top: 5, right: 20, bottom: 5 },
- titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
- source: dataAdapter,
- xAxis:
- {
- dataField: 'COUNTRY',
- showGridLines: true,
- flip: false
- },
- colorScheme: 'scheme01',
- seriesGroups:
- [
- {
- type: 'column',
- orientation: 'horizontal',
- columnsGapPercent: 100,
- toolTipFormatSettings: { thousandsSeparator: ',' },
- valueAxis:
- {
- flip: true,
- unitInterval: 100000000,
- maxValue: 1500000000,
- displayValueAxis: true,
- description: '',
- formatFunction: function (value) {
- return parseInt(value / 1000000);
- }
- },
- series: [
- { dataField: 'POPULATION', displayText: 'Population (millions)' }
- ]
- }
- ]
- };
-
- $scope.chartSettings = settings;
- });
-
-
-
-
- </script>
-
-
-
- }
-
- <h2>AngularJS Chart Bar Series Example</h2>
- <div ng-app="myApp" ng-controller="ChartCtrl">
- <jqx-chart id='chartContainer' jqx-settings="chartSettings" style="width: 850px; height: 500px"></jqx-chart>
- </div>
Output
That's it. Please share your feedback and queries in the Comments section.