Charts In AngularJS Using Web API

In the development world, you may get a requirement from your client, which might be weekly, quarterly or yearly charts. You always think, how can I make charts with the use of AngularJS? Today, in this article, we are going to learn the following.

  • How to create charts in AngularJS, using WebAPI.
  • Higcharts.js

In this example, our requirement is that we have a drop-down list of the student names.

If the user selects any student, it will show the selected student's marks in the form of charts.

What are Highcharts?

HighCharts is a new Rich javascript based chart library. With the use of high charts, you can create the interactive charts as per your requirement.

Below is the cdn for Highcharts.

<script src="https://code.highcharts.com/highcharts.js"></script>

HighChart Features

We know HighCharts is a JavaScript based library, so HighCharts uses features in JSON format. Thus, I am going to describe some features of HighCharts.

  1. title
    It is used to show the title in the header part.

  2. Subtitle
    It is used to show sub title in the header part.

  3. xAxis
    You can define here in the form of an array.

  4. yAxis
    It defines y axis.

  5. legend
    It helps in setting alignment and layout of the chart.

  6. Chart
    Here you can define type, like what type of charts you want, For example - Column, Area, Line, Pie etc.

  7. Series
    This is the main feature. This is the place, where we define the data in the form of an array.

  8. Highcharts.chart
    This is used to show the charts. Here, we have to define the Id name of div. For example, I use here Highcharts.chart('container'). Here, the container is used as div Id. To call this, we have to create it.
    1. <div id='container'>  
    2. </div>  
    3. Highcharts.chart('container', { title: { text: 'Monthly Examination Summary', x: -20 //center }, subtitle: { text: 'Source: ST. Thomas School', x: -20 }, xAxis: { categories: ['Hindi', 'Maths', 'English', 'Science'] }, yAxis: { title: { text: 'Marks' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', borderWidth: 0 }, series: [{ name: $scope.studentMarks[0].Name, data: [$scope.studentMarks[0].Hindi, $scope.studentMarks[0].Maths, $scope.studentMarks[0].English, $scope.studentMarks[0].Science] }] });  

Now, let's come to the code and understand more in regards to how we can use Highcharts.

Step 1

Open Visual Studio.

Step 2

File>>New>> Project.

Programming

Choose ASP.NET Web Application.

Step 3

Select WebAPI.

Programming

Click OK button.

Check Solution Explorer.

Programming

In this article, we have StudentMarksMaster table.

Programming

Programming

Now, let's create an ADO.NET Entity Data Model file to connect the tables.

Programming

Click Add button. Create an EDMX file, which is based on Wizard.

  • Once you click on the finish button, your screen looks, as shown below.

    Programming

Now, let's talk about code.

First of all, we want to display the student's name in the drop-down list. For this, I am going to create a StudentModel class.

StudentModel.cs

  1. public class StudentModel {  
  2.     public int StudentId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public String StudentName {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
You can see, in the code, mentioned above, we need this data in our drop-down list. Thus, we will use StudentId for the value and StudentName for the text.

Now in the next step, I am going to write code for StudentController class to fetch the data into drop-down list.

StudentController.cs
  1. using AngularwithWebAPI.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. namespace AngularwithWebAPI.Controllers {  
  9.     public class StudentController: ApiController {  
  10.         DbContext.StudentEntities studentEntities = new DbContext.StudentEntities();  
  11.         public IHttpActionResult GetStudents() {  
  12.             var query = studentEntities.StudentMarksMasters.Select(x => new StudentModel {  
  13.                 StudentId = x.Id, StudentName = x.Name  
  14.             }).ToList();  
  15.             return Ok(query);  
  16.         }  
  17.   
  18.         public IHttpActionResult GetStudentMarks(int studentId) {  
  19.             var query = studentEntities.StudentMarksMasters.Where(x => x.Id == studentId).ToList();  
  20.             return Ok(query);  
  21.         }  
  22.     }  
  23. }  
Script.js
  1. angular.module('myModule', [])  
  2. .controller('myController'function ($scope, $http) {  
  3. $http.get('http://localhost:50623/api/student/getstudents').then(function(response) {    
  4.     $scope.students = response.data;    
  5.     $scope.studentId = $scope.students[0].StudentId;    
  6.     $scope.studentChange($scope.students[0].StudentId);    
  7. });    
  8.     
  9. $scope.studentChange = function(studentId) {    
  10. $http.get('http://localhost:50623/api/student/getstudentmarks', {    
  11.     params: {    
  12.         studentId: studentId    
  13.     }    
  14. }).then(function(response) {    
  15.     $scope.studentMarks = response.data;    
  16.     
  17.     Highcharts.chart('container', {    
  18.         chart: {    
  19.             type: 'line'    
  20.         },    
  21.         title: {    
  22.             text: 'Monthly Examination Summary',    
  23.             x: -20 //center    
  24.         },    
  25.         subtitle: {    
  26.             text: 'Source: ST. Thomas School',    
  27.             x: -20    
  28.         },    
  29.         xAxis: {    
  30.             categories: ['Hindi''Maths''English''Science']    
  31.         },    
  32.         yAxis: {    
  33.             title: {    
  34.                 text: 'Marks'    
  35.             },    
  36.     
  37.         },    
  38.     
  39.         legend: {    
  40.             layout: 'vertical',    
  41.             align: 'right',    
  42.             verticalAlign: 'middle',    
  43.             borderWidth: 0    
  44.         },    
  45.         series: [{    
  46.             name: $scope.studentMarks[0].Name,    
  47.             data: [$scope.studentMarks[0].Hindi, $scope.studentMarks[0].Maths, $scope.studentMarks[0].English, $scope.studentMarks[0].Science]    
  48.         }]    
  49.     });    
  50. });    
  51. }    
  52. });    
index.html
  1. <!DOCTYPE html>  
  2. <html ng-app="myModule">  
  3.   
  4. <head>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>  
  6.     <script src="https://code.highcharts.com/highcharts.js"></script>  
  7.     <script src="Scripts/js/Script.js"></script>  
  8. </head>  
  9.   
  10. <body ng-controller="myController">  
  11.     <table>  
  12.         <tr>  
  13.             <td>  
  14.                 Select Student  
  15.             </td>  
  16.             <td>  
  17.                 <select ng-model="studentId" ng-change="studentChange(studentId)">  
  18. <option data-ng-repeat="st in students" value="{{st.StudentId}}">{{st.StudentName}}</option>  
  19. </select>  
  20.             </td>  
  21.         </tr>  
  22.     </table>  
  23.   
  24.     <br />  
  25.   
  26.     <div id="container"></div>  
  27. </body>  
  28.   
  29. </html>  
Now, the time has come to run the code.

Programming

You can see in the code, mentioned above, first I create studentcontroller.cs and make two methods.
  1. GetStudents() For getting students in dropdownlist.
  2. GetStudentMarks(int studentId) on change event of dropdownlist this funcion will gives you data of students subjectwise.

Now, in script.js, I called these two methods after which I called highcharts.charts and the Id div. chart information store in $scope.studentMarks. We have to call this into series data property. One more thing here is, I am using chart:{type:line}. You can see in the output, mentioned above, it shows Line chart. If you want to change this, just simply change here. For example, if you want to create Column graph, just put column and your output will look, as shown below.

Programming

Now, in index.html, call <div id='container'></div>

That's it. Your chart is created, I hope you enjoyed this article. If you have any suggestions, please send your valuable comments. Happy programming.