Demonstration of Average Call Time(ACT) for Telecaller using Line Fusion Chart with AngularJS and WebAPI.

Rajnilari2015
Posted by in AngularJS category on for Beginner level | Points: 250 | Views : 724 red flag

In this article, we will look into the integration of Line Fusion Chart with AngularJS and WebAPI.


 Download source code for Demonstration of Average Call Time(ACT) for Telecaller using Line Fusion Chart with AngularJS and WebAPI.

Recommendation
Read Integration of MSLine Fusion Chart with AngularJS and WebAPI before this article.

Introduction

Data Visualization is a very important aspect from the view point of an end user since it presents the visual representation of data. It brings clarity in communicating the information to the end users via the graphs, plots, tables, charts etc. It is through the application of proper and effective visualization that help users to analyze and reason about data and evidence which on the other hand brings more data accessibility, understand ability and thereby usability leading to make proper strategic decision.

FusionCharts, part of InfoSoft Global (P) Ltd, is privately held software provider of data visualization products (JavaScript Charts, Maps, Widgets and Dashboards).

In this article, we will look into the integration of Line Fusion Chart with AngularJS and WebAPI.

Objective

Consider a scenario where say a telecaller is calling to his/her customer(s) over a day. Now say the telecaller has made a call to 50 customers in a day and the Total time taken is say 450 minutes. So the Average Call Time(ACT) will be 450/50 = 9 minutes per customer.The objective is to plot a Fusion Chart of type line where the x-axis will display the days (date-wise) on which telecaller made the call (consider it of the total number of days for the current month) and the y-axis will plot the Average Call Time(ACT) in minutes.

The final output should be as under

Using the code

Let us first create the data structure

public class ACT
{
    public string label { get; set; }
    public string value { get; set; }
}

and then write the WebAPI as under

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace WebAPI2.Controllers
{
    public class ACTReportController : ApiController
    {

        [Route("FusionChartReports/ACTReport")]
        public List<ACT> GetACTReport()
        {
            var objACT = new List<ACT>();

            Enumerable
               .Range(1, GetTotalNoOfDaysInCurrentMonth())
               .ToList()
               .ForEach(i =>objACT.Add(new ACT { label = i.ToString(), value = GetRandomNumbers(1, 200).ToString() }));

            return objACT;
        }

        /// <summary>
        /// Get Total No Of Days In CurrentMonth
        /// </summary>
        /// <returns></returns>
        private int GetTotalNoOfDaysInCurrentMonth()
        {
            DateTime date = DateTime.Now;
            var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
            var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            return Convert.ToInt32((lastDayOfMonth - firstDayOfMonth).TotalDays) + 1;
        }

        /// <summary>
        /// generate random numbers
        /// </summary>
        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();
        private static int GetRandomNumbers(int minVal, int maxVal)
        {
            lock (syncLock) return random.Next(minVal, maxVal);
        }
    }
}

When we ran this in PostMan, we receive the below JSON

Now, it's time to consume the web services through AngularJS and use the Fusion line Chart for the Data Visualization part.

For this to happen we will create a "ACTReport.js" file where we will define the Module, Controller and the Service consumption.

var myApp = angular.module("myApp", []);
myApp.controller("ACTReportController", function($scope, $http){

    $scope.displayChart = function(){

        FusionCharts.ready(function () {

        var actMonthlyChart; //declare an object that will hold the instance of the FusionChart
        
        var chartLayout = {
                        "caption": "Average Call Time(ACT) for Telecaller",
                        "yaxisname": "Time (Minutes)",
                        "xaxisname": "Date",
                        "yaxismaxvalue": "180",
                        "divlinealpha": "30",
                        "showvalues": "1",
                        "canvasbgalpha": "0",
                        "bgalpha": "0",
                        "plotborderalpha": "0",
                        "showborder": "0",
                        "showalternatehgridcolor": "0",
                        "plotgradientcolor": "",
                        "showplotborder": "0",
                        "numdivlines": "5",
                        "showyaxisvalues": "0",
                        "palettecolors": "#62e515",
                        "canvasborderthickness": "1",
                        "canvasbordercolor": "#2c1913",
                        "basefontcolor": "#cf5b6f",
                        "divlinecolor": "#00ffff",
                        "tooltipborderalpha": "0"
                          };
            
            //invoke the service            
            var promiseGet = $http.get("http://localhost:60422/FusionChartReports/ACTReport");

            promiseGet.then(function (promiseObj) {             
                            
                    // binding the data
                    $scope.dataSource = { 'chart' : chartLayout, 'data': promiseObj.data};

                    actMonthlyChart = new FusionCharts({
                                        type: 'line',
                                        renderAt: 'chartContainer',                                             
                                        width: "800",
                                        height: "500",
                                        dataFormat: 'json',
                                        dataSource : $scope.dataSource

                                        });

                    //render the chart to the div which is chartContainer
                    actMonthlyChart.render("chartContainer");                   
            },function (errorPl) { //if something went wrong
                  $log.error('failure loading the record', errorPl);
              }
          ); //end of promise object
        });//end Fusion Chart Ready Function
    }; //end function displayChart
}); //end controller

Here we are consuming the WebAPI by using the promise object and then merging the JSON in the way Fusion Chart needs that. The Fusion Data Source needs a)chart and b)data. The later is coming from the API.

Finally we need to bind the data. For this, let us create "ACTReport.html" and add the below script

<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>Average Call Time(ACT) for Telecaller Monthly Report</title> 
      <link href="css/custom.css" rel="stylesheet">
      <link href="css/custom2.css" rel="stylesheet">  
      <script data-require="angular.js@1.4.0-beta.6" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script> 
      <script type='text/javascript' src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
      <script type='text/javascript' src="http://static.fusioncharts.com/code/latest/fusioncharts.widgets.js"></script>     
      <script type='text/javascript' src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js"></script>
      <script src="ACTReport.js"></script>
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body ng-app="myApp"> 
        <div 
                id="chartContainer" 
                ng-controller="ACTReportController" 
                align="center"  
                ng-init='displayChart()'>
        </div>         
    </body>
</html>

That's all we need to do for the complete integration.

N.B.~ This article assumes that users have previous experience in AngularJS and WebAPI. If not then here is a good point to start

  1. AngularJS How to tutorials
  2. CRUD Operation using Web API and MVC4

Conclusion

Hope this will be helpful. Thanks for reading the article. Zipped file attached.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Posted by: Debendra256 on: 4/1/2016 | Points: 25
Great Example Sir....
Posted by: Poster on: 4/2/2016 | Points: 25
Niladri, it would be good to write tutorials on the product that are available free. It feels like you are promoting FusionChart and I do not know how many will be ready to buy this.


Posted by: Rajnilari2015 on: 4/3/2016 | Points: 25
@Poster, I am not promoting any FusionChart product . It's those charts that we are using in the project. Henceforth are the examples.(see the background description) (:

Login to post response

Comment using Facebook(Author doesn't get notification)