Highcharts With Angular Custom Directive And Web API

This article will demonstrate how to create charts using Highcharts Library in Angular JS with Web API.

We already know that DotNet Highcharts library is a client-side library for charting and we can create very clean and flexible charts with minimum configuration. Using Highcharts, we can create probably all types of charts which we use in daily life, it could be a line chart, it could be a bar chart or even a column chart.

Charts must be a reusable component in any application so that we can reuse it at different places. That’s why here, we are creating a generic custom directive in AngularJS for charts, which will render different types of charts based on the type of the chart.

Angular

In this demonstration, we will use SQL Server for data storage which will be fetched by Web API. At client-side, we are using AngularJS where service fetches the data from API and share with Custom Directive which will responsible to render charts.

This demonstration will cover up three different topics and it seems that if we cover up all topics in single article that will be very long and boring. So, we will divide it in three different parts as following.

  • Part 1: Create charts using Highcharts Library and AngularJS custom directive with Web API
  • Part 2: Implement Drilldown functionality with charts [Nested charts]
  • Part 3: Show loading image/message when rendering the charts

So, let’s move to practical implementation of PART 1 and create charts. To complete this demonstration, there are several tasks which need to be completed.

  • TASK 1: Create Database, tables and insert scripts in SQL Server.
  • TASK 2: Create Asp.Net MVC application with Web API
  • TASK 3: Add Ado.Net Entity Data Model to fetch data from server in API
  • TASK 4: Create client side application using Angular JS and implement Highcharts
  • TASK 5: Create services and controller to fetch data from API
  • TASK 6: Create generic custom directive to render charts

Above is the objective of this article which will be covered  step by step as following.

TASK 1 - Create Database tables and insert scripts in SQL Server.

Open SQL Server Management Studio and create a database named “Summit” as well as, two more tables named “Batsmen” and “BatsmenRuns”. These tables will store the data about Top Indian Batsmen’s runs. So, just execute the following scripts to create these two tables and their data.

  1. CREATE DATABASE Summit  
  2. Go  
  3.   
  4. USE Summit  
  5.   
  6.   
  7. --Table which will store the total scored runs by top india batsmen   
  8. CREATE TABLE Batsmen (ID INT PRIMARY KEY IDENTITY(1,1), Name VARCHAR(255) NOT NULL, TotalMatches INT NOT NULL, TotalRuns INT NOT NULL)  
  9.   
  10. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('Sachin Tendulkar',463, 18426);  
  11. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('Yuvraj Singh', 304, 8701 );  
  12. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('M S Dhoni', 306, 9758 );  
  13. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('Rahul Dravid',344, 10889 );  
  14. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('Sourav Ganguly',311, 11363 );  
  15. INSERT INTO Batsmen (Name,TotalMatches,TotalRuns) VALUES('Virender Sehwag',251, 8273);  
  16.   
  17.   
  18. --Table which will store the scored runs in respective years  
  19. CREATE TABLE BatsmenRuns (ID INT PRIMARY KEY IDENTITY(1,1), Name VARCHAR(255) NOT NULLYear INT NOT NULL, Runs INT NOT NULL)  
  20.   
  21. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1989, 0   );  
  22. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1990, 239 );  
  23. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1991, 417 );  
  24. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1992, 704 );  
  25. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1993, 319 );  
  26. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1994, 108 );  
  27. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1995, 444 );  
  28. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1996, 161 );  
  29. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1997, 101 );  
  30. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1998, 189 );  
  31. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',1999, 843 );  
  32. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2000, 132 );  
  33. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2001, 904 );  
  34. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2002, 741 );  
  35. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2003, 114 );  
  36. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2004, 812 );  
  37. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2005, 412 );  
  38. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2006, 628 );  
  39. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2007, 142 );  
  40. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2008, 460 );  
  41. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2009, 972 );  
  42. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2010, 204 );  
  43. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2011, 513 );  
  44. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sachin Tendulkar',2012, 315 );  
  45.   
  46.   
  47. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2000,260 );  
  48. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2001,238 );  
  49. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2002,659 );  
  50. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2003,600 );  
  51. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2004,841 );  
  52. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2005,839 );  
  53. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2006,849 );  
  54. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2007,1287)  
  55. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2008,893 );  
  56. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2009,783 );  
  57. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2010,349 );  
  58. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2011,453 );  
  59. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2012,2 );  
  60. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2013,276 );  
  61. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Yuvraj Singh',2017,372 );  
  62.   
  63. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2004,19 );  
  64. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2005,895 );  
  65. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2006,821 );  
  66. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2007,1103);  
  67. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2008,1097);  
  68. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2009,1198);  
  69. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2010,600 );  
  70. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2011,764 );  
  71. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2012,524 );  
  72. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2013,753 );  
  73. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2014,418 );  
  74. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2015,640 );  
  75. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2016,278 );  
  76. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('M S Dhoni',2017,648 );  
  77.   
  78. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',1996,475 );  
  79. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',1997,951 );  
  80. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',1998,283 );  
  81. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',1999,1761);  
  82. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2000,980 );  
  83. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2001,740 );  
  84. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2002,913 );  
  85. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2003,623 );  
  86. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2004,1025);  
  87. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2005,1092);  
  88. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2006,919 );  
  89. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2007,823 );    
  90. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2009,180 );    
  91. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Rahul Dravid',2011,124 );  
  92.   
  93.   
  94. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',1992, 3 );     
  95. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',1996, 269 );  
  96. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',1997, 1338);  
  97. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',1998, 1328);  
  98. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',1999, 1767);  
  99. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2000, 1579);  
  100. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2001, 813 );  
  101. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2002, 1114 );  
  102. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2003, 756 );  
  103. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2004, 947 );  
  104. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2005, 209 );  
  105. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Sourav Ganguly',2007, 1240 );  
  106.   
  107. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',1999, 1 );  
  108. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2000, 19 );  
  109. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2001, 439 );  
  110. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2002, 1130);  
  111. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2003, 871 );  
  112. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2004, 671 );  
  113. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2005, 1017);  
  114. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2006, 608 );  
  115. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2007, 475 );  
  116. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2008, 893 );  
  117. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2009, 810 );  
  118. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2010, 446 );  
  119. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2011, 645 );  
  120. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2012, 217 );  
  121. INSERT INTO BatsmenRuns (Name,Year,Runs) VALUES('Virender Sehwag',2013, 31 ); 
