FusionCharts AngularJS Plugin

Introduction - In this article, we will learn how to use the FusionCharts Plugin with the AngularJS Framework. I hope you will like this.

What’s FusionCharts Plugin?

FusionCharts Suite XT includes an extensive range of charts, gauges, and maps that you can use to plot all types of static and real-time data.

The Angular-FusionCharts plugin, along with FusionCharts Suite XT, lets you add interactive JavaScript charts and graphs to your web and mobile applications using only a single AngularJS directive.

You can download the zip file from FusionCharts.


Features

  • Add a chart using just a single directive.
  • Auto-updates your chart object on modifying scope.
  • Angular-friendly events let you call controller functions directly from the chart.
  • Offers advanced control by giving you access to full FusionCharts object.
  • Has a variety of ways to add a chart, from JSON URL to Scope Array Binding.
  • Plenty of examples and good documentation.

Create your Application

Open Visual Studio and select File, click New Project.

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.



Next, new dialog will pop up for selecting the template. We are going choose Empty and click OK.



Angular Part

First of all, we need to create  a new folder which includes two js files respectively: AppChart.js and ChartController.js.



In order to create new js files, right click on AngularApp folder > Add > JavaScript File.





AppChart.js

  1. var app = angular.module('myApp', ['ngRoute''ng-fusioncharts']);  
  2. app.config(['$routeProvider'function ($routeProvider) {  
  3.   
  4.     $routeProvider.when('/ChartDemo', {  
  5.   
  6.         templateUrl: '/DemoChart.html',  
  7.         controller: 'ChartController'  
  8.   
  9.     }).otherwise({  
  10.         controller: 'ChartController'  
  11.     })  
  12.   
  13. }]);  
Here, we have defined AngularJS module (in our case myApp), and injected ngRoute module which allow us to use routing then ng-fusioncharts module that give us access to use different features of fusionchart like directives, events…

ChartController.js
  1. app.controller('ChartController'function ($scope) {  
  2.   
  3.     $scope.DataSource1 = {  
  4.         chart: {  
  5.             caption: "Harry's SuperMart",  
  6.             subCaption: "Top 5 stores in last month by revenue",  
  7.             numberPrefix: "$",  
  8.             theme: "fint"  
  9.         },  
  10.         data: [{  
  11.             label: "Bakersfield Central",  
  12.             value: "880000"  
  13.         },  
  14.         {  
  15.             label: "Garden Groove harbour",  
  16.             value: "730000"  
  17.         },  
  18.         {  
  19.             label: "Los Angeles Topanga",  
  20.             value: "590000"  
  21.         },  
  22.         {  
  23.             label: "Compton-Rancho Dom",  
  24.             value: "520000"  
  25.         },  
  26.         {  
  27.             label: "Daly City Serramonte",  
  28.             value: "330000"  
  29.         }]  
  30.     }  
  31.   
  32.     $scope.DataSource2  = {  
  33.         chart: {  
  34.             caption: "Age profile of website visitors",  
  35.             subcaption: "Last Year",  
  36.             startingangle: "120",  
  37.             showlabels: "0",  
  38.             showlegend: "1",  
  39.             enablemultislicing: "0",  
  40.             slicingdistance: "15",  
  41.             showpercentvalues: "1",  
  42.             showpercentintooltip: "0",  
  43.             plottooltext: "Age group : $label Total visit : $datavalue",  
  44.             theme: "fint"  
  45.         },  
  46.         data: [  
  47.             {  
  48.                 label: "Teenage",  
  49.                 value: "1250400"  
  50.             },  
  51.             {  
  52.                 label: "Adult",  
  53.                 value: "1463300"  
  54.             },  
  55.             {  
  56.                 label: "Mid-age",  
  57.                 value: "1050700"  
  58.             },  
  59.             {  
  60.                 label: "Senior",  
  61.                 value: "491000"  
  62.             }  
  63.         ]  
  64.     }  
  65.      
  66.   
  67. });  
Create HTML Page - For doing this, right click on project name > Add > HTML Page



DemoChart.html
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>.: Fuusion Chart Using AngularJS :. </title>  
  5.     <meta charset="utf-8" />  
  6. </head>  
  7. <body>  
  8.   
  9.     <div style="margin-left:50px;">  
  10.   
  11.         <div fusioncharts  
  12.              width="600"  
  13.              height="400"  
  14.              type="column2d" style="float:left"  
  15.              dataSource="{{DataSource1}}">  
  16.         </div>  
  17.   
  18.   
  19.         <div fusioncharts  
  20.              width="600"  
  21.              height="400"  
  22.              type="pie3d"   
  23.              dataSource="{{DataSource2}}">  
  24.         </div>  
  25.   
  26.     </div>   
  27.   
  28. </body>  
  29. </html>  
Before running, we need to add some libraries inside index.html as shown below.

Index.html
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <meta charset="utf-8" />  
  6.     <!-- CSS -->  
  7.     <link href="CSS/mainstyle.css" rel="stylesheet" />  
  8.   
  9. </head>  
  10. <body ng-app="myApp">  
  11.   
  12.     <div class="row" ng-view>  
  13.   
  14.     </div>  
  15.   
  16.     <!-- JS -->  
  17.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  18.     <script src="https://code.angularjs.org/1.4.7/angular-route.js"></script>  
  19.   
  20.     <script src="AngularApp/AppChart.js"></script>  
  21.     <script src="AngularApp/ChartController.js"></script>  
  22.   
  23.     <script src="scripts/fusioncharts.charts.js"></script>  
  24.     <script src="scripts/fusioncharts.js"></script>  
  25.     <script src="scripts/angular-fusioncharts.min.js"></script>  
  26. </body>  
  27. </html>  
Output




That’s all. Please send your feedback and queries in comments box.