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
Create table
- USE [DataSys]
- GO
-
- /****** Object: Table [dbo].[ChartKEG] Script Date: 9/17/2016 8:16:06 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[ChartKEG](
- [ID] [int] NOT NULL,
- [Day] [varchar](50) NULL,
- [Keith] [int] NULL,
- [Erica] [int] NULL,
- [George] [int] NULL,
- CONSTRAINT [PK_ChartKEG] 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, 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
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using Chart_AngularJS.Models;
-
- namespace Chart_AngularJS.Controllers
- {
- public class ChartController : ApiController
- {
- [HttpGet]
- public List<ChartModel> GetDataList()
- {
-
- SqlConnection conx = new SqlConnection("Data Source=.;Initial Catalog=DataSys;Integrated Security=True");
-
- conx.Open();
-
- SqlCommand cmd = new SqlCommand("SELECT * FROM ChartKEG", conx);
- List<ChartModel> listData = new List<Models.ChartModel>();
- SqlDataReader dr = cmd.ExecuteReader();
-
- while (dr.Read())
- {
- ChartModel chart = new Models.ChartModel();
- chart.Day = dr[1].ToString();
- chart.Keith = int.Parse(dr[2].ToString());
- chart.Erica = int.Parse(dr[3].ToString());
- chart.George = int.Parse(dr[4].ToString());
-
- listData.Add(chart);
- }
-
- conx.Close();
-
-
- return listData;
- }
-
- }
- }
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace Chart_AngularJS.Models
- {
- public class ChartModel
- {
-
- public string Day { get; set; }
- public int Keith { get; set; }
- public int Erica { get; set; }
- public int George { get; set; }
-
- }
- }
HomeController.cs
- using Chart_AngularJS.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Web;
- using System.Web.Mvc;
-
- namespace Chart_AngularJS.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- ViewBag.Title = "Home Page";
-
- return View();
- }
-
- IEnumerable<ChartModel> PopulationList = Enumerable.Empty<ChartModel>();
-
- [HttpGet]
- public JsonResult GetChartList()
- {
-
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:49487/");
- client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
-
- HttpResponseMessage response = client.GetAsync("api/Chart").Result;
-
- if (response.IsSuccessStatusCode)
- {
- PopulationList = response.Content.ReadAsAsync<List<ChartModel>>().Result;
- }
-
-
- return Json(PopulationList, JsonRequestBehavior.AllowGet);
-
-
- }
- }
- }
As you can see, I am creating GetChartList() action, which calls our API.
To call our API, you need to-
- Create an object from HttpClient class.
- Specify URL of our API (in this example, the URL used is: http://localhost:49487/ ).
- The header of request. I’m choosing application/json, but you can choose another format like XML, CSV etc.
- Finally, to call API, we need to use GetAsyc("api/Chart"), as mentioned above.
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-
- <!-- 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/jqxangular.js"></script>
- <script src="~/Scripts/jqxcore.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 = "Home Page";
- }
-
-
- @section scripts{
-
- <!-- 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/jqxangular.js"></script>
- <script src="~/Scripts/jqxcore.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 demoApp = angular.module("myApp", ["jqwidgets"]);
- demoApp.controller("ChartCtrl", ['$scope', function ($scope) {
-
-
-
-
- var source = {
- datatype: 'json',
- datafields: [
- { name: 'Day' },
- { name: 'Keith' },
- { name: 'Erica' },
- { name: 'George' }
- ],
- url: 'GetChartList',
-
- };
- var dataAdapter = new $.jqx.dataAdapter(source);
-
-
-
- var settings = {
- title: "Fitness & exercise weekly scorecard",
- description: "Time spent in vigorous exercise",
- enableAnimations: true,
- showLegend: true,
- padding: { left: 5, top: 5, right: 5, bottom: 5 },
- titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
- source: dataAdapter,
- xAxis:
- {
- dataField: 'Day',
- showGridLines: true
- },
- colorScheme: 'scheme01',
- seriesGroups:
- [
- {
- type: 'column',
- columnsGapPercent: 50,
- seriesGapPercent: 0,
- valueAxis:
- {
- unitInterval: 10,
- minValue: 0,
- maxValue: 100,
- displayValueAxis: true,
- description: 'Time in minutes',
- axisSize: 'auto',
- tickMarksColor: '#888888'
- },
- series: [
- { dataField: 'Keith', displayText: 'Keith' },
- { dataField: 'Erica', displayText: 'Erica' },
- { dataField: 'George', displayText: 'George' }
- ]
- }
- ]
- };
-
- $scope.chartSettings = settings;
- }]);
- </script>
-
-
- }
-
- <div ng-app="myApp" ng-controller="ChartCtrl">
-
- <jqx-chart id='chartContainer' jqx-settings="chartSettings" style="width: 850px; height: 500px"></jqx-chart>
-
- </div>
Output-