TASK 2  - Create ASP.NET MVC application with Web API

For this demonstration, we are using Visual Studio 2015 but you can use 2013 and 2017 as well. Open Visual Studio 2015 and go to File >> New >> Project. From the installed template, choose Visual C# -> Web -> ASP.NET Web Application. Just add a suitable name and click OK.

Angular

The next window will ask to choose the efficient template for the application. So here, we are choosing MVC with Web API with No Authentication.

Angular

TASK 3 - Add ADO.NET Entity Data Model to fetch the data from Server in API

Our application is ready, so now, it’s time to implement ADO.NET Entity Data Model for fetching the records from SQL Server from Web API Controller.

From the Solution Explorer, right click on project -> Add -> New Item.

It will open a window as follows. From here, we need to choose ADO.NET Entity Data Model and provide the perfect name and click Add.

angular

Once we click to Add, it will open next window as Wizard where we have to perform several processes to make connection from database. So, from here, just choose EF Designer from Database. Then, click Next.

angular

The next window will ask you to create a new connection with SQL Server. Choose your SQL Server name and database and click Next. It will now give you the list of all tables associated with that database. You have to select both of them which we have created earlier and click Finish.

It will create a data model for tables as below.

angular

Now, we have to create a Web API Controller which will fetch the data. So, to do that, right click on Controllers folder from the solution explorer and Add -> Controller.

We have to choose “Web API 2 Controller – Empty” and click Add. On the next window, just pass the Controller name as “CricketController” and click on "Add" again.

angular

Now, our API Controller is ready. So, you have to add the following code with that Controller.

  1. using System.Linq;  
  2. using System.Web.Http;  
  3.   
  4. namespace HighchartWithAngularJS.Controllers  
  5. {  
  6.     public class CricketController : ApiController  
  7.     {  
  8.         [Route("Cricket/TopBatsmenInODI")]  
  9.         [HttpGet]  
  10.         public IHttpActionResult GetTopBatsmenInODI()  
  11.         {  
  12.             try  
  13.             {  
  14.                 using (var db = new BatsmenEntities())  
  15.                 {  
  16.                     var topBatsmenRuns = db.Batsmen.ToList();  
  17.                     var result = topBatsmenRuns;  
  18.                     return Ok(result);  
  19.                 }  
  20.             }  
  21.             catch (System.Exception)  
  22.             {  
  23.                 throw;  
  24.             }  
  25.         }  
  26.     }  

Above, “GetTopBatsmenInODI” action method will get all top Indian batsmen in ODI from the database.

TASK 4 - Create a client-side application using AngularJS and implement Highcharts

Before going to create an AngularJS application, we will install AngularJS and Highcharts from the NuGet Package Manager Console using the following commands.

  1. Install-Package AngularJS  
  2. Install-Package DotNet.Highcharts  

angular

Once you install these client-side libraries with you project, your scripts folder will look like below.

angular

We are done with the data part above and installation also. Now, we have to create a client-side AngularJS application which will implement Highcharts. So, create a folder named as “App” in the same solution and add files as following image shows.

angular

TASK 5 - Create services and controller to fetch data from API

To start with AngularJS application, first, create “app.js” which will create a module for your application. I have given it name as “chartApp”. This name should be defined with ng-app=” chartApp” on _layout.cshtml.

APP.JS

  1. (function(){  
  2.     'use strict';  
  3.   
  4.     var app = angular.module("chartApp", []);  
  5. })();  

To get the data from API, we have created ChartSerrvice factory as service which will return the top batsmen's record.

CHARTSERVICE.JS

  1. (function (app) {  
  2.     'use strict'  
  3.   
  4.     app.factory('ChartService', ['$http', ChartService]);  
  5.   
  6.     function ChartService($http) {  
  7.   
  8.         var getTopBatsmenInODI = function () {  
  9.           return $http.get('/Cricket/TopBatsmenInODI');  
  10.         };  
  11.         return {  
  12.             topBatsmenInODI: getTopBatsmenInODI              
  13.         }  
  14.     };  
  15. })(angular.module("chartApp"));  

To fetch the data from service and share with chart, we have to create one “ChartController”. It will get the records and bind with scope variable which will be accessible on UI.

CHARTCONTROLLER.JS

  1. (function(app){  
  2.     'use strict'  
  3.   
  4.     app.controller('ChartController', ['$scope''ChartService', ChartController]);  
  5.   
  6.     function ChartController($scope, ChartService) {  
  7.           
  8.       ChartService.topBatsmenInODI().then(function (response) {  
  9.             $scope.TopBatsmenInODIList = response.data;           
  10.         });          
  11.     };  
  12. })(angular.module("chartApp")); 

TASK 6 - Create generic custom directive to render the charts

Before moving to create Chart Custom Directive to render charts, we would like to inform that we are changing the templates dynamically based on type of the chart respectively using the following lines of code.

  1. templateUrl: function (element, attrs) {  
  2.     return "/App/directive/" + attrs.type + 'ChartTemplate.html'  
  3. },  

Chart always renders inside the container, so every time, we are changing the container as well.

  1. chart: {  
  2.                         type: scope.type,  
  3.                         renderTo: 'chart_container_' + scope.type                        
  4.                     }, 

From the UI, we are only passing the type of the chart and data which should be use to populate the series of the charts.

  1. scope: {  
  2.                 type: '@',  
  3.                 data: '='  
  4.             },  

Following is the whole code to generate charts using Highcharts.

CHARDIRECTIVE.JS

  1. (function (app) {  
  2.     'use strict'  
  3.   
  4.     app.directive('chartDirective', ['$timeout', chartDirective]);  
  5.   
  6.     function chartDirective($timeout) {  
  7.         return {  
  8.             restrict: 'E',  
  9.             templateUrl: function (element, attrs) {  
  10.                 return "/App/directive/" + attrs.type + 'ChartTemplate.html'  
  11.             },  
  12.             scope: {  
  13.                 type: '@',  
  14.                 data: '='  
  15.             },  
  16.             link: function (scope, element, attribute) {  
  17.                 scope.chartConfig = new Highcharts.Chart({  
  18.                     chart: {  
  19.                         type: scope.type,  
  20.                         renderTo: 'chart_container_' + scope.type                        
  21.                     },  
  22.                     title: {  
  23.                         text: 'Top ODI Batsman in India'  
  24.                     },  
  25.   
  26.                     xAxis: {  
  27.                         type: 'category',  
  28.                         categories: []  
  29.                     },  
  30.                     yAxis: {  
  31.                         title: {  
  32.                             text: 'Runs'  
  33.                         },  
  34.                         plotLines: [{  
  35.                             value: 0,  
  36.                             width: 1,  
  37.                             color: '#808080'  
  38.                         }]  
  39.                     },  
  40.                     tooltip: {  
  41.                         valueSuffix: ' Run Scored'  
  42.                     }  
  43.                 });  
  44.   
  45.                 scope.seriesData = [];  
  46.                 scope.categories = [];  
  47.                                
  48.                 scope.$watch('data'function (newValue) {  
  49.                     if (newValue != undefined) {  
  50.   
  51.                         _.forEach(scope.data, function (item) {  
  52.                             scope.categories.push(item.Name);  
  53.                             scope.seriesData.push({ name: item.Name, y: item.TotalRuns, drilldown: item.Name });  
  54.                         });  
  55.   
  56.                         $timeout(function () {  
  57.                             scope.chartConfig.addSeries({  
  58.                                 name: 'Top ODI Batsman',  
  59.                                 data: scope.seriesData  
  60.                             });  
  61.                              
  62.                         }, 2000);  
  63.                     }  
  64.                 });  
  65.   
  66.             },  
  67.             controller: function ($scope, ChartService) {  
  68.                 
  69.             }  
  70.         }  
  71.     }  
  72. })(angular.module("chartApp"));  

We are using different templates to render charts based on their types. If you would like to add many charts then you will need to create templates as well and also make one entry on Index.cshtml page [Go to below].

LINECAHRTTEMPLATE.HTML

  1. <div class="col-sm-6">  
  2.     <div id="chart_container_line" style="border:5px solid gray;">  
  3.     </div>  
  4. </div>  

BARCAHRTTEMPLATE.HTML

  1. <div class="col-sm-6">  
  2.     <div id="chart_container_bar" style="border:5px solid gray;">  
  3.     </div>  
  4. </div>  

COLUMNCAHRTTEMPLATE.HTML

  1. <div class="col-sm-6">  
  2.     <div id="chart_container_column" style="border:5px solid gray;">  
  3.     </div>  
  4. </div>  

PIECAHRTTEMPLATE.HTML

  1. <div class="col-sm-6">  
  2.     <div id="chart_container_pie" style="border:5px solid gray;">  
  3.     </div>  
  4. </div>  

The following page renders all the charts. From here, we basically pass its type and data as well. If you are going to add many charts, don’t forget to make entry here.

INDEX.CSHTML

  1. <div class="jumbotron" align="center">  
  2.   <strong>Highcharts with Angular JS and Web API</strong>  
  3. </div>  
  4. <div ng-controller="ChartController">  
  5.   <div class="row">  
  6.     <chart-directive type="column" data="TopBatsmenInODIList"></chart-directive>  
  7.     <chart-directive type="line" data="TopBatsmenInODIList"></chart-directive>  
  8.   </div>  
  9.   <div class="row">  
  10.        
  11.   </div>  
  12.   <div class="row">  
  13.     <chart-directive type="bar" data="TopBatsmenInODIList"></chart-directive>  
  14.     <chart-directive type="pie" data="TopBatsmenInODIList"></chart-directive>  
  15.   </div>  
  16. </div>  

As we discussed above, _Layout.cshtml file will be used to define your Angular module.

  1. <html ng-app="chartApp">  

It will also contain all the listed JS files like Service, Controller, App etc.

_LAYOUT.CSHTML

  1. <!DOCTYPE html>  
  2. <html ng-app="chartApp">  
  3. <head>  
  4.   <meta charset="utf-8" />  
  5.   <meta name="viewport" content="width=device-width" />  
  6.   <title>@ViewBag.Title</title>  
  7.   @Styles.Render("~/Content/css")  
  8.   @Scripts.Render("~/bundles/modernizr")  
  9.   
  10. </head>  
  11. <body>  
  12.   <div class="navbar navbar-inverse navbar-fixed-top">  
  13.     <div class="container">  
  14.       <div class="navbar-header">  
  15.         <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  16.           <span class="icon-bar"></span>  
  17.           <span class="icon-bar"></span>  
  18.           <span class="icon-bar"></span>  
  19.         </button>  
  20.         @Html.ActionLink("Application name""Index""Home"new { area = "" }, new { @class = "navbar-brand" })  
  21.       </div>  
  22.       <div class="navbar-collapse collapse">  
  23.         <ul class="nav navbar-nav">  
  24.           <li>@Html.ActionLink("Home""Index""Home"new { area = "" }, null)</li>  
  25.           <li>@Html.ActionLink("API""Index""Help"new { area = "" }, null)</li>  
  26.         </ul>  
  27.       </div>  
  28.     </div>  
  29.   </div>  
  30.   <div class="container body-content">  
  31.     @RenderBody()  
  32.     <hr />  
  33.     <footer>  
  34.       <p>© @DateTime.Now.Year - My ASP.NET Application</p>  
  35.     </footer>  
  36.   </div>  
  37.   
  38.   @Scripts.Render("~/bundles/jquery")  
  39.   @Scripts.Render("~/bundles/bootstrap")  
  40.   @RenderSection("scripts", required: false)  
  41.   
  42.   <script src="~/Scripts/angular.min.js"></script>  
  43.   
  44.   <script src="~/Scripts/Highcharts-4.0.1/js/highcharts.js"></script>  
  45.   <script src="~/Scripts/Highcharts-4.0.1/js/modules/drilldown.js"></script>  
  46.   <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>  
  47.   
  48.   <script src="~/App/app.js"></script>  
  49.   <script src="~/App/controller/chartController.js"></script>  
  50.   <script src="~/App/service/chartService.js"></script>  
  51.   <script src="~/App/directive/chartDirective.js"></script>  
  52. </body>  
  53. </html>  

Now, everything has been setup, it’s time to run your application. Run and enjoy.

In the next part, we will cover the Highcharts Drilldown functionality.

Conclusion

Today, we have learned how to create charts using Highcharts and AngularJS custom directive and Web API.

I hope this post helps you. Please put your feedback which will help me improve for the next post. If you have any doubts, please ask in the comments section.


Similar Articles