Introduction

In this article, we will learn, how to use column series chart, using Web API2, AngularJS, and ADO.NET Framework.

Prerequisites

As I said before, we are going to use jqwidgets 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 find the scripts to create the database and the table.

Create database

  1. USE [master]
  2. GO
  3. /****** Object: Database [DataSys] Script Date: 9/17/2016 8:15:45 AM ******/
  4. CREATE DATABASE [DataSys]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. ( NAME = N'DataSys', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DataSys.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOG ON
  9. ( NAME = N'DataSys_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DataSys_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  10. GO
  11. ALTER DATABASE [DataSys] SET COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  14. begin
  15. EXEC [DataSys].[dbo].[sp_fulltext_database] @action = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE [DataSys] SET ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE [DataSys] SET ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE [DataSys] SET ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE [DataSys] SET ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE [DataSys] SET ARITHABORT OFF
  27. GO
  28. ALTER DATABASE [DataSys] SET AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE [DataSys] SET AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [DataSys] SET AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE [DataSys] SET AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE [DataSys] SET CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE [DataSys] SET CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE [DataSys] SET CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE [DataSys] SET NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE [DataSys] SET QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE [DataSys] SET RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE [DataSys] SET DISABLE_BROKER
  49. GO
  50. ALTER DATABASE [DataSys] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE [DataSys] SET DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE [DataSys] SET TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE [DataSys] SET ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE [DataSys] SET PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE [DataSys] SET READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE [DataSys] SET HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE [DataSys] SET RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE [DataSys] SET MULTI_USER
  67. GO
  68. ALTER DATABASE [DataSys] SET PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE [DataSys] SET DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE [DataSys] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  73. GO
  74. ALTER DATABASE [DataSys] SET TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE [DataSys] SET READ_WRITE
  77. GO
Create table
  1. USE [DataSys]
  2. GO
  3. /****** Object: Table [dbo].[ChartKEG] Script Date: 9/17/2016 8:16:06 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].[ChartKEG](
  11. [ID] [int] NOT NULL,
  12. [Day] [varchar](50) NULL,
  13. [Keith] [int] NULL,
  14. [Erica] [int] NULL,
  15. [George] [int] NULL,
  16. CONSTRAINT [PK_ChartKEG] PRIMARY KEY CLUSTERED
  17. (
  18. [ID] ASC
  19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  20. ) ON [PRIMARY]
  21. GO
  22. SET ANSI_PADDING OFF
  23. GO
After creating the table, you can add some records, as shown below-



Create your MVC application

Open Visual Studio and select file, click new project, a new dialog will pop up with the name New Project. Select ASP.NET Web Application (.NET Framework), name your project and click OK button.



Now, new dialog will pop up to select the template. We are going to choose Web API and click OK.



After creating our project, we will proceed to create Web API2 controller.

Create a controller

Now, we are going to create a controller. Right click on the controllers folder > Add > Controller> select Web API 2 Controller – Empty > click Add.



Enter Controller name (‘ChartController’).



ChartController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Web.Http;
  8. using Chart_AngularJS.Models;
  9. namespace Chart_AngularJS.Controllers
  10. {
  11. public class ChartController : ApiController
  12. {
  13. [HttpGet]
  14. public List<ChartModel> GetDataList()
  15. {
  16. SqlConnection conx = new SqlConnection("Data Source=.;Initial Catalog=DataSys;Integrated Security=True");
  17. conx.Open();
  18. SqlCommand cmd = new SqlCommand("SELECT * FROM ChartKEG", conx);
  19. List<ChartModel> listData = new List<Models.ChartModel>();
  20. SqlDataReader dr = cmd.ExecuteReader();
  21. while (dr.Read())
  22. {
  23. ChartModel chart = new Models.ChartModel();
  24. chart.Day = dr[1].ToString();
  25. chart.Keith = int.Parse(dr[2].ToString());
  26. chart.Erica = int.Parse(dr[3].ToString());
  27. chart.George = int.Parse(dr[4].ToString());
  28. listData.Add(chart);
  29. }
  30. conx.Close();
  31. return listData;
  32. }
  33. }
  34. }
Here, I’m creating GetDataList() action to retrieve all the data from Chart KEG table.

For this action, I’m using ADO.NET framework instead of Entity Framework. First of all, we need to declare SqlConnection object, which allows us to connect to the database (in our case Datasys). We should use SqlCommand object, which takes two parameters respectively , which are query string and connection object. We need to declare SqlDataReader, which receives the records after executing ExecuteReader() method. Finally, we use while to loop all the records.

Here, you find the definition of ChartModel class.

ChartModel.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace Chart_AngularJS.Models
  6. {
  7. public class ChartModel
  8. {
  9. public string Day { get; set; }
  10. public int Keith { get; set; }
  11. public int Erica { get; set; }
  12. public int George { get; set; }
  13. }
  14. }
HomeController.cs
  1. using Chart_AngularJS.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace Chart_AngularJS.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. public ActionResult Index()
  13. {
  14. ViewBag.Title = "Home Page";
  15. return View();
  16. }
  17. IEnumerable<ChartModel> PopulationList = Enumerable.Empty<ChartModel>();
  18. [HttpGet]
  19. public JsonResult GetChartList()
  20. {
  21. HttpClient client = new HttpClient();
  22. client.BaseAddress = new Uri("http://localhost:49487/");
  23. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  24. HttpResponseMessage response = client.GetAsync("api/Chart").Result;
  25. if (response.IsSuccessStatusCode)
  26. {
  27. PopulationList = response.Content.ReadAsAsync<List<ChartModel>>().Result;
  28. }
  29. return Json(PopulationList, JsonRequestBehavior.AllowGet);
  30. }
  31. }
  32. }
As you can see, I am creating GetChartList() action, which calls our API.

To call our API, you need to-

Adding View

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



Note - Don’t forget to download the libraries, given below, 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/jqxangular.js"></script>
  6. <script src="~/Scripts/jqxcore.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 = "Home Page";
  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/jqxangular.js"></script>
  10. <script src="~/Scripts/jqxcore.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 demoApp = angular.module("myApp", ["jqwidgets"]);
  16. demoApp.controller("ChartCtrl", ['$scope', function ($scope) {
  17. //prepare chart data as an array
  18. var source = {
  19. datatype: 'json',
  20. datafields: [
  21. { name: 'Day' },
  22. { name: 'Keith' },
  23. { name: 'Erica' },
  24. { name: 'George' }
  25. ],
  26. url: 'GetChartList',
  27. };
  28. var dataAdapter = new $.jqx.dataAdapter(source);
  29. // prepare jqxChart settings
  30. var settings = {
  31. title: "Fitness & exercise weekly scorecard",
  32. description: "Time spent in vigorous exercise",
  33. enableAnimations: true,
  34. showLegend: true,
  35. padding: { left: 5, top: 5, right: 5, bottom: 5 },
  36. titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
  37. source: dataAdapter,
  38. xAxis:
  39. {
  40. dataField: 'Day',
  41. showGridLines: true
  42. },
  43. colorScheme: 'scheme01',
  44. seriesGroups:
  45. [
  46. {
  47. type: 'column',
  48. columnsGapPercent: 50,
  49. seriesGapPercent: 0,
  50. valueAxis:
  51. {
  52. unitInterval: 10,
  53. minValue: 0,
  54. maxValue: 100,
  55. displayValueAxis: true,
  56. description: 'Time in minutes',
  57. axisSize: 'auto',
  58. tickMarksColor: '#888888'
  59. },
  60. series: [
  61. { dataField: 'Keith', displayText: 'Keith' },
  62. { dataField: 'Erica', displayText: 'Erica' },
  63. { dataField: 'George', displayText: 'George' }
  64. ]
  65. }
  66. ]
  67. };
  68. $scope.chartSettings = settings;
  69. }]);
  70. </script>
  71. }
  72. <div ng-app="myApp" ng-controller="ChartCtrl">
  73. <jqx-chart id='chartContainer' jqx-settings="chartSettings" style="width: 850px; height: 500px"></jqx-chart>
  74. </div>
Output